WCF全面解析第一章 WCF 简介

1.WCF中的 "A","B","C" 介绍

  我们先看个生活中的例子,某一天,公司的领导让你去送一份合同文件,送文件的过程你可以选择的交通方式为“打车”、“公交”、“地铁”,当然费用是根据发票来报销的,到了对方公司后你要找到某经理,并且要一份收到合同文件的回执和相关文件。

  要完成这项工作任务我们执行以下几个主要的步骤:

  (1)我们首先要知道对方公司的地址,引出WCF中的"A"。

  A(Address):英文理解为"地址",在计算机中是通过一个URI唯一地址标识,通过这个地址我们可以找到我们要调用的WCF服务。

  A解决了:Where to locate the WCF Service?

  (2)我们还要选择我们的交通方式,每种交通方式达到的结果不一样。如:打车费用较贵、但是过程舒服些,时间上视道路情况而定。公交最便宜,并且可选择多条线路。地铁最方便,但是偶尔会很挤,一般都没座等等,引出WCF中的"B"。

  B(Binding):英文理解为"捆绑,绑定", Binding实现在Client和Service通信的所有底层细节。如:我们在客户端与服务端传输的时候采用的是什么样的编码,XML?Text?二进制?...采用哪种传输协议进行传输,TCP?Http?以及采用什么样的机制解决安全问题,SSL?加密?...

  B解决了:How to communicate with service?

  (3)到了对方公司之后我们能做哪些事?I.送合同,II.拿回执。我们不能要求对方公司给我们其他的东西,引出WCF中的"C"。

  C(Contract):英文理解为"合同",合同是什么?告诉我们哪些事能做,如些事不能做。 Contract的主要的作用是暴露某个WCF Service所提供的所有有效的方法。Contract实际上是把每个方法的转化成为相对应的消息。

  C解决了:What functionalities do the Service provide?

2.简化WCF的配置

我们可以通过单击“工具”--“WCF服务配置编辑器”,开启WCF服务配置编辑器。

3.构建一个简单的WCF应用

WCF全面解析第一章 WCF 简介

接口ICalculator

[ServiceContract(Name = "CalculatorService", Namespace ="http://www.wzl.com/")]
    public interface ICalculator
    {
        [OperationContract]
        double Add(double x, double y);
        [OperationContract]
        double Subtract(double x, double y);
        [OperationContract]
        double Multiply(double x, double y);
        [OperationContract]
        double Divide(double x, double y);
    }

实现接口Service

public class CalculatorService : ICalculator
    {
        public double Add(double x, double y)
        {
            return x + y;
        }
        public double Subtract(double x, double y)
        {
            return x - y;
        }
        public double Multiply(double x, double y)
        {
            return x * y;
        }
        public double Divide(double x, double y)
        {
            return x / y;
        }

第一种:

Host启用服务:不需要任何配置文件

 static void Main(string[] args)
        {
            using (ServiceHost host = new ServiceHost(typeof(CalculatorService)))
            {
                host.AddServiceEndpoint(typeof(ICalculator), new WSHttpBinding(),"http://127.0.0.1:3721/calculatorservice");
                if (host.Description.Behaviors.Find<ServiceMetadataBehavior>() == null)
                {
                    ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
                    behavior.HttpGetEnabled = true;
                    behavior.HttpGetUrl = new Uri("http://127.0.0.1:3721/calculatorservice/metadata");
                    host.Description.Behaviors.Add(behavior);
                }
                host.Opened += delegate
                {
                    Console.WriteLine("CalculaorService已经启动,按任意键终止服务!");
                };

                host.Open();
                Console.Read();
            }
        }

Client调用:通过添加服务引用的方式(右键引用-添加服务引用)

 static void Main(string[] args)
        {
            using (CalculatorServiceClient proxy = new CalculatorServiceClient())
            {
                Console.WriteLine("x + y = {2} when x = {0} and y = {1}", 1, 2, proxy.Add(1, 2));
                Console.WriteLine("x - y = {2} when x = {0} and y = {1}", 1, 2, proxy.Subtract(1, 2));
                Console.WriteLine("x * y = {2} when x = {0} and y = {1}", 1, 2, proxy.Multiply(1, 2));
                Console.WriteLine("x / y = {2} when x = {0} and y = {1}", 1, 2, proxy.Divide(1, 2));
            }
            Console.Read();
        }

第二种:

Host启用服务:通过配置文件

 <system.serviceModel>
     <behaviors>
       <serviceBehaviors>
         <behavior name="metadataBehavior">
           <serviceMetadata httpGetEnabled="true"
                            httpGetUrl="http://127.0.0.1:3721/calculatorservice/metadata" />
         </behavior>
       </serviceBehaviors>
     </behaviors>
     <services>
       <service name="Artech.WcfServices.Service.CalculatorService"
                behaviorConfiguration="metadataBehavior" >
         <endpoint address="http://127.0.0.1:3721/calculatorservice"
                   binding="wsHttpBinding"
                   contract="Artech.WcfServices.Service.Interface.ICalculator" />
       </service>
     </services>
   </system.serviceModel>

启用

  static void Main(string[] args)
         {
             using (ServiceHost host = new ServiceHost(typeof(CalculatorService)))
             {
                 host.Opened += delegate
                 {
                     Console.WriteLine("CalculaorService已经启动,按任意键终止服务!");
                 };

                 host.Open();
                 Console.Read();
             }
         }

Client调用:

  static void Main(string[] args)
         {
             using (ChannelFactory<ICalculator> channelFactory = new ChannelFactory<ICalculator>(new WSHttpBinding(), "http://127.0.0.1:3721/calculatorservice"))
             {
                 ICalculator proxy = channelFactory.CreateChannel();
                 Console.WriteLine(, , proxy.Add(, ));
                 Console.WriteLine(, , proxy.Subtract(, ));
                 Console.WriteLine(, , proxy.Multiply(, ));
                 Console.WriteLine(, , proxy.Divide(, ));
             }
             Console.Read();
         }

第三种:

Host启用服务:通过配置文件

   <system.serviceModel>
     <behaviors>
       <serviceBehaviors>
         <behavior name="metadataBehavior">
           <serviceMetadata httpGetEnabled="true"
                            httpGetUrl="http://127.0.0.1:3721/calculatorservice/metadata" />
         </behavior>
       </serviceBehaviors>
     </behaviors>
     <services>
       <service name="Artech.WcfServices.Service.CalculatorService"
                behaviorConfiguration="metadataBehavior" >
         <endpoint address="http://127.0.0.1:3721/calculatorservice"
                   binding="wsHttpBinding"
                   contract="Artech.WcfServices.Service.Interface.ICalculator" />
       </service>
     </services>
   </system.serviceModel>

启用

 static void Main(string[] args)
         {
             using (ServiceHost host = new ServiceHost(typeof(CalculatorService)))
             {
                 host.Opened += delegate
                 {
                     Console.WriteLine("CalculaorService已经启动,按任意键终止服务!");
                 };

                 host.Open();
                 Console.Read();
             }
         }

Client调用:通过配置文件

   <system.serviceModel>
     <client>
       <endpoint name="calculatorservice"
                 address="http://127.0.0.1:3721/calculatorservice"
                 binding="wsHttpBinding"
                 contract="Artech.WcfServices.Service.Interface.ICalculator"  />
     </client>
   </system.serviceModel>

调用

 static void Main(string[] args)
         {
             using (ChannelFactory<ICalculator> channelFactory = new ChannelFactory<ICalculator>("calculatorservice"))
             {
                 ICalculator proxy = channelFactory.CreateChannel();
                 Console.WriteLine(, , proxy.Add(, ));
                 Console.WriteLine(, , proxy.Subtract(, ));
                 Console.WriteLine(, , proxy.Multiply(, ));
                 Console.WriteLine(, , proxy.Divide(, ));
             }
             Console.Read();
         }

第四种:通过发布部署到IIS的方式

上一篇:WCF学习之旅—WCF服务部署到应用程序(十)


下一篇:jquery json ajax -2