【问题标题】:WCF Multiple Service EndpointsWCF 多个服务端点
【发布时间】:2010-07-22 18:19:33
【问题描述】:

我将 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>

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

【问题讨论】:

  • 如果您发布代码或 XML,在文本编辑器中突出显示这些行,然后单击编辑器工具栏上的“代码”按钮 (101 010) 以很好地格式化和语法高亮!

标签: wcf wcf-binding wcf-client


【解决方案1】:

不,您不能添加第二个基于 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 使用特定的完全限定 URL,您可以在以下位置调用它

http://xxx.xxx.xxx:8080/MathServiceLibrary

但是:您为什么要拥有第二个端点?如果您想公开不同的协议,通常只有第二个端点,例如您通常会有一个 http、一个 https、一个 net.tcp 端点等等。拥有两个具有相同合同和相同绑定的独立 http 端点对我来说并没有多大意义.....

【讨论】:

  • 我应该更清楚。我真正想做的就是将 localhost 更改为 IP,并让其他机器可以使用该服务,并且能够从本地机器上的客户端调用该服务。
猜你喜欢
  • 2011-09-19
  • 1970-01-01
  • 2011-08-18
  • 1970-01-01
  • 1970-01-01
  • 2011-11-13
  • 1970-01-01
  • 2012-06-17
  • 2012-01-25
相关资源
最近更新 更多