入门代码教程第六节 如何:使用客户端
2008-09-25 12:10
721 查看
如何:使用 Windows Communication Foundation 客户端
在创建并配置了 Windows Communication Foundation (WCF) 代理后,就可以创建客户端实例,进而编译客户端应用程序并使用它与 WCF 服务进行通信。 本主题描述创建和使用 WCF 客户端的过程。 此过程执行三个操作:创建 WCF 客户端,从生成的代理调用服务操作,以及在完成操作调用后关闭客户端。
在过程后面的示例中还提供了过程中所讨论的代码。
使用 Windows Communication Foundation 客户端
1.为要调用的服务的基址创建 EndpointAddress 实例,然后创建 WCF [b]Client 对象。[/b]
2.从 [b]Client 内调用客户端操作。[/b]
3.在 WCF 客户端上调用 [b]Close。[/b]
将生成的 WCF 客户端和下面的代码示例编译为可执行文件 Client.exe。 在编译代码时,务必引用 System.ServiceModel。
Code
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 <ENTER> to terminate client.");
Console.ReadLine();
}
}
}
确保在尝试使用客户端之前服务正在运行。
WCF是不是太简单了呢。当然,有关优化和配置和复杂的部分,后面也提高,不过现在应该很开心了吧
在创建并配置了 Windows Communication Foundation (WCF) 代理后,就可以创建客户端实例,进而编译客户端应用程序并使用它与 WCF 服务进行通信。 本主题描述创建和使用 WCF 客户端的过程。 此过程执行三个操作:创建 WCF 客户端,从生成的代理调用服务操作,以及在完成操作调用后关闭客户端。
在过程后面的示例中还提供了过程中所讨论的代码。
使用 Windows Communication Foundation 客户端
1.为要调用的服务的基址创建 EndpointAddress 实例,然后创建 WCF [b]Client 对象。[/b]
2.从 [b]Client 内调用客户端操作。[/b]
3.在 WCF 客户端上调用 [b]Close。[/b]
示例
下面的示例演示如何创建 WCF 客户端,如何调用客户端操作,以及在完成操作调用后如何关闭客户端。将生成的 WCF 客户端和下面的代码示例编译为可执行文件 Client.exe。 在编译代码时,务必引用 System.ServiceModel。
Code
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 <ENTER> to terminate client.");
Console.ReadLine();
}
}
}
确保在尝试使用客户端之前服务正在运行。
WCF是不是太简单了呢。当然,有关优化和配置和复杂的部分,后面也提高,不过现在应该很开心了吧
相关文章推荐
- 入门代码教程第五节 如何:配置客户端
- 入门代码教程第四节 如何:创建客户端
- C++ STL入门教程(2)——list(双向链表)的使用(附完整程序代码)
- 如何使用CodeSmith批量生成代码(原创系列教程)
- 【配套代码 及 视频下载地址】 使用Visual Studio2005入门.Net2.0系列视频教程
- Yii2.0教程入门篇——使用Gii生成代码
- 如何使用github托管项目---快速入门(菜鸟教程)
- (转载)directshow的下载、VC2008中的配置、入门教程以及如何在opencv中使用
- 如何使用Retrofit写一个Android的REST客户端的小教程
- 黄聪:如何使用CodeSmith批量生成代码(原创系列教程)
- 入门代码教程第一节 如何:定义服务协定
- directshow的下载、VC2008中的配置、入门教程以及如何在opencv中使用
- 使用XFire开发Web Service客户端完整入门教程
- C#分析数据库结构,使用XSL模板自动生成代码 - 清清月儿 .NET万花筒 Asp.net技术 Asp.net教程 Asp.net源码 Asp.net基础 Asp.net控件 Asp.net入门 - CSDNBlog
- C++ STL入门教程(2)——list(双向链表)的使用(附完整程序代码)
- 黄聪:如何使用CodeSmith批量生成代码(原创系列教程)
- C++ STL入门教程(1)——vector(向量容器)的使用(附完整程序代码)
- C++ STL入门教程(2)——list(双向链表)的使用(附完整程序代码)
- C++ STL入门教程(4)——stack(栈),queue(队列),priority_queue(优先队列)的使用(附完整程序代码)
- 使用XFire开发Web Service客户端完整入门教程