快速阅读

如何在wcf中用net tcp协议进行通讯,一个打开wcf的公共类。比较好好,可以记下来。 配置文件中注意配置 service,binding,behaviors. service中配置endpoint 指明abc ,binding中配置tcp通讯的要关参数,behaivor中配置http请求的 地址

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>
 </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.启动服务

控制台中启动服务

 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;
  }
 }
 }

5wcftesttoos软件测试

软件路径位于,可以根据自己安装vs的目录去找。
d:\program files (x86)\microsoft visual studio\2017\professional\common7\ide

测试

参考:

wcf绑定nettcpbinding寄宿到控制台应用程序:

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对www.887551.com的支持。