posts - 85,comments - 18,views - 10万

 

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

  



posted on   JamelAr  阅读(2108)  评论(4编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

打赏

>>

欢迎打赏支持我 ^_^

扫描二维码打赏

了解更多

点击右上角即可分享
微信分享提示