在解决方案资源管理器中,需要添加两个引用: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-PCOverBlue" /> </identity> </endpoint> bindingConfiguration= "HttpBinding" contract= "ServiceReference1.IService1" name= "HttpBinding" > <identity> <userPrincipalName value= "OverBlue-PCOverBlue" /> </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这么简单就完成了。