怎么在WCF中使用nettcp協(xié)議實(shí)現(xiàn)通訊-創(chuàng)新互聯(lián)

怎么在WCF中使用nettcp協(xié)議實(shí)現(xiàn)通訊?針對(duì)這個(gè)問題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡單易行的方法。

創(chuàng)新互聯(lián)成立與2013年,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目網(wǎng)站設(shè)計(jì)制作、成都做網(wǎng)站網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元呼中做網(wǎng)站,已為上家服務(wù),為呼中各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:18982081108

1.建立服務(wù)服務(wù)端


還是用上次的代碼,提供一個(gè)user類,實(shí)現(xiàn)一個(gè)方法

[ServiceContract]
 public interface IUser
 {
 [OperationContract]
 string GetUserInfo();
 }
[ServiceContract]
 public interface IUser
 {
 [OperationContract]
 string GetUserInfo();
 }

2.ServiceHostManager公有類


通過公有類可以減少代碼編寫量,可以保存下來,以后用的時(shí)候 直接拿來用

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)聽{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中使用nettcp協(xié)議實(shí)現(xiàn)通訊


關(guān)于怎么在WCF中使用nettcp協(xié)議實(shí)現(xiàn)通訊問題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識(shí)。

本文題目:怎么在WCF中使用nettcp協(xié)議實(shí)現(xiàn)通訊-創(chuàng)新互聯(lián)
標(biāo)題鏈接:http://muchs.cn/article26/cdsgjg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站、移動(dòng)網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)公司域名注冊(cè)、建站公司、網(wǎng)站導(dǎo)航

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站優(yōu)化排名