wcf中有多个端点

时间:2014-07-31 15:05:07

标签: c# wcf wcf-endpoint hosted

我尝试为单个WCF服务执行多个端点,以便每个端点都有其独立的接口/方法。我使用的是TCP

app.config

<configuration>
  ....
  <system.serviceModel>
    <services>
      <service name="WCFLibrary.CalculatorService">
          <host>
            <baseAddresses>
              <!--<add baseAddress="http://localhost:8788/CalculatorService/" />-->
              <add baseAddress="net.tcp://localhost:8523/CalculatorService" />
            </baseAddresses>
          </host>
          <endpoint name="ServiceTCPEndPoint" 
              address="" 
              binding="netTcpBinding" 
              contract="WCFLibrary.ICalculator">
            <identity>
              <dns value="localhost" />
            </identity>
          </endpoint>
          <endpoint name="ServiceMexEndPoint" address="mex" binding="mexTcpBinding" contract="IMetadataExchange" />
      </service>
      <service name ="WCFLibrary.MyWorldService">
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://localhost:8524/MyWorldService"/>
          </baseAddresses>
        </host>
        <endpoint name="HelloWorldTCPEndpoint" 
            address="" 
            binding="netTcpBinding" 
            contract="WCFLibrary.MyWorld">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint name="HelloWorldMexEndPoint" 
            address="mex" 
            binding="mexTcpBinding" 
            contract="IMetadataExchange" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata/>
          <serviceDebug includeExceptionDetailInFaults="True" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

我的第一个端点正在工作,但我的第二个端点无效。这可能有什么不对。其次,我们为什么不能浏览tcp,就像我们可以为HTTP做的那样。我知道我们可以使用svcutil测试tcp。无论如何都要浏览TCP。

编辑:

WCF:

http://pastebin.com/gHHRKeYZ

WindowsService:

http://pastebin.com/kjM3iRYj

2 个答案:

答案 0 :(得分:2)

所有net.tcp端点都应使用port sharing使用相同的端口。

MSDN中有几篇文章/教程,以及如何实现这一点。这是one

尝试此配置设置

<system.serviceModel>
<bindings>
  <netTcpBinding>
    <binding name="tcpDefault" portSharingEnabled="true" />

  </netTcpBinding>
</bindings>
<services>
  <service name="WCFLibrary.CalculatorService">

      <host>
        <baseAddresses>
          <!--<add baseAddress="http://localhost:8788/CalculatorService/" />-->
          <add baseAddress="net.tcp://localhost:8523/CalculatorService" />
        </baseAddresses>
      </host>
      <!-- Service Endpoints -->
      <!-- Unless fully qualified, aBddress is relative to base address supplied above -->
      <endpoint name="ServiceTCPEndPoint" address="" binding="netTcpBinding" bindingConfiguration="tcpDefault" contract="WCFLibrary.ICalculator">
        <!-- 
          Upon deployment, the following identity element should be removed or replaced to reflect the 
          identity under which the deployed service runs.  If removed, WCF will infer an appropriate identity 
          automatically.
      -->
        <identity>
          <dns value="localhost" />
        </identity>
      </endpoint>
      <!-- Metadata Endpoints -->
      <!-- The Metadata Exchange endpoint is used by the service to describe itself to clients. -->
      <!-- This endpoint does not use a secure binding and should be secured or removed before deployment -->
      <endpoint name="ServiceMexEndPoint" address="mex" binding="mexTcpBinding" contract="IMetadataExchange" />
    </service>
  <service name ="WCFLibrary.MyWorldService">
    <host>
      <baseAddresses>
        <add baseAddress="net.tcp://localhost:8523/MyWorldService"/>
      </baseAddresses>
    </host>
    <endpoint name="HelloWorldTCPEndpoint" address="" binding="netTcpBinding"  bindingConfiguration="tcpDefault" contract="WCFLibrary.MyWorld">
      <identity>
        <dns value="localhost" />
      </identity>

    </endpoint>
    <endpoint name="HelloWorldMexEndPoint" address="mex" binding="mexTcpBinding" contract="IMetadataExchange" />
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior>
      <!-- To avoid disclosing metadata information, 
      set the values below to false before deployment -->
      <!--<serviceMetadata httpGetEnabled="False" httpsGetEnabled="False" />-->
      <serviceMetadata/>
      <!-- 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="True" />
    </behavior>
  </serviceBehaviors>
</behaviors>

答案 1 :(得分:1)

如果您使用的是@Staish配置,则可以尝试在服务行为中取消注释此行。

 <!--<serviceMetadata httpGetEnabled="**True**" httpsGetEnabled="False" />-->

然后您就可以使用svcutil来创建代理。

如果您愿意,可以在发布前对其进行评论。