WCF中使用nettcp協(xié)議進(jìn)行通訊的方法
快速閱讀
如何在wcf中用net tcp協(xié)議進(jìn)行通訊,一個(gè)打開(kāi)Wcf的公共類(lèi)。比較好好,可以記下來(lái)。 配置文件中注意配置 Service,binding,behaviors. Service中配置endpoint 指明abc ,binding中配置tcp通訊的要關(guān)參數(shù),behaivor中配置http請(qǐng)求的 地址
1.建立服務(wù)服務(wù)端
還是用上次的代碼,提供一個(gè)user類(lèi),實(shí)現(xiàn)一個(gè)方法
[ServiceContract] public interface IUser { [OperationContract] string GetUserInfo(); } [ServiceContract] public interface IUser { [OperationContract] string GetUserInfo(); }
2.ServiceHostManager公有類(lèi)
通過(guò)公有類(lèi)可以減少代碼編寫(xiě)量,可以保存下來(lái),以后用的時(shí)候 直接拿來(lái)用
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服務(wù)已經(jīng)啟動(dòng)監(jiān)聽(tīng){0}",host.Description.Endpoints[0].Address); }; host.Closed+= (sender, e) => { Console.WriteLine("wcf服務(wù)已經(jīng)啟動(dòng)關(guān)閉{0}", host.Description.Endpoints[0].Address); }; } public void Start() { Console.WriteLine("正在啟動(dòng)wcf服務(wù){(diào)0}",host.Description.Endpoints[0].Name); host.Open(); } public void Stop() { if (host != null && host.State == CommunicationState.Opened) { Console.WriteLine("正在關(guān)閉wcf服務(wù){(diào)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.配置的相關(guān)參數(shù)
配置文件中注意配置 Service,binding,behaviors. Service中配置endpoint 指明abc ,binding中配置tcp通訊的要關(guān)參數(shù),behaivor中配置http請(qǐng)求的 地址
<?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>
4.啟動(dòng)服務(wù)
控制臺(tái)中啟動(dòng)服務(wù)
static void Main(string[] args) { Console.WriteLine("初始化..."); Console.WriteLine("服務(wù)運(yùn)行期間,請(qǐng)不要關(guān)閉窗口。"); Console.Title = "wcf net tcp測(cè)試 "; var cancelTokenSouce = new CancellationTokenSource(); ServiceHostManager<User>.StartNew(cancelTokenSouce); while (true) { if (Console.ReadKey().Key == ConsoleKey.Escape) { Console.WriteLine(); cancelTokenSouce.Cancel(); break; } } }
5wcftesttoos軟件測(cè)試
軟件路徑位于,可以根據(jù)自己安裝vs的目錄去找。
D:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE
測(cè)試
參考:
WCF綁定netTcpBinding寄宿到控制臺(tái)應(yīng)用程序:http://www.dbjr.com.cn/article/165257.htm
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
代碼實(shí)現(xiàn)打印功能(asp.net+javascript)
頁(yè)面實(shí)現(xiàn)打印的效果代碼,分為服務(wù)器端和客戶(hù)端單個(gè)即可,客戶(hù)端的比較不錯(cuò),本站也是類(lèi)似的方法。2009-05-05Asp.net使用SignalR實(shí)現(xiàn)聊天室的功能
這篇文章主要介紹了Asp.net使用SignalR實(shí)現(xiàn)聊天室的功能的相關(guān)資料,需要的朋友可以參考下2016-04-04DataSet、DataTable、DataRow區(qū)別詳解
這篇文章主要介紹了DataSet、DataTable、DataRow區(qū)別詳解,需要的朋友可以參考下2014-02-02Asp.Net MVC4通過(guò)id更新表單內(nèi)容的思路詳解
一個(gè)表單一旦創(chuàng)建完,其中大部分的字段便不可再編輯。只能編輯其中部分字段。下面通過(guò)本文給大家分享Asp.Net MVC4通過(guò)id更新表單內(nèi)容的思路詳解,需要的朋友參考下吧2017-07-07asp.net下實(shí)現(xiàn)輸入數(shù)字的冒泡排序
.net下實(shí)現(xiàn)輸入數(shù)字的冒泡排序2010-03-03詳解ASP.NET MVC 常用擴(kuò)展點(diǎn):過(guò)濾器、模型綁定
本篇文章主要介紹了詳解ASP.NET MVC 常用擴(kuò)展點(diǎn):過(guò)濾器、模型綁定,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-05-05asp.net 票據(jù)簡(jiǎn)單應(yīng)用
asp.net票據(jù)應(yīng)用實(shí)例代碼。2009-03-03asp.net Javascript獲取CheckBoxList的value
最近在做一個(gè)BS的小項(xiàng)目,記得自己搞asp.net的時(shí)候,還是兩年以前,大部分的東西只是有點(diǎn)印象,忘得差不多了,所以這次也算是溫習(xí)的過(guò)程吧,一邊學(xué)習(xí),一邊趕工,呵呵呵。。。。2009-12-12asp.net MVC利用ActionFilterAttribute過(guò)濾關(guān)鍵字的方法
這篇文章主要介紹了asp.net MVC利用ActionFilterAttribute過(guò)濾關(guān)鍵字的方法,結(jié)合實(shí)例形式分析了ActionFilterAttribute過(guò)濾關(guān)鍵字的原理與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-03-03