使用两个tcp端口上的net.tcp绑定在IIS 7(WAS)中托管WCF服务

5 wcf iis-7 was net.tcp

默认情况下,IIS 7网站的net.tcp绑定带有"808:"绑定信息字符串.如果我添加另一个net.tcp绑定与"xxx: "异常发生:

此集合已包含方案net.tcp的地址.此集合中每个方案最多只能有一个地址.参数名称:item

如何解决这个问题并在两个端口监听我的服务?

mar*_*c_s 8

基本上,在您的服务中,您应该能够在任意数量的端口上定义任意数量的服务端点.

有两种方法可以做到这一点:

  • 在服务端点中定义基址和相对地址
  • 定义每个端点中的完整地址

如果你做选项#1,你会有这样的事情:

<service name="YourService">
  <host>
    <baseAddresses>
      <add baseAddress="net.tcp://YourServer:5151/Services" />
    </baseAddresses>
  </host>
  <endpoint name="endpoint1"
            address="Service1"
            binding="netTcpBinding"
            contract="IYourService" />
  <endpoint name="endpoint2"
            address="Service2"
            binding="netTcpBinding"
            contract="IYourService" />
</service>
Run Code Online (Sandbox Code Playgroud)

因此,在这种情况下,您有相同合同的两个服务端点,他们将监听URL

net.tcp://YourServer:5151/Services/Service1
Run Code Online (Sandbox Code Playgroud)

net.tcp://YourServer:5151/Services/Service2
Run Code Online (Sandbox Code Playgroud)

您可以拥有多个服务端点,但只能有一个基址.

另一个选项是不指定基址,并直接在端点中指定完整的服务地址:

<service name="YourService">
  <endpoint name="endpoint1"
            address="net.tcp://YourServer:5151/Services/Service1"
            binding="netTcpBinding"
            contract="IYourService" />
  <endpoint name="endpoint2"
            address="net.tcp://YourServer:6868/Services/Service2"
            binding="netTcpBinding"
            contract="IYourService" />
</service>
Run Code Online (Sandbox Code Playgroud)

在这种情况下,由于您在端点中定义了整个地址,因此可以选择两个不同的TCP端口,每个端点一个端口.这应该没有任何问题.您在两个单独的端口上有两个单独的端点,这两个端口都在后台监听和由同一服务类提供服务.