一个WCF使用TCP协议进行通协的例子
2024-09-27 16:28:22
在解决方案资源管理器中,需要添加两个引用:System.ServiceModel和WCFService。然后双击窗口,在Form_Load事件中编写如下代码:
添加一个应用程序配置文件App.Config,然后粘贴如下内容:
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
|
<?xml version= "1.0" encoding= "utf-8" ?> <configuration> <system.serviceModel> <bindings> <netTcpBinding> <binding name= "TcpBinding" closeTimeout= "00:01:00" openTimeout= "00:01:00" receiveTimeout= "00:10:00" sendTimeout= "00:01:00" transactionFlow= "false" transferMode= "Buffered" transactionProtocol= "OleTransactions" hostNameComparisonMode= "StrongWildcard" listenBacklog= "10" maxBufferPoolSize= "524288" maxBufferSize= "65536" maxConnections= "10" maxReceivedMessageSize= "65536" > <readerQuotas maxDepth= "32" maxStringContentLength= "8192" maxArrayLength= "16384" maxBytesPerRead= "4096" maxNameTableCharCount= "16384" /> <reliableSession ordered= "true" inactivityTimeout= "00:10:00" enabled= "false" /> <security mode= "Transport" > <transport clientCredentialType= "Windows" protectionLevel= "EncryptAndSign" /> <message clientCredentialType= "Windows" /> </binding> </netTcpBinding> <wsDualHttpBinding> <binding name= "HttpBinding" closeTimeout= "00:01:00" openTimeout= "00:01:00" receiveTimeout= "00:10:00" sendTimeout= "00:01:00" bypassProxyOnLocal= "false" transactionFlow= "false" hostNameComparisonMode= "StrongWildcard" maxBufferPoolSize= "524288" maxReceivedMessageSize= "65536" messageEncoding= "Text" textEncoding= "utf-8" useDefaultWebProxy= "true" > <readerQuotas maxDepth= "32" maxStringContentLength= "8192" maxArrayLength= "16384" maxBytesPerRead= "4096" maxNameTableCharCount= "16384" /> <reliableSession ordered= "true" inactivityTimeout= "00:10:00" /> <security mode= "Message" > <message clientCredentialType= "Windows" negotiateServiceCredential= "true" algorithmSuite= "Default" /> </security> </binding> </wsDualHttpBinding> </bindings> <client> bindingConfiguration= "TcpBinding" contract= "ServiceReference1.IService1" name= "TcpBinding" > <identity> <userPrincipalName value= "OverBlue-PC\OverBlue" /> </identity> </endpoint> bindingConfiguration= "HttpBinding" contract= "ServiceReference1.IService1" name= "HttpBinding" > <identity> <userPrincipalName value= "OverBlue-PC\OverBlue" /> </identity> </endpoint> </client> </system.serviceModel> </configuration> |
对于这个配置文件,我是这样理解的:
1、baseAddress:由协议、地址和端口三个部分组成。其中net.tcp对应TCP协议,http对应Http协 议。
2、endPoint:该属性有一个address属性,指的是在baseAddress基础上增加address属性等于一个 完整的路径。contract则是对应程序接口,这个就不多说。而每种协议都是对应WCFService.IService1契约。
到现在,WCF宿主程序就已经建立好了,我们编译并在"非VS环境下"运行WCFHost应用程序。
三、建立客户端应用程序
在解决方案上按右键,选择"添加" -> "新建项目",然后新建一个Windows 窗体应用程序,程序名称为"WCFClient"。0004
然后在项目上按右键,选择"添加服务引用",在弹出的添加服务引用中,输入baseAddRess地址 :http://localhost:8001,然后点击“前往”,当确定没问题后,点击“确定”按钮。
在客户端程序中,会自动产生一个app.config文件,双击打开该文件,我们可以在"client"段 中可以看到,net.tcp和Http两种协议属性下面都有一个"name"属性。通过这个"name"属性,我们可 以控制使用什么协议与访问服务端。
我们现在为程序添加一个按钮,双击后编写如下代码:
1
2
3
4
5
6
7
|
private void button1_Click( object sender, EventArgs e) { WCFClient.ServiceReference1.Service1Client sc = new ServiceReference1.Service1Client( "TcpBinding" ); sc.Open(); MessageBox.Show(sc.GetData(10)); sc.Close(); } |
就个Demo这么简单就完成了。
最新文章
- 10 件有关 JavaScript 让人费解的事情
- php中echo(),print(),print_r()之间的区别
- PHP是什么
- Gocd持续部署利器
- (letcode)String to Integer (atoi)
- codevs 3186 队列练习2
- Apache OFBiz 学习笔记 之 服务引擎 一
- MongoDB 聚合
- SmartSql漫谈
- SeleniumIDE_初识
- 每日算法---Two Sum
- vscode调试C/C++时支持查看定义查看引用
- nodejs+mysql入门实例(链接到数据库)
- jQuery轮播图(手动点击轮播)
- 2017-2018-1 JAVA实验站 第八周作业
- 一款基于TweenMax.js的网页幻灯片
- 《DSP using MATLAB》示例 Example 9.10
- 【BZOJ】1682: [Usaco2005 Mar]Out of Hay 干草危机(kruskal)
- orcale 之函数
- MyBatis之Mapper XML 文件详解(三)-Result Maps
热门文章
- 2.3UML建模之用例图关系
- Linux文件系统十问---深入理解文件存储方式(rhel6.5,EXT4)【转】
- 研究Mac OS中Migration Assistant工具
- 根据mysql中字段为时间戳查询某天数据
- hdu 5663 Hillan and the girl 莫比乌斯反演
- 做一个完整的Java Web项目需要掌握的技能
- nova libvirt event
- 执行安装redis报错undefined reference to `__sync_add_and_fetch_4&#39;
- jquery jsonp模版
- 被人遗忘的MAX_FILE_SIZE文件上传限制大小参数