介绍
怎么在WCF中使用nettcp协议实现通讯?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
<强> 1。建立服务服务端强>
还是用上次的代码,提供一个用户类,实现一个方法
[ServiceContract] public interface 国际单位 ,{ ,(OperationContract) ,string GetUserInfo (); ,} (ServiceContract) public interface 国际单位 ,{ ,(OperationContract) ,string GetUserInfo (); 以前,}><强> 2。ServiceHostManager公有类强>
通过公有类可以减少代码编写量,可以保存下来,以后用的时候直接拿来用
public interface IServiceHostmanager : IDisposable ,{ ,void 开始(); ,void 停止(); ,} ,public class  ServiceHostManager(); ,,shm.Start (); ,,while (真正的) ,,{ ,,if (conTokenSource.IsCancellationRequested ,,, shm !=, null) ,,{ ,,,shm.Stop (); ,,,休息; ,,} ,,} ,,} catch 才能;(Exception 交货) {才能 ,,Console.WriteLine (ex.Message); ,,if (shm !=, null), shm.Stop (); ,,} ,,},conTokenSource.Token); return 才能;任务; ,} 以前,}> <强> 3。配置的相关参数强>
配置文件中注意配置服务,绑定,行为。服务中配置端点指明abc,绑定中配置tcp通讯的要关参数,分别使用中配置http请求的地址
& lt; ? xml version=?.0“,编码=皍tf-8", ?比; & lt; configuration> ,& lt; system.serviceModel> ,& lt; services> ,& lt; service name=癶cbServiceB.User", behaviorConfiguration=皍serBehavior"比; ,& lt; endpoint 地址=皀et.tcp://localhost: 12345/User",绑定=皀etTcpBinding",合同=癶cbServiceB.IUser"比; & lt;才能identity> & lt;才能dns 价值=https://www.yisu.com/zixun/" localhost "/> >身份 端点> 服务> 服务 <绑定><绑定名称=" netTcpBindingConfig " closeTimeout=?0:30:00 openTimeout”=?0:30:00”receiveTimeout=?0:30:00 sendTimeout”=?0:30:00”transactionFlow=癴alse”transferMode=盎撼濉眛ransactionProtocol=癘leTransactions hostNameComparisonMode”=癝trongWildcard”listenBacklog=" 100 " maxBufferPoolSize=?147483647”maxBufferSize=?147483647”maxConnections=" 100 " maxReceivedMessageSize=" 2147483647 "> 绑定 <行为><安全模式=霸耸洹? <运输clientCredentialType=癢indows”protectionLevel=" EncryptAndSign "/> 安全> > 绑定 <行为name=" userBehavior "> 怎么在WCF中使用nettcp协议实现通讯