通过 WCF 的套接字连接协议 net.tcp 的问题

Posted

技术标签:

【中文标题】通过 WCF 的套接字连接协议 net.tcp 的问题【英文标题】:Issue with socket connecting protocol net.tcp via WCF 【发布时间】:2020-08-25 17:24:34 【问题描述】:

对于以下所有用例,我都遇到过这种情况。 除了情况 3 正常且一般,其余情况需要解决

以下这些服务运行时没有任何错误:

    Net.Msmq 侦听器适配器 Net.Pipe 侦听器适配器 Net.Tcp 侦听器适配器 Net.Tcp 端口共享服务 Windows 进程激活服务

还启用了协议:net.tcp,http

在调用 WCF 编写的服务时从命令行或通过应用程序:

C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6.1 Tools>svcutil net.tcp://localhost/TNA/TAServices/AuthenticationManager/mex

案例 1:

Could not connect to
net.tcp://localhost/TNA/TAServices/AuthenticationManager/mex. The
connection attempt lasted for a time span of 00:00:04.0935290. TCP
error code 10061: No connection could be made because the target
machine actively refused it 127.0.0.1:808.
No connection could be made because the target machine actively refused it 127.0.0.1:808

案例 2:

There was no endpoint listening at
net.tcp://localhost/TNA/TAServices/AuthenticationManager/mex that
could accept the message. This is often caused by an incorrect address
or SOAP action. See InnerException, if present, for more details.

案例 3:

The socket connection was aborted. This could be caused by an error
processing your message or a receive timeout being exceeded by the
remote host, or an underlying network resource issue. Local socket
timeout was '00:04:59.9843875'.
An existing connection was forcibly closed by the remote host

案例 2 的事件日志:

An error occurred in the Activation Service 'NetTcpActivator' of the protocol 'net.tcp' while trying to listen for the site '1', thus the protocol is disabled for the site temporarily. See the exception message for more details.
 URL: WeakWildcard:net.tcp://username.domainname.com/
 Status: FailedToListen
 Exception: System.ServiceModel.AddressAlreadyInUseException: There is already a listener on IP endpoint 0.0.0.0:808. This could happen if there is another application already listening on this endpoint or if you have multiple service endpoints in your service host with the same IP endpoint but with incompatible binding configurations. ---> System.Net.Sockets.SocketException: Only one usage of each socket address (protocol/network address/port) is normally permitted
   at System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot, SocketAddress socketAddress)
   at System.Net.Sockets.Socket.Bind(EndPoint localEP)
   at System.ServiceModel.Channels.SocketConnectionListener.Listen()
   --- End of inner exception stack trace ---
   at System.ServiceModel.Channels.SocketConnectionListener.Listen()
   at System.ServiceModel.Activation.TransportListener.Go(IConnectionListener connectionListener)
   at System.ServiceModel.Activation.TransportListener..ctor(IPEndPoint endPoint)
   at System.ServiceModel.Activation.TransportListener.Listen(IPEndPoint endPoint)
   at System.ServiceModel.Activation.RoutingTable.TcpStart(MessageQueue messageQueue, BaseUriWithWildcard path)
   at System.ServiceModel.Activation.MessageQueue.Register(BaseUriWithWildcard path)
   at System.ServiceModel.Activation.ListenerAdapter.RegisterBindings(IActivatedMessageQueue queue, Int32 siteId, String[] bindings, String path)
 Process Name: SMSvcHost
 Process ID: 4608

我们应该如何实现默认情况下进入案例3,无论何时何地,例如引导和重新启动系统?

【问题讨论】:

【参考方案1】:

Case 1Case 2 在 WCF 服务配置中需要一个 Mex 服务终结点。请添加一个 Mex 端点以交换服务元数据。

<system.serviceModel>
        <services>
            <service name="WcfService1.Service1">
                <endpoint address="service1" binding="basicHttpBinding" contract="WcfService1.IService1" ></endpoint>
                <endpoint address="service2" binding="netTcpBinding"  contract="WcfService1.IService1"></endpoint>
                <!--for exchanging service metadata.-->
                <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange"></endpoint>
            </service>
        </services>

如果有什么我可以帮忙的,请随时告诉我。

【讨论】:

以上是关于通过 WCF 的套接字连接协议 net.tcp 的问题的主要内容,如果未能解决你的问题,请参考以下文章

如何将基于套接字的客户端与 WCF (net.tcp) 服务一起使用?

升级到 Windows 10 后 net.tcp 无法正常工作

WCF传输协议

net.tcp协议共享tcp端口的功能

在从 net.tcp 协议连接到 WCF 的端点处侦听的问题

WCF Net.tcp 仅在本地失败