快速阅读
如何在wcf中用net tcp协议进行通讯,一个打开Wcf的公共类。比较好好,可以记下来。 配置文件中注意配置 Service,binding,behaviors. Service中配置endpoint 指明abc ,binding中配置tcp通讯的要关参数,behaivor中配置http请求的 地址
1.建立服务服务端
还是用上次的代码,提供一个user类,实现一个方法
?
1
2
3
4
5
6
7
8
9
10
11
12
|
[ServiceContract]
public interface IUser
{
[OperationContract]
string GetUserInfo();
}
[ServiceContract]
public interface IUser
{
[OperationContract]
string GetUserInfo();
}
|
2.ServiceHostManager公有类
通过公有类可以减少代码编写量,可以保存下来,以后用的时候 直接拿来用
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
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请求的 地址
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
<? 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.启动服务
控制台中启动服务
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
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寄宿到控制台应用程序:http://www.zzvips.com/article/76680.html
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对快网idc的支持。
原文链接:http://www.hechunbo.com/index.php/archives/162.html
相关文章
猜你喜欢
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
- 64M VPS建站:能否支持高流量网站运行? 2025-06-10
- 64M VPS建站:怎样选择合适的域名和SSL证书? 2025-06-10
- 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-25 64
-
2025-05-29 98
-
2025-05-29 83
-
2025-05-26 61
-
2025-05-29 42
热门评论