如何在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公有类
通过公有类可以减少代码编写量,可以保存下来,以后用的时候 直接拿来用。
< ?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>
< /system.serviceModel>
< /configuration>
3.配置的相关参数
配置文件中注意配置 Service,binding,behaviors. Service中配置endpoint 指明abc ,binding中配置tcp通讯的要关参数,behaivor中配置http请求的地址。
WCF中使用nettcp协议进行通讯的方法代码块。
4.启动服务
控制台中启动服务
static void Main(string[] args)
{
Console.WriteLine("初始化...");
Console.WriteLine("服务运行期间,请不要关闭窗口。");
Console.Title = "wcf net tcp测试 ";
var cancelTokenSouce = new CancellationTokenSource();
ServiceHostManager.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
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值。