一个WCF使用TCP协议进行通协的例子
之前写的例子都是基于http协议的,但在局域网环境下,我希望可以获取更高的传输性能和更低的服务端资源占用,所以我尝试使用TCP协议来进行传输。网上的例子都讲得非常复杂,但终于让我找到一个简单的实现方法,记录如下:
一、建立WCF服务
首先建立一个WCF服务库,名称为"WCFService",解决方案名称为"WCFDemo",同时"为解决方案
创建目录"要勾上。
确定后VS会自动创建一个IService1接口和Service1程序文件,不过由于这个例子是演示如何使
用TCP协议的,所以我们就不再作任何修改。
二、建立宿主程序
在解决方案上按右键,选择"添加" -> "新建项目",然后新建一个Windows 窗体应用程序,程
序名称为"WCFHost"。
在解决方案资源管理器中,需要添加两个引用: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> <endpoint address= "net.tcp://localhost:8000/service" binding= "netTcpBinding" bindingConfiguration= "TcpBinding" contract= "ServiceReference1.IService1" name= "TcpBinding" > <identity> <userPrincipalName value= "OverBlue-PC\OverBlue" /> </identity> </endpoint> <endpoint address= "http://localhost:8001/service" binding= "wsDualHttpBinding" 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这么简单就完成了。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?