WCF中应用nettcp协议书开展通信的方式
摘要: 迅速阅读文章1.创建服务服务端還是用之前的编码,出示一个user类,完成一个方式[ServiceContract] public interface IUser { [OperationContract] string GetUserInfo(); }[ServiceContract] public interface IUser { [Op...
迅速阅读文章
1.创建服务服务端
還是用之前的编码,出示一个user类,完成一个方式
[ServiceContract] public interface IUser { [OperationContract] string GetUserInfo(); }[ServiceContract] public interface IUser { [OperationContract] string GetUserInfo(); }
2.ServiceHostManager公有制类
根据公有制类能够降低编码撰写量,能够储存出来,之后用的情况下 立即用来用
public interface IServiceHostmanager : IDisposable { void Start(); void Stop(); } public class ServiceHostManager TService :IServiceHostmanager where TService:class { private ServiceHost host; public void Dispose() { Stop(); } public ServiceHostManager() { host=new ServiceHost(typeof(User)); host.Opened+= (sender, e) = { Console.WriteLine("wcf服务早已起动监视{0}",host.Description.Endpoints[0].Address); }; host.Closed+= (sender, e) = { Console.WriteLine("wcf服务早已起动关掉{0}", host.Description.Endpoints[0].Address); }; } public void Start() { Console.WriteLine("已经起动wcf服务{0}",host.Description.Endpoints[0].Name); host.Open(); } public void Stop() { if (host != null && host.State == CommunicationState.Opened) { Console.WriteLine("已经关掉wcf服务{0}", host.Description.Endpoints[0].Name); host.Close(); } } public static Task StartNew(CancellationTokenSource conTokenSource) { var task = Task.Factory.StartNew(() = { IServiceHostmanager shm = null; try { shm = new ServiceHostManager TService shm.Start(); while (true) { if (conTokenSource.IsCancellationRequested && shm != null) { shm.Stop(); break; } } } catch (Exception ex) { Console.WriteLine(ex.Message); if (shm != null) shm.Stop(); } },conTokenSource.Token); return task; } }
3.配备的有关主要参数
配备文档中留意配备 Service,binding,behaviors. Service中配备endpoint 指出abc ,binding中配备tcp通信的要关主要参数,behaivor中配备http恳求的 详细地址
xml version="1.0" encoding="utf-8" configuration system.serviceModel services service name="hcbServiceB.User" behaviorConfiguration="userBehavior" endpoint address="net.tcp://localhost:12345/User" binding="netTcpBinding" contract="hcbServiceB.IUser" identity dns value="localhost"/ /identity /endpoint /service /TcpBinding binding name="netTcpBindingConfig" closeTimeout="00:30:00" openTimeout="00:30:00" receiveTimeout="00:30:00" sendTimeout="00:30:00" transactionFlow="false" transferMode="Buffered" transactionProtocol="OleTransactions" hostNameComparisonMode="StrongWildcard" listenBacklog="100" maxBufferPoolSize="" maxBufferSize="" maxConnections="100" maxReceivedMessageSize="" readerQuotas maxDepth="64" maxStringContentLength="" maxArrayLength=" " maxBytesPerRead="4096" maxNameTableCharCount="16384" / reliableSession ordered="true" inactivityTimeout="00:30:00" enabled="false" / security mode="Transport" transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" / /security /binding /netTcpBinding /bindings behaviors serviceBehaviors behavior name="userBehavior" serviceMetadata httpGetEnabled="True" httpGetUrl="User" / serviceDebug includeExceptionDetailInFaults="True" / serviceThrottling maxConcurrentCalls="1000" maxConcurrentInstances="1000" maxConcurrentSessions="1000" / /behavior /serviceBehaviors /behaviors /system.serviceModel /configuration
4.起动服务
操纵台中起动服务
static void Main(string[] args) { Console.WriteLine("原始化..."); Console.WriteLine("服务运作期内,请不必关掉对话框。"); Console.Title = " tcp检测 "; var cancelTokenSouce = new CancellationTokenSource(); ServiceHostManager User .StartNew(cancelTokenSouce); while (true) { if (Console.ReadKey().Key == ConsoleKey.Escape) { Console.WriteLine(); cancelTokenSouce.Cancel(); break; } } }
5wcftesttoos手机软件检测
手机软件相对路径坐落于,能够依据自身安裝vs的文件目录去找。
D:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE
检测
参照:
小结
之上便是本文的所有內容了,期待文中的內容对大伙儿的学习培训或是工作中具备一定的参照学习培训使用价值,感谢大伙儿对脚本制作之家的适用。
