WCF(一)控制台寄宿

WCF是微软开发的一款通信框架。具有跨平台跨操作系统的特点,所以,WCF一般用于开发第三方接口或者在分布式系统用做数据交互。

WCF三要素分别是地址(Address)、绑定(Binding)、契约(Contract)。

地址:服务端与客户端通信的uri。

绑定:描述了服务端与客户端交互数据的协议,服务端和客户端必须保持一致才能交互数据。

契约:系统间进行交互的数据/消息结构、格式。

做个Demo演示一下。

项目结构:

 

1、定义契约(服务契约、数据契约)

 1 namespace WCFService  //服务契约
 2 {
 3     [ServiceContract]
 4     public interface ICalculate
 5     {
 6         [OperationContract]
 7         int Add(int a, int b);
 8         [OperationContract]
 9         int Subtract();
10     }
11 }
 1 namespace WCFModel  //数据契约
 2 {
 3     [DataContract]
 4     public class OperationNumber
 5     {
 6         [DataMember]
 7         public int Num1 { get; set; }
 8         [DataMember]
 9         public int Num2 { get; set; }
10     }
11 }

2、实现契约,定义服务

 1 namespace WCFService
 2 {
 3     public class Calculate : ICalculate
 4     {
 5         public int Add(int a, int b)
 6         {
 7             return a + b;
 8         }
 9 
10         OperationNumber oper = new OperationNumber() { Num1 = 6, Num2 = 4 };
11         public int Subtract()
12         {
13             int result = oper.Num1 - oper.Num2;
14             return result;
15         }
16     }
17 }

3、服务端开发,把WCF寄宿到控制台应用程序中

3.1、创建控制台应用程序

 1 namespace WCFServer
 2 {
 3     class Program
 4     {
 5         static void Main(string[] args)
 6         {
 7             using (ServiceHost host = new ServiceHost(typeof(WCFService.Calculate)))
 8             {
 9                 host.Open();
10                 Console.WriteLine("服务开启了");
11                 Console.ReadKey();
12             }
13         }
14     }

3.2 配置服务端App.config文件

 1 <?xml version="1.0" encoding="utf-8" ?>
 2 <configuration>
 3   <startup>
 4     <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
 5   </startup>
 6   <system.serviceModel>
 7     <services>
 8       <service name="WCFService.Calculate" behaviorConfiguration="metadataBehavior" >
 9         <host>
10           <baseAddresses>
11             <add baseAddress="http://127.0.0.1:9999/Calculate"/>
12           </baseAddresses>
13         </host>
14         <endpoint address="" binding="wsHttpBinding" contract="WCFService.ICalculate"></endpoint>
15         <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
16       </service>
17     </services>
18     <behaviors>
19       <serviceBehaviors>
20         <behavior name="metadataBehavior">
21           <serviceMetadata httpGetEnabled="true" />
22         </behavior>
23       </serviceBehaviors>
24     </behaviors>
25   </system.serviceModel>
26 </configuration>

3.3、编译生成下服务端,让服务端处于运行状态。在浏览器中输入“http://127.0.0.1:9999/Calculate”,看到类似下面的图片,表示服务端开发完成。

 

4、实现客户端调用WCF,完成数据交互

4.1创建客户端控制台应用程序,客户端在应用程序的"引用"节点右键,选择"添加引用服务",在弹出来的对话框中的地址栏写入服务端的地址,选择“发现”即可看到客户顿获得服务端定义的方法。

namespace WCFClient
{
    class Program
    {
        static void Main(string[] args)
        {
            using (ChannelFactory<ICalculate> channelFactory = new ChannelFactory<ICalculate>("WSHttpBinding_ICalculate"))
            {
                ICalculate proxy = channelFactory.CreateChannel();
                using(proxy as IDisposable)
                {
                    Console.WriteLine(proxy.Add(2,4));
                    Console.WriteLine(proxy.Subtract());
                }
            }
            Console.ReadKey();
        }
    }

4.2配置客户端App.config文件

 1 <?xml version="1.0" encoding="utf-8" ?>
 2 <configuration>
 3     <startup> 
 4         <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
 5     </startup>
 6     <system.serviceModel>
 7         <bindings>
 8             <wsHttpBinding>
 9                 <binding name="WSHttpBinding_ICalculate" />
10             </wsHttpBinding>
11         </bindings>
12         <client>
13             <endpoint address="http://127.0.0.1:9999/Calculate" binding="wsHttpBinding"
14                 bindingConfiguration="WSHttpBinding_ICalculate" contract="WCFReference.ICalculate"
15                 name="WSHttpBinding_ICalculate">
16                 <identity>
17                     <userPrincipalName value="SUN-PCAdministrator" />
18                 </identity>
19             </endpoint>
20         </client>
21     </system.serviceModel>
22 </configuration>

 5、生成下客户端,在服务端程序运行的情况下,运行客户端,可以看到客户端运行结果。

 备注:

  添加契约需要引用"System.ServiceModel"命名空间。

  数据契约除了引用上面的命名空间外,还需要引用“System.Runtime.Serialization”命名空间。

原文地址:https://www.cnblogs.com/sunice/p/6586291.html