Chinaunix首页 | 论坛 | 博客
  • 博客访问: 46462
  • 博文数量: 11
  • 博客积分: 640
  • 博客等级: 上士
  • 技术积分: 175
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-27 23:19
文章分类
文章存档

2011年(1)

2008年(10)

我的朋友

分类: WINDOWS

2008-11-13 23:15:05

   前面三篇文章是讲服务器端的部署和运行,下面讲讲客户端如何配置和使用。

  • 创建WCF客户端
  1. 通过执行以下步骤,在 Visual Studio 2005 中为客户端创建新项目:

    1. 在包含该服务(之前文章所述的服务)的同一解决方案中的“解决方案资源管理器”(位于右上角)中,右击当前解决方案,然后选择“添加新项目”

    2. “添加新项目”对话框中,选择“Visual Basic”“Visual C#”,选择“控制台应用程序”模板,然后将其命名为 Client。 使用默认的位置。

    3. 单击“确定”

  2. 为项目提供对 System.ServiceModel 命名空间的引用:在“解决方案资源管理器”中右击“Service”项目,从“.NET”选项卡上的“组件名称”列中选择“System.ServiceModel”,然后单击“确定”

  3. System.ServiceModel 命名空间添加 using 语句:using System.ServiceModel;

  4. 启动在前面的步骤中创建的服务。(即打开在服务器项目中生成的Service.exe可执行文件)

  5. 通过执行以下步骤,使用适当的开关运行Service Model Metadata Utility Tool (SvcUtil.exe) 以创建客户端代码和配置文件:

    1. 通过选择“开始”菜单中的“Microsoft Windows SDK”项下的“CMD Shell”,启动 Windows SDK 控制台会话。

    2. 导航到要放置客户端代码的目录。 如果使用默认设置创建 Client 项目,则目录为 C:\Documents and Settings\<用户名>\Documents\Visual Studio 2008\Projects\Service\Client。

    3. 将命令行工具Service Model Metadata Utility Tool (SvcUtil.exe) 与适当的开关一起使用以创建客户端代码。 下面的示例生成服务的代码文件和配置文件。

      svcutil.exe /language:cs /out:generatedProxy.cs /config:app.config 
  6. 在 Visual Studio 中将生成的代理添加到 Client 项目中,方法是在“解决方案资源管理器”中右击“Client”并选择“添加现有项”。 然后选择在上一步中生成的 generatedProxy.cs 文件。


  • 配置WCF客户端
    在 Visual Studio 中,将在前一过程中生成的 App.config 配置文件添加到客户端项目中。 在“解决方案资源管理器”中右击该客户端,选择“添加现有项”,然后从 C:\Documents and Settings\<用户名>\Documents\Visual Studio 2008\Projects\Service\Client\bin 目录中选择 App.config 配置文件。
    将app.config添加到项目中后,就算是完成了wcf客户端的配置。因为具体的配置信息,我们在使用svcutil.exe工具时,它就帮我们配置好并写入了app.config文件。


  • 使用WCF客户端

1、为要调用的服务的基址创建 EndpointAddress 实例,然后创建 WCF Client 对象。

    //Create an endpoint address and an instance of the WCF Client.
EndpointAddress epAddress = new EndpointAddress("http://localhost:8000/ServiceModelSamples/Service/CalculatorService");
CalculatorClient client = new CalculatorClient(new WSHttpBinding(), epAddress);

2、从 Client 内调用客户端操作。

// Call the service operations.
// Call the Add service operation.
double value1 = 100.00D;
double value2 = 15.99D;
double result = client.Add(value1, value2);
Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result);

// Call the Subtract service operation.
value1 = 145.00D;
value2 = 76.54D;
result = client.Subtract(value1, value2);
Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result);

// Call the Multiply service operation.
value1 = 9.00D;
value2 = 81.25D;
result = client.Multiply(value1, value2);
Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result);

// Call the Divide service operation.
value1 = 22.00D;
value2 = 7.00D;
result = client.Divide(value1, value2);
Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result);

3、在 WCF 客户端上调用 Close

// Closing the client gracefully closes the connection and cleans up resources.
client.Close();


下面是客户端的完整代码:
using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;

namespace ServiceModelSamples


{

class Client
{
static void Main()
{
//Step 1: Create an endpoint address and an instance of the WCF Client.
EndpointAddress epAddress = new EndpointAddress("http://localhost:8000/ServiceModelSamples/Service/CalculatorService");
CalculatorClient client = new CalculatorClient(new WSHttpBinding(), epAddress);


// Step 2: Call the service operations.
// Call the Add service operation.
double value1 = 100.00D;
double value2 = 15.99D;
double result = client.Add(value1, value2);
Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result);

// Call the Subtract service operation.
value1 = 145.00D;
value2 = 76.54D;
result = client.Subtract(value1, value2);
Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result);

// Call the Multiply service operation.
value1 = 9.00D;
value2 = 81.25D;
result = client.Multiply(value1, value2);
Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result);

// Call the Divide service operation.
value1 = 22.00D;
value2 = 7.00D;
result = client.Divide(value1, value2);
Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result);

//Step 3: Closing the client gracefully closes the connection and cleans up resources.
client.Close();

Console.WriteLine();
Console.WriteLine("Press to terminate client.");
Console.ReadLine();


}
}
}

若要启动客户端,请在“开始”菜单中的“Microsoft Windows SDK”项下选择“CMD Shell”,从而启动 Windows SDK 控制台会话。 定位至 C:\Documents and Settings\<用户名>\Documents\Visual Studio 2008\Projects\Service\Client\obj\Debug 目录,键入 client,然后按 Enter。 操作请求和响应将出现在客户端控制台窗口中,如下所示。

Add(100,15.99) = 115.99
Subtract(145,76.54) = 68.46
Multiply(9,81.25) = 731.25
Divide(22,7) = 3.14285714285714

Press to terminate client.


PS:再一次说明,这些文章的内容都摘自MSDN。在实际操作中,可能会遇到一些错误或者异常,希望各位能够
认真参考文档、仔细差错,并且可以在我们的群上提问,我们来共同解决。

阅读(1860) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2009-04-07 10:13:18

ZZxs