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

posted @   jeffery1010  Views(552)  Comments(0Edit  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗
历史上的今天:
2016-05-16 远程git url
点击右上角即可分享
微信分享提示