WCF多服务端点

时间:2010-07-22 18:19:34

标签: wcf wcf-binding wcf-client

我将WCF服务作为Windows服务托管。

下面的快照。

myHost = new ServiceHost(typeof(AnalyticsService));
Uri address = new Uri("http://localhost:8080/MathServiceLibrary");

WSHttpBinding binding = new WSHttpBinding();
binding.MaxReceivedMessageSize = 2147483647;
binding.MaxBufferPoolSize = 2147483647;

//binding.Security.Mode = SecurityMode.None;
Type contract = typeof(IAnalyticsService);

myHost.AddServiceEndpoint(contract,binding,address);

正如您所看到的,我之前只是在本地公开服务。我想添加另一个ServiceEndpoint,以便我网络上的其他机器也可以调用该服务。

我假设我需要在上面的代码中添加这样的内容:

myHost = new ServiceHost(typeof(AnalyticsService));

Uri address = new Uri("http://localhost:8080/MathServiceLibrary");
Uri new address = new Uri("http://xxx.xxx.xxx:8080/MathServiceLibrary");

WSHttpBinding binding = new WSHttpBinding();
binding.MaxReceivedMessageSize = 2147483647;
binding.MaxBufferPoolSize = 2147483647;

Type contract = typeof(IAnalyticsService);

myHost.AddServiceEndpoint(contract, binding, address);
myHost.AddServiceEndpoint(contract, binding, newaddress);

现在我当前的服务库APP配置如下:

 <system.serviceModel>
    <services>
      <service behaviorConfiguration="ServiceLibrary.Service1Behavior"
        name="ServiceLibrary.AnalyticsService">
        <endpoint address="" binding="wsHttpBinding" contract="ServiceLibrary.IAnalyticsService">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8731/Design_Time_Addresses/ServiceLibrary/Service1/" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceLibrary.Service1Behavior">
          <!-- To avoid disclosing metadata information, 
          set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="True"/>
          <!-- To receive exception details in faults for debugging purposes, 
          set the value below to true.  Set to false before deployment 
          to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="False" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

虽然服务库主机中的app.config

  <system.serviceModel>
    <services>
      <service name="ServiceLibrary.AnalyticsService"
               behaviorConfiguration ="MathServiceMEXBehavior">
        <endpoint address="mex"
                  binding="mexHttpBinding"
                  contract="IMetadataExchange"/>
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8080/MathService"/>
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="MathServiceMEXBehavior">
          <serviceMetadata httpGetEnabled="true"/>
          <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

我假设我需要使用外部(非本地主机)地址向服务库文件添加另一个基地址。我对服务库文件中要更改的内容感到困惑。

1 个答案:

答案 0 :(得分:0)

不,你不能添加第二个基于http的基地址 - 你只能为每个协议选择一个 - 一个用于http,一个用于net.tcp,依此类推。

您需要做的是在app.config中为您的服务主机应用程序创建第二个端点(甚至不会使用服务库中的app.config - 忘记该文件),并为其分配一个完全合格的地址。

<system.serviceModel>
    <services>
      <service name="ServiceLibrary.AnalyticsService"
               behaviorConfiguration ="MathServiceMEXBehavior">

        <endpoint name="firstEndpoint"
                  address=""
                  binding="wsHttpBinding"
                  contract="IAnalyticsService" />

        <endpoint name="secondEndpoint"
                  address="http://xxx.xxx.xxx:8080/MathServiceLibrary"
                  binding="wsHttpBinding"
                  contract="IAnalyticsService" />

        <endpoint address="mex"
                  binding="mexHttpBinding"
                  contract="IMetadataExchange"/>
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8080/MathService"/>
          </baseAddresses>
        </host>
      </service>
    </services>
  </system.serviceModel>

因此firstEndpoint将使用基地址,您可以在

下调用它
http://localhost:8080/MathService

secondEndpoint使用特定的完全限定网址,您可以在

下调用它
http://xxx.xxx.xxx:8080/MathServiceLibrary

然而:为什么你想要第二个终点?如果要公开不同的协议,通常只有第二个端点,例如你通常有一个http,一个https,一个net.tcp端点等等。有两个单独的http端点具有相同的合同和相同的绑定对我来说真的没有多大意义.....