如何在wcf中用net tcp協議進行通信,一個打開Wcf的公共類。比較好好,能夠記下來。 配置文件中注意配置 Service,binding,behaviors. Service中配置endpoint 指明abc ,binding中配置tcp通信的要關參數,behaivor中配置http請求的 地址html
仍是用上次的代碼,提供一個user類,實現一個方法c#
[ServiceContract] public interface IUser { [OperationContract] string GetUserInfo(); } [ServiceContract] public interface IUser { [OperationContract] string GetUserInfo(); }
經過公有類能夠減小代碼編寫量,能夠保存下來,之後用的時候 直接拿來用tcp
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; } }
配置文件中注意配置 Service,binding,behaviors. Service中配置endpoint 指明abc ,binding中配置tcp通信的要關參數,behaivor中配置http請求的 地址ide
<?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> </services> <bindings> <netTcpBinding> <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="2147483647" maxBufferSize="2147483647" maxConnections="100" maxReceivedMessageSize="2147483647"> <readerQuotas maxDepth="64" maxStringContentLength="2147483647" maxArrayLength="2147483647 " 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="http://localhost:8081/User" /> <serviceDebug includeExceptionDetailInFaults="True" /> <serviceThrottling maxConcurrentCalls="1000" maxConcurrentInstances="1000" maxConcurrentSessions="1000" /> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel> </configuration>
控制檯中啓動服務測試
static void Main(string[] args) { Console.WriteLine("初始化..."); Console.WriteLine("服務運行期間,請不要關閉窗口。"); Console.Title = "wcf net tcp測試 "; var cancelTokenSouce = new CancellationTokenSource(); ServiceHostManager<User>.StartNew(cancelTokenSouce); while (true) { if (Console.ReadKey().Key == ConsoleKey.Escape) { Console.WriteLine(); cancelTokenSouce.Cancel(); break; } } }
軟件路徑位於,能夠根據本身安裝vs的目錄去找。 D:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE
WCF綁定netTcpBinding寄宿到控制檯應用程序:http://www.javashuo.com/article/p-tbvasazy-kg.htmlcode
我對個人文章負責,發現好多網上的文章 沒有實踐,都發出來的,讓人走不少彎路,若是你在個人文章中遇到沒法實現,或者沒法走通的問題。能夠直接在公衆號《愛碼農愛生活 》留言。一定會再次複查緣由。讓每一篇 文章的流程都能順利實現。xml