快速阅读

如何在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寄宿到控制台应用程序:https://www.cnblogs.com/felixnet/p/7397139.html

友情提示

​ 我对我的文章负责,发现好多网上的文章 没有实践,都发出来的,让人走很多弯路,如果你在我的文章中遇到无法实现,或者无法走通的问题。可以直接在公众号《爱码农爱生活 》留言。必定会再次复查原因。让每一篇 文章的流程都能顺利实现。

如何在wcf中用net tcp协议进行通讯的更多相关文章

  1. WCF 采用net.tcp协议

    WCF 采用net.tcp协议实践   概述 与Socket相比,WCF真是爽得不得了,其基本指导思想为SOA——面向服务. 其基本配置在于ABC(Address,Binding,Contract), ...

  2. WCF 采用net.tcp协议实践

    概述 与Socket相比,WCF真是爽得不得了,其基本指导思想为SOA——面向服务. 其基本配置在于ABC(Address,Binding,Contract),通常,只要这三个因素配置对了,那么,基本 ...

  3. WCF 采用net.tcp协议实践(转)

    概述 与Socket相比,WCF真是爽得不得了,其基本指导思想为SOA——面向服务. 其基本配置在于ABC(Address,Binding,Contract),通常,只要这三个因素配置对了,那么,基本 ...

  4. 如何在WCF中用TcpTrace工具查看发送和接收的SOAP消息

    WCF对消息加密(只对消息加密,不考虑Authorize)其实很简单,只要在server和client端的binding加入security mode为Message(还有Transport, Tra ...

  5. 基于TCP协议的网络通讯流程

    不多说了,先上个图: 从上面的图中可以看出来,基于TCP协议进行通讯可以大致分成以下几个阶段: 1. 首先是在服务器端, TCP Sever调用socket(), bind(), listen()完成 ...

  6. WCF:如何将net.tcp协议寄宿到IIS

    1 部署IIS 1.1 安装WAS IIS原本是不支持非HTTP协议的服务,为了让IIS支持net.tcp,必须先安装WAS(Windows Process Activation Service),即 ...

  7. [转]WCF:如何将net.tcp协议寄宿到IIS

    本文转自:http://www.cnblogs.com/Gyoung/archive/2012/12/11/2812555.html 1 部署IIS 1.1 安装WAS IIS原本是不支持非HTTP协 ...

  8. WCF开发中将net.tcp协议寄宿到IIS的方法

    1 部署IIS 1.1 安装WAS IIS原本是不支持非HTTP协议的服务,为了让IIS支持net.tcp,必须先安装WAS(Windows Process Activation Service),即 ...

  9. WCF入门教程(四)通过Host代码方式来承载服务 一个WCF使用TCP协议进行通协的例子 jquery ajax调用WCF,采用System.ServiceModel.WebHttpBinding System.ServiceModel.WSHttpBinding协议 学习WCF笔记之二 无废话WCF入门教程一[什么是WCF]

    WCF入门教程(四)通过Host代码方式来承载服务 Posted on 2014-05-15 13:03 停留的风 阅读(7681) 评论(0) 编辑 收藏 WCF入门教程(四)通过Host代码方式来 ...

随机推荐

  1. csrf 功能 及 csrf装饰器使用

    目录 csrf 功能 及 csrf装饰器使用 简单了解csrf 防范措施 了解更多csrf点击 django 中 csrf csrf装饰器 csrf功能(执行流程) csrf 功能 及 csrf装饰器 ...

  2. XML 约束

    XML约束 一.约束 约束:规定 xml 文档的书写规则 要求: 1.能够在 xml 中引入约束文档 2.能够简单的读懂约束文档 分类: 1.DTD:一种简单的约束技术(后缀.dtd) 2.Schem ...

  3. 如何使用adb工具在电脑上使用程序的方式操控自己的android手机

    在电脑安装adb工具: sudo apt install android-tools-adb android-tools-fastboot# 检查是否成功adb version 开启adb服务 sud ...

  4. EasyUI DataGrid 根据ID选中行(转载)

      转载来源: https://blog.csdn.net/chq00788/article/details/51505519 function selectRows(){ //获取需要选中的记录ID ...

  5. centos 7.6 修改vim配色方案

    cd ~ vim .vimrc colorscheme desert

  6. python蟒蛇绘制的代码以及目前还不知道怎么用的RGB颜色对照表

    #PythonDraw.py import turtle#引入海龟库 turtle.setup(650,350,200,200)#确定窗口大小,长650,高350,确定窗口位置,距离电脑左上角200, ...

  7. sshd安全加固

    常见的防护措施: ——用户限制,黑白名单 ——更改验证方式(密码——>密钥对) ——防火墙..... 修改 sshd 配置文件 /etc/ssh/sshd_config -port 3389 # ...

  8. springboot进行热部署项目

    百度了挺多的热部署,一种就是idea中一个插件,但是听说还需要 花钱,而且效果还是不太好. 自己按照网上的经验配置了一种属于自己的热部署,下面是详细的配置过程: 一.就是引入热部署需要的依赖: < ...

  9. Django之路——1 Django的简介

    今天我们来学习django,在学习Django之前我们先来了解一下django和web开发中的http协议 1.mvc模型和mtv模型 既然学习Django,那么我们一定要知道web开发中的mvc模型 ...

  10. maven 在执行package,install,deploy时使用clean与不使用clean的区别

    有时候用mvn install后,新改的内容不生效,一定要后来使用mvn clean install 才生效,由于之前没有做记录,以及记不清是什么情况下才会出现的问题,于是想看看clean和不clea ...