您的位置:首页 > 其它

MSDN上面的WCF入门教程练习

2010-08-04 17:00 260 查看
http://msdn.microsoft.com/zh-cn/library/ms734712.aspx

如何:定义服务协定
如何:实现服务协定
如何:承载和运行基本的服务
如何:创建客户端
如何:配置客户端
如何:使用客户端

照着说明测试了一下:
一、创建解决方案WCFtest

二、创建服务端
1、在WCFtest下添加一个“控制台应用程序”,命名为
Service

2、修改默认的命名空间为
Microsoft.ServiceModel.Samples

3、添加引用System.ServiceModel.dll
4、编写Program.cs代码:

View Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;
namespace Client
{
class Program
{
static void Main(string[] args)
{
//Step 1: Create an endpoint address and an instance of the WCF Client.
CalculatorClient client = new CalculatorClient();

// 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();
}
}
}


最后,先打开Server\bin\Debug下的Service.exe运行服务端,再打开Client\bin\Debug下的Client.exe运行客户端。
显示如下:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: