WCF(一) ---- 简单调用
通过配置文件和webServer引用实现客户端调用服务端方法
1.创建服务端,项目名称:SimpleWCFServer,创建服务类:CSimpleServer,新增配置文件App.config工程如下图:
配置文件App.config代码:
1 <?xml version="1.0" encoding="utf-8" ?> 2 <configuration> 3 <system.serviceModel> 4 <bindings> 5 <netTcpBinding> 6 <binding name="netTcpBindConfig" closeTimeout="00:30:00" 7 openTimeout="00:30:00" receiveTimeout="00:30:00" sendTimeout="00:30:00" 8 transactionFlow="false" transferMode="Buffered" transactionProtocol="OleTransactions" 9 hostNameComparisonMode="StrongWildcard" listenBacklog="10" 10 maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxConnections="10" 11 maxReceivedMessageSize="2147483647"> 12 <readerQuotas maxDepth="2147483647" 13 maxStringContentLength="2147483647" 14 maxArrayLength="2147483647" 15 maxBytesPerRead="2147483647" 16 maxNameTableCharCount="2147483647" /> 17 <reliableSession ordered="true" inactivityTimeout="00:01:00" enabled="false" /> 18 <security mode="None"> 19 <transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" /> 20 <message clientCredentialType="Windows" /> 21 </security> 22 </binding> 23 </netTcpBinding> 24 </bindings> 25 <services> 26 <service behaviorConfiguration="MyBehavior" name="SimpleWCFServer.CSimpleServer"> 27 <host> 28 <baseAddresses> 29 <add baseAddress="net.tcp://localhost:6001/CSimpleServer"/> 30 </baseAddresses> 31 </host> 32 <endpoint address="" binding="netTcpBinding" contract="SimpleWCFServer.ICSimpleServer" bindingConfiguration="netTcpBindConfig"></endpoint> 33 <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" ></endpoint> 34 </service> 35 </services> 36 <behaviors> 37 <serviceBehaviors> 38 <behavior name="MyBehavior" > 39 <serviceMetadata/> 40 <serviceDebug includeExceptionDetailInFaults="true" /> 41 <dataContractSerializer maxItemsInObjectGraph="6553600"/> 42 </behavior> 43 </serviceBehaviors> 44 </behaviors> 45 </system.serviceModel> 46 </configuration>
注意节点:
service节点中的name属性对应着服务项目的命名空间和类名;
add节点中的baseAddress属性表示服务提供访问的服务地址(端口号不能是已被占用的);
endpoint节点中的contract属性中的内容表示定义终结点契约对象,可为接口也可为类,在此用来对象;
服务端代码展现:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.ServiceModel; 6 7 namespace SimpleWCFServer 8 { 9 [ServiceContract] 10 public interface ICSimpleServer 11 { 12 [OperationContract] 13 string ReturnValue(string value); 14 } 15 public class CSimpleServer : ICSimpleServer 16 { 17 #region ICSimpleServer 成员 18 19 public string ReturnValue(string value) 20 { 21 return "Hello," + value; 22 } 23 24 #endregion 25 } 26 27 }
服务端采用了控制台项目,启动服务代码如下:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.ServiceModel; 6 7 namespace SimpleWCFServer 8 { 9 [ServiceContract] 10 public interface ICSimpleServer 11 { 12 [OperationContract] 13 string ReturnValue(string value); 14 } 15 public class CSimpleServer : ICSimpleServer 16 { 17 #region ICSimpleServer 成员 18 19 public string ReturnValue(string value) 20 { 21 return "Hello," + value; 22 } 23 24 #endregion 25 } 26 27 }
2.创建客户端,项目名称:SimpleWCFClient
添加服务引用,在弹出的文本框中输入服务端地址:net.tcp://localhost:6001/CSimpleServer,该地址在服务端配置文件中输入,在服务启动时生成;点击"前往"按钮,当服务端处于启动状态,可搜索到服务对象:CSimpleServer,修改命名空间为:Pro_CSimpleServer,如图所示:
引用成功后我们可以发现客户端多了一个服务引用对象和一个配置文件app.config,如下图所示:
打开app.config文件:
1 <?xml version="1.0" encoding="utf-8" ?> 2 <configuration> 3 <system.serviceModel> 4 <bindings> 5 <netTcpBinding> 6 <binding name="NetTcpBinding_ICSimpleServer" closeTimeout="00:01:00" 7 openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" 8 transactionFlow="false" transferMode="Buffered" transactionProtocol="OleTransactions" 9 hostNameComparisonMode="StrongWildcard" listenBacklog="10" 10 maxBufferPoolSize="524288" maxBufferSize="65536" maxConnections="10" 11 maxReceivedMessageSize="65536"> 12 <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" 13 maxBytesPerRead="4096" maxNameTableCharCount="16384" /> 14 <reliableSession ordered="true" inactivityTimeout="00:10:00" 15 enabled="false" /> 16 <security mode="None"> 17 <transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" /> 18 <message clientCredentialType="Windows" /> 19 </security> 20 </binding> 21 </netTcpBinding> 22 </bindings> 23 <client> 24 <endpoint address="net.tcp://localhost:6001/CSimpleServer" binding="netTcpBinding" 25 bindingConfiguration="NetTcpBinding_ICSimpleServer" contract="Pro_CSimpleServer.ICSimpleServer" 26 name="NetTcpBinding_ICSimpleServer" /> 27 </client> 28 </system.serviceModel> 29 </configuration>
客户端client节点下的endpoint,终结点引用服务的地址
服务代理对象包含对象如下:
客户端调用代码:
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 10 namespace SimpleWCFClient 11 { 12 public partial class Form1 : Form 13 { 14 public Form1() 15 { 16 InitializeComponent(); 17 } 18 19 private void button1_Click(object sender, EventArgs e) 20 { 21 Pro_CSimpleServer.CSimpleServerClient client = new Pro_CSimpleServer.CSimpleServerClient(); 22 textBox1.Text = client.ReturnValue("Simple_Client"); 23 } 24 } 25 }
结果显示:
浙公网安备 33010602011771号