zoukankan      html  css  js  c++  java
  • WCF学习——构建一个简单的WCF应用(二)

    我们接着上一篇文章进行讲解 http://www.cnblogs.com/songjianhui/p/7060698.html

    一:客户端通过添加引用调用服务

        WCF应用服务被成功寄宿后,WCF服务应用便开始了服务调用请求的监听工作。此外,服务寄宿将服务描述通过元数据的形式发布出来,相应的客户端就可以获取这些元数据。接下来我们来创建客户端程序进行服务的调用。

        1)先运行服务寄宿程序(Hosting.exe)

        2) 在Visual Studio 2013的“解决方案资源管理器”中,把Client项目展开,左键选中“引用”,点击鼠标右键,弹出菜单,在弹出的上下文菜单中选择“添加服务引用(Add Service References)”。如下图。

        

        3) 此时会弹出一个对话框,如下图所示。在对话框中的“地址”输入框中输入服务元数据发布的源地址:http://127.0.0.1:3721/calculatorService/metadata,并在“命名空间”输入框中输入一个命名空间,然后点击“确定”按钮(如下图)。Visual studio 2013会自动生成一系列用于服务调用的代码和配置。

        

      

         4)  在Visual Studio 2013自动生成的类中,包含一个服务协定接口、一个服务代理对象和其他相关的类。

          

        5) 我们可以实例化CalculatorServiceClient对象,执行相应方法调用WCF服务操作。客户端进行WCF服务调用的代码如下:

        

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace Client
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             MyWcfService.CalculatorServiceClient client = new MyWcfService.CalculatorServiceClient();
    14             Console.WriteLine("x+y={2} when  x={0} and y={1}", 1, 2, client.Add(1, 2));
    15             Console.WriteLine("x-y={2} when  x={0} and y={1}", 1, 2, client.Add(1, 2));
    16             Console.WriteLine("x*y={2} when  x={0} and y={1}", 1, 2, client.Add(1, 2));
    17             Console.WriteLine("x/y={2} when  x={0} and y={1}", 1, 2, client.Add(1, 2));
    18             Console.ReadLine();
    19         }
    20     }
    21 }

        6)结果

        

      

    二:客户端通过ChannelFactory<T>方式调用WCF服务

         1) WCF采用基于契约的服务调用方法。从上面的例子也可以看到,Visual Studio2013 在进行服务引用添加的过程中会在客户端创建一个与服务端等效的服务契          约接口。由于服务端和客户端在同一个解决方案中。因此完全可以让服务端和客户端引用相同的契约
                    
                    2) 为了演示这种场景,我们将添加的服务引用移除,并为Client项目添加Service.Interface项目的引用。在客户端程序中基于地址和绑定对象创建一个              ChannelFactory<ICalculator>,然后调用它的CreateChannel方法 创建的服务代理对象完成服务调用(这里我们就直接创建一个控制台来进行演示)

        3)创建一个控制台应用程序 引用Service.Interface和System.ServiceModel;

        

        4) 编写ChanelClient的代码

          1.通过代码的方式配置终结点

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 using Service.Interface;
     7 using System.ServiceModel;
     8 
     9 namespace ChannelClient
    10 {
    11     class Program
    12     {
    13         static void Main(string[] args)
    14         {
    15             //基于地址和绑定对象创建一个ChannelFactory<ICalculator> 通过代码
    16             using (ChannelFactory<ICalculator> channelFactory = new ChannelFactory<ICalculator>(new WSHttpBinding(), "http://127.0.0.1:3721/calculatorService"))
    17             {
    18                 //创建服务代理对象
    19                 ICalculator proxy = channelFactory.CreateChannel();
    20                 //调用计算器方法
    21                 Console.WriteLine("x+y={2} when  x={0} and y={1}",1,2,proxy.Add(1,2));
    22                 Console.WriteLine("x-y={2} when  x={0} and y={1}", 1, 2, proxy.Add(1, 2));
    23                 Console.WriteLine("x*y={2} when  x={0} and y={1}", 1, 2, proxy.Add(1, 2));
    24                 Console.WriteLine("x/y={2} when  x={0} and y={1}", 1, 2, proxy.Add(1, 2));
    25 
    26                 Console.ReadLine();
    27             }
    28         }
    29     }
    30 }

         

          

          2.通过配置文件的方式来配置终结点(在app.config中进行配置)

            

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <startup> 
            <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
        </startup>
    
      <system.serviceModel>
        <client>
          <endpoint name="calculatorService" address="http://127.0.0.1:3721/CalculatorService" binding="wsHttpBinding" contract="Service.Interface.ICalculator"/>
        </client>
      </system.serviceModel>
    </configuration>

    ChannelClient
    中的代码就要进行更改
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 using Service.Interface;
     7 using System.ServiceModel;
     8 
     9 namespace ChannelClient
    10 {
    11     class Program
    12     {
    13         static void Main(string[] args)
    14         {
    15             //基于地址和绑定对象创建一个ChannelFactory<ICalculator>  通过代码
    16             //using (ChannelFactory<ICalculator> channelFactory = new ChannelFactory<ICalculator>(new WSHttpBinding(), "http://127.0.0.1:3721/CalculatorService"))
    17             //{
    18             //    //创建服务代理对象
    19             //    ICalculator proxy = channelFactory.CreateChannel();
    20             //    //调用计算器方法
    21             //    Console.WriteLine("x+y={2} when  x={0} and y={1}",1,2,proxy.Add(1,2));
    22             //    Console.WriteLine("x-y={2} when  x={0} and y={1}", 1, 2, proxy.Add(1, 2));
    23             //    Console.WriteLine("x*y={2} when  x={0} and y={1}", 1, 2, proxy.Add(1, 2));
    24             //    Console.WriteLine("x/y={2} when  x={0} and y={1}", 1, 2, proxy.Add(1, 2));
    25 
    26             //    Console.ReadLine();
    27             //}
    28 
    29             //基于地址和绑定对象创建一个ChannelFactory<ICalculator>   通过配置文件
    30             using (ChannelFactory<ICalculator> channelFactory = new ChannelFactory<ICalculator>("calculatorService"))
    31             {
    32                 //创建服务代理对象
    33                 ICalculator proxy = channelFactory.CreateChannel();
    34                 //调用计算器方法
    35                 Console.WriteLine("x+y={2} when  x={0} and y={1}", 1, 2, proxy.Add(1, 2));
    36                 Console.WriteLine("x-y={2} when  x={0} and y={1}", 1, 2, proxy.Add(1, 2));
    37                 Console.WriteLine("x*y={2} when  x={0} and y={1}", 1, 2, proxy.Add(1, 2));
    38                 Console.WriteLine("x/y={2} when  x={0} and y={1}", 1, 2, proxy.Add(1, 2));
    39 
    40                 Console.ReadLine();
    41             }
    42         }
    43     }
    44 }

        

      5) 执行hosting.exe应用程序

      6)运行ChanelClient

        

        

     源码地址:

        https://github.com/JamelsAr/WcfServicesThird

      

    作者:JamelAr
    个性签名:独学而无友,则孤陋而寡闻。做一个灵魂有趣的人!
    如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,博主在此感谢!

    本文版权归作者和博客园共有,欢迎转载,但必须给出原文链接,并保留此段声明,否则保留追究法律责任的权利。
  • 相关阅读:
    Date类型 方法
    迭代方法和归并函数
    js快速排序方法
    reset
    水平垂直居中
    css清除浮动
    box-shadow
    display---我的第一篇博客
    centos7基础安装
    aws和ufile挂载数据盘EBS
  • 原文地址:https://www.cnblogs.com/JamelAr/p/7062109.html
Copyright © 2011-2022 走看看