WCF NetTcpBinding

服务端:

<system.serviceModel>
<bindings>
<netTcpBinding>
<binding portSharingEnabled="true" name="tcpbindingConfig">
<security mode="None"/>
</binding>
</netTcpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="serviceBehavior">
<!-- 为避免泄漏元数据信息,请在部署前将以下值设置为 false -->
<!--<serviceMetadata httpGetEnabled="True" httpGetUrl ="http://localhost:8300/myEndpoint"/>-->
<serviceMetadata httpGetEnabled="false" httpsGetEnabled="false"/>
<!-- 要接收故障异常详细信息以进行调试,请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息 -->
<serviceDebug includeExceptionDetailInFaults="false"/>
<!--<serviceThrottling maxConcurrentCalls="1000" maxConcurrentSessions="1000" maxConcurrentInstances="1000"/>-->
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
<services>
<service name="TWS.TcpService" behaviorConfiguration="serviceBehavior">
<endpoint address="" binding="netTcpBinding" contract="TWS.ITcpService" bindingConfiguration="tcpbindingConfig"/>
<endpoint address="mex" binding="mexTcpBinding" bindingConfiguration="" contract="IMetadataExchange"/>
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:8299/TcpService"/>
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>

宿主:

http://www.bubuko.com/infodetail-325542.html

客户端:

string serviceUri = "net.tcp://localhost:8298/TcpService.svc";
NetTcpBinding tcpbinding = new NetTcpBinding(SecurityMode.None);
tcpbinding.MaxReceivedMessageSize = int.MaxValue;
tcpbinding.ReaderQuotas.MaxDepth = 32;
tcpbinding.ReaderQuotas.MaxStringContentLength = int.MaxValue;
tcpbinding.SendTimeout = TimeSpan.MaxValue;
tcpbinding.ReceiveTimeout = new TimeSpan(0, 10, 10);
EndpointAddress endpoing = new EndpointAddress(serviceUri);
using (ChannelFactory<ITcpService> chf = new ChannelFactory<ITcpService>(tcpbinding,endpoing)) {
ITcpService proxy = chf.CreateChannel();
string result=proxy.GetData(10000);
MessageBox.Show(result);

}

注意安全模式需要客户端与服务端匹配,否则会报错

System.ServiceModel.CommunicationException:“套接字连接已中止。这可能是由于处理消息时出错或远程主机超过接收超时或者潜在的网络资源问题导致的....

原文地址:https://www.cnblogs.com/jeffry/p/10873872.html