WCF中使用nettcp协议进行通讯的方法
 更新时间:2019年07月12日 14:24:01   作者:hcb  

这篇文章主要给大家介绍了关于WCF中使用nettcp协议进行通讯的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用WCF具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧

快速阅读

如何在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=”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.cnhackhy.com/article/165257.htm
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对华域联盟的支持。

您可能感兴趣的文章:利用WCF双工模式实现即时通讯

wcf
nettcp
协议

相关文章
详解Func<T>与Action<T>区别本文主要介绍Func<T>和Action<T>的用法以及他们之间的区别,希望对大家有所帮助 2016-11-11
ASP.NET设计网络硬盘之文件夹实现ASP.NET设计网络硬盘之文件夹实现… 2006-09-09
ASP.NET组件System.Web.Optimization原理及缓存问题详解这篇文章主要介绍了ASP.NET组件System.Web.Optimization的运行原理,及基本的缓存问题,感兴趣的小伙伴们可以参考一下 2016-05-05
.NET Visual Studio 代码性能分析工具大家都知道性能优化对程序员至关重要,一个小问题可能导致程序瘫痪,这里我就给大家介绍如何使用工具帮助程序员进行代码性能优化,需要的朋友可以参考下 2015-07-07
一个可以让.net程序在非WIN平台上运行的软件Mono一个可以让.net程序在非WIN平台上运行的软件Mono… 2007-03-03
Razor TagHelper实现Markdown转HTML的方法下面小编就为大家分享一篇Razor TagHelper实现Markdown转HTML的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧 2017-12-12
VS2012实现简单登录界面这篇文章主要为大家详细介绍了VS2012实现简单登录界面,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 2018-06-06
基于Jexus-5.6.3使用详解下面小编就为大家分享一篇基于Jexus-5.6.3使用详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧 2018-02-02
解决uploadify使用时session发生丢失问题的方法这篇文章主要为大家详细介绍了uploadify使用时发现session发生丢失问题的解决方法,遇到过类似问题的朋友可以参考本文进行解决 2016-05-05
asp.net 数字签名实例代码数字签名具体的好处就不说了,看了很多文章,网络上各种图,理解起来可能比较麻烦,在这里简单说一下原理,减少大家的误区,纯属个人理解,欢迎纠正 2012-06-06

最新评论

声明:本站(华域联盟www.cnhackhy.com)所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。