ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

WCF学习——构建一个简单的WCF应用(二)

2022-04-04 18:04:11  阅读:181  来源: 互联网

标签:Console when System 学习 Add 构建 WriteLine WCF using


引用网址:https://www.cnblogs.com/JamelAr/p/7062109.html

我们接着上一篇文章进行讲解 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/WcfServicesFirst

 

    https://github.com/JamelsAr/WcfServicesSecond

 

    https://github.com/JamelsAr/WcfServicesThird

  

 

标签:Console,when,System,学习,Add,构建,WriteLine,WCF,using
来源: https://www.cnblogs.com/bruce1992/p/16099866.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有