65.9K
CodeProject 正在变化。 阅读更多。
Home

生成 WCF 服务代理的三种方法

starIconstarIconstarIconemptyStarIconemptyStarIcon

3.00/5 (5投票s)

2014 年 6 月 16 日

CPOL

3分钟阅读

viewsIcon

128313

在 Windows Communication Foundation 中,客户端应用程序要与 WCF 服务进行通信,可以使用以下选项: 使用 ChannelFactory 生成代理 我已经在之前的 WCF 教程中讨论了 ChannelFactory 和代理之间的区别。 在本文中,

在 Windows Communication Foundation 中,客户端应用程序要与 WCF 服务进行通信,可以使用以下选项

  • 使用 ChannelFactory
  • 生成代理

我已经在之前的 WCF 面试问题 – 第 2 部分 中讨论了 ChannelFactory 和代理之间的区别。 在本文中,我们将只关注在 Windows Communication Foundation 中生成代理的多种方法。

“Windows Communication Foundation 中的代理是一个类,它使客户端应用程序能够通过发送和接收消息与服务进行通信。 它实际上封装了许多服务细节,如服务路径、服务实现技术、使用的平台、通信协议等,以及服务合约的所有方法(仅签名)。”

Windows Communication Foundation 支持以下三种为 WCF 服务生成代理的方法。

  • 添加服务引用
  • 实现 ClientBase
  • 使用工具,即 SvcUtil.exe

因此,在本 WCF 教程中,我们将执行所有三种可能的方式来为 WCF 服务生成代理。 让我们首先创建一个简单的 WCF 服务。

创建 WCF 服务

我已经在一个单独的 WCF 教程 这里 提供了新 WCF 服务的创建和托管的实现。 请按照步骤创建“StudentService”并 在控制台应用程序中托管

选项 1:通过添加服务引用生成代理

通过添加服务引用生成代理的实现也 可以在这里 找到,用于相同的 WCF 服务,即“StudentService”。

选项 2:通过实现 ClientBase<T> 类生成代理

通过使用 ClientBase<T> 类选项生成代理的优点是它在运行时创建代理,因此它将适应服务实现更改。 让我们按照步骤生成代理。

    • 向名为“ClientApp2”的解决方案添加一个客户端项目,它基本上是一个控制台应用程序。

ClientBase Proxy

    • 添加 StudentService 项目到 ClientApp2 的引用。
    • 使用 ClientBase<T> 添加以下代理类,如下所示
  public class StudentServiceProxy : ClientBase<IStudentService>, IStudentService
  {
           public string GetStudentInfo(int studentId)
           {
                 return base.Channel.GetStudentInfo(studentId);
           }
  }

注意: 不要忘记将“using StudentService”添加到类中。

    • 以下是 ClientApp2 中 program.cs 类的代码。 我们正在使用上面创建的代理类与 WCF 服务“StudentService”进行通信。
 class Program
 {
         static void Main(string[] args)
         {
                     StudentServiceProxy myclient;
                     myclient = new StudentServiceProxy();
 
                     int studentId = 1;
                     Console.WriteLine(“Calling StudentService with StudentId = 1…..”);
                     Console.WriteLine(“Student Name = {0}”, myclient.GetStudentInfo(studentId));
                     Console.ReadLine();
         }
  }

注意: 不要忘记将“using System.ServiceModel”添加到类中。

  • App.Config 文件将具有以下配置
  <system.serviceModel>
            <bindings>
                <wsHttpBinding>
                    <binding name=”WSHttpBinding_IStudentService” />
                </wsHttpBinding>
            </bindings>
            <client>
                 <endpoint address=”https://:4321/StudentService”
                                  binding=”wsHttpBinding”
                                  bindingConfiguration=”WSHttpBinding_IStudentService”
                                  contract=”StudentService.IStudentService”
                                  name=”WSHttpBinding_IStudentService”>
                 </endpoint>
             </client>
  </system.serviceModel>

现在,当我们运行客户端应用程序时,我们将收到与我们在早期选项“添加服务引用”中获得的相同输出。

WCF Proxy

选项 3:使用 SvcUtil.exe 工具生成代理

让我们通过逐步方法,使用第三个选项,即 SvcUtil.exe 工具生成代理。

  • 向名为“ClientApp3”的解决方案添加一个客户端项目,它基本上是一个控制台应用程序。

SvcUtil Tool

  • 我们的 WCF 服务必须正在运行,所以让我们运行我们的服务。

WCF Service Running

  • 打开 Visual Studio 命令提示符,并使用 svcutil.exe 工具生成代理,如下所示

svcutil https://:4321/StudentService /out:StudentServiceProxy.cs

SvcUtil Command 它将生成一个新类“StudentServiceProxy.cs”。

  • 将新创建的代理类添加到我们的客户端应用程序“ClientApp3”。
  • 使用代理类调用 WCF 服务,如下所示
  class Program
  {
          static void Main(string[] args)
          {
                     StudentServiceClient myclient;
                     myclient = new StudentServiceClient();
                     int studentId = 1;
                     Console.WriteLine(“Calling StudentService with StudentId = 1…..”);
                     Console.WriteLine(“Student Name = {0}”, myclient.GetStudentInfo(studentId));
                     Console.ReadLine();
           }
  }

当我们运行应用程序时,它将调用 StudentService 方法 getStudentInfo,并生成与其他选项中收到的相同输出。

希望本 WCF 教程将有助于理解 WCF 代理的概念以及生成它的不同方法。

其他相关教程

这篇文章 生成 WCF 服务的代理的三种方法 首次出现在 WCF 教程 上。

© . All rights reserved.