如何在app.config文件中定义端点?

时间:2009-07-07 05:47:57

标签: wcf

如何在app.config文件中定义具有MEX端点的端点以及然后运行我的应用程序所需的内容。我有一个名为IXMLService的服务合同,我正在使用WsHttpBinding。 请举个例子。 创建app.config后,如何启动服务?

2 个答案:

答案 0 :(得分:6)

<system.serviceModel>
    <behaviors>
        <serviceBehaviors>
            <behavior name="MetadataBehavior">
                <serviceMetadata httpGetEnabled="true" />
            </behavior>
        </serviceBehaviors>
    </behaviors>
    <services>
        <service name="YourNamespace.XMLService" behaviorConfiguration="MetadataBehavior">

            <!-- Use the host element only if your service is self-hosted (not using IIS) -->
            <host>
                <baseAddresses>
                    <add baseAddress="http://localhost:8000/service"/>
                </baseAddresses>
            </host>

            <endpoint address=""
                      binding="wsHttpBinding"
                      contract="YourNamespace.IXMLService"/>

            <endpoint address="mex"
                      binding="mexHttpBinding"
                      contract="IMetadataExchange"/>
        </service>
    </services>
</system.serviceModel>

更新:要启动该服务,您可以编写以下控制台应用程序来托管它(通过添加以前的app.config):

class Program
{
    static void Main(string[] args)
    {
        using (var host = new System.ServiceModel.ServiceHost(typeof(XMLService)))
        {
            host.Open();
            Console.WriteLine("Service started. Press Enter to stop");
            Console.ReadLine();
        }
    }
}

答案 1 :(得分:2)

Darin的回答是几乎 - 你需要为你的服务和你的mex端点指定完整的地址,或者你需要添加一个基地址:

<system.serviceModel>
    <behaviors>
        <serviceBehaviors>
            <behavior name="MetadataBehavior">
                <serviceMetadata httpGetEnabled="true" />
            </behavior>
        </serviceBehaviors>
    </behaviors>
    <services>
        <service name="XMLService" behaviorConfiguration="MetadataBehavior">
          <host>
            <baseAddresses>
              <add baseAddress="http://localhost:8888/"/>
            </baseAddresses>
           </host>
           <endpoint address="MyService"
                      binding="wsHttpBinding"
                      contract="IXMLService"/>

           <endpoint address="mex"
                      binding="mexHttpBinding"
                      contract="IMetadataExchange"/>
        </service>
    </services>
</system.serviceModel>

您的服务位于http://localhost:8888/MyService,您的MEX数据位于http://localhost:8888/mex

如果您愿意,还可以直接在端点中指定完整地址:

        <service name="XMLService" behaviorConfiguration="MetadataBehavior">
           <endpoint address="http://localhost:8888/MyService"
                      binding="wsHttpBinding"
                      contract="IXMLService"/>

           <endpoint address="http://localhost:8888/MyService/mex"
                      binding="mexHttpBinding"
                      contract="IMetadataExchange"/>
        </service>

马克