`
winzenghua
  • 浏览: 1317439 次
  • 性别: Icon_minigender_2
  • 来自: 广州
文章分类
社区版块
存档分类
最新评论

MSDN上的WCF入门教程

阅读更多

MSDN上发现了一篇很好的WCF入门教程

看了很多学习WCF的例子,感觉受益匪浅,但是由于每个人学习的侧重点不同,一些很详细的细节例如每一个属性都是用来干什么的,建立不同的项目类型对创建的服务有什么区别等等,都不得而知。终于,在MSDN上发现了一篇入门教程。讲解的十分基本,十分详细,想进到每一个细节,然我彻底了解入门的每一个细节,整个教程结构清晰,代码简洁,讲解细致,值得推荐。

地址: http://msdn.microsoft.com/en-us/library/ms734712.aspx

做这分5部来讲解创建一个最基本的基于B/S构架的WCF应用。服务是根据输入的两个数字,返回这两个数字的加减乘除运算结果。

第一步:定义WCF服务契约(创建项目,加入引用,定义Interface)
第二部:引入WCF服务契约(添加具体服务函数)
第三部:构架WCF服务,运行WCF服务(添加Uri,定义服务对象地址,运行服务)
第四部:利用工具访问服务,自动生成WCF服务代理的代码文件
第五部:配置一个简单的WCF客户端(用客户端引入服务代理,通过服务代理来访问服务)
第六部:运行程序

先建立一个解决方案。
在这个解决方案下面建立一个叫做Server的控制台应用项目,再建立一个叫做Client的控制台应用项目。
分别给每一个项目添加引用到System.ServiceModel
编辑每个项目下面的Program.cs

服务端Program.cs代码
复制内容到剪贴板
代码:
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.ServiceModel;
5
6namespace ServiceModelSamples
7{
8
9 class Client
10 {
11 static void Main()
12 {
13 //Step 1: Create an endpoint address and an instance of the WCF Client.
14 EndpointAddress epAddress = new EndpointAddress("http://localhost:8000/ServiceModelSamples/Service/CalculatorService");
15 CalculatorClient client = new CalculatorClient(new WSHttpBinding(), epAddress);
16
17
18 // Step 2: Call the service operations.
19 // Call the Add service operation.
20 double value1 = 100.00D;
21 double value2 = 15.99D;
22 double result = client.Add(value1, value2);
23 Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result);
24
25 // Call the Subtract service operation.
26 value1 = 145.00D;
27 value2 = 76.54D;
28 result = client.Subtract(value1, value2);
29 Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result);
30
31 // Call the Multiply service operation.
32 value1 = 9.00D;
33 value2 = 81.25D;
34 result = client.Multiply(value1, value2);
35 Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result);
36
37 // Call the Divide service operation.
38 value1 = 22.00D;
39 value2 = 7.00D;
40 result = client.Divide(value1, value2);
41 Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result);
42
43 //Step 3: Closing the client gracefully closes the connection and cleans up resources.
44 client.Close();
45
46 Console.WriteLine();
47 Console.WriteLine("Press <ENTER> to terminate client.");
48 Console.ReadLine();
49
50
51 }
52 }
53}
54
服务端创建好了以后,就可以试运行了。
这时候可以用微软提供的命令行工具访问这个服务,生成服务代理 app.config 和 generatedProxy.cs两个文件。

svcutil.exe /language:cs /out:generatedProxy.cs /config:app.config http://localhost:8000/ServiceModelSamples/service

把这两个文件添加到客户端项目里去。
现在就可以编辑客户端代码了。

客户端Program.cs代码
复制内容到剪贴板
代码:
1using System;
2using System.ServiceModel;
3using System.ServiceModel.Description;
4
5namespace Microsoft.ServiceModel.Samples
6{
7 // Define a service contract.
8 [ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples")]
9 public interface ICalculator
10 {
11 [OperationContract]
12 double Add(double n1, double n2);
13 [OperationContract]
14 double Subtract(double n1, double n2);
15 [OperationContract]
16 double Multiply(double n1, double n2);
17 [OperationContract]
18 double Divide(double n1, double n2);
19 }
20
21 // Service class that implements the service contract.
22 // Added code to write output to the console window.
23 public class CalculatorService : ICalculator
24 {
25 public double Add(double n1, double n2)
26 {
27 double result = n1 + n2;
28 Console.WriteLine("Received Add({0},{1})", n1, n2);
29 Console.WriteLine("Return: {0}", result);
30 return result;
31 }
32
33 public double Subtract(double n1, double n2)
34 {
35 double result = n1 - n2;
36 Console.WriteLine("Received Subtract({0},{1})", n1, n2);
37 Console.WriteLine("Return: {0}", result);
38 return result;
39 }
40
41 public double Multiply(double n1, double n2)
42 {
43 double result = n1 * n2;
44 Console.WriteLine("Received Multiply({0},{1})", n1, n2);
45 Console.WriteLine("Return: {0}", result);
46 return result;
47 }
48
49 public double Divide(double n1, double n2)
50 {
51 double result = n1 / n2;
52 Console.WriteLine("Received Divide({0},{1})", n1, n2);
53 Console.WriteLine("Return: {0}", result);
54 return result;
55 }
56 }
57
58
59 class Program
60 {
61 static void Main(string[] args)
62 {
63
64 // Step 1 of the address configuration procedure: Create a URI to serve as the base address.
65 Uri baseAddress = new Uri("http://localhost:8000/ServiceModelSamples/Service");
66
67 // Step 1 of the hosting procedure: Create ServiceHost
68 ServiceHost selfHost = new ServiceHost(typeof(CalculatorService), baseAddress);
69 try
70 {
71
72
73 // Step 3 of the hosting procedure: Add a service endpoint.
74 selfHost.AddServiceEndpoint(
75 typeof(ICalculator),
76 new WSHttpBinding(),
77 "CalculatorService");
78
79
80 // Step 4 of the hosting procedure: Enable metadata exchange.
81 ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
82 smb.HttpGetEnabled = true;
83 selfHost.Description.Behaviors.Add(smb);
84
85 // Step 5 of the hosting procedure: Start (and then stop) the service.
86 selfHost.Open();
87 Console.WriteLine("The service is ready.");
88 Console.WriteLine("Press <ENTER> to terminate service.");
89 Console.WriteLine();
90 Console.ReadLine();
91
92 // Close the ServiceHostBase to shutdown the service.
93 selfHost.Close();
94 }
95 catch (CommunicationException ce)
96 {
97 Console.WriteLine("An exception occurred: {0}", ce.Message);
98 selfHost.Abort();
99 }
100 }
101 }
102}
103
分享到:
评论

相关推荐

    MSDN一篇很好的WCF入门教程

    MSDN一篇很好的WCF入门教程 很多学习WCF的例子,感觉受益匪浅,但是由于每个人学习的侧重点不同,一些很详细的细节例如每一个属性都是用来干什么的,建立不同的项目类型对创建的服务有什么区别等等,都不得而知。...

    WCF入门教程_从入门到精通

    最好的WCF入门教程 WCF 入门教程这一系列文章的内容是从 MSDN 中 COPY 过来的, 讲述的是最简单的 WCF 程序示例: 如何在控制台应 用程序实现和承载 WCF 服务,以及如何创建,配置和使用 WCF 客户端

    WCF服务(初学者使用,很好的一个例子)

    很多学习WCF的例子,感觉受益匪浅,但是由于每...终于,在MSDN上发现了一篇入门教程。讲解的十分基本,十分详细,想进到每一个细节,然我彻底了解入门的每一个细节,整个教程结构清晰,代码简洁,讲解细致,值得推荐。

    WCF Hands-On-Lab Manual

    微软MSDN的WCF实验室手册 是WCF初学者的入门法宝

    MSDN_08年第一期

    本期MSDN杂志的主要内容有: • IIS 7.0: 使用集成的 ASP.NET 管道增强应用程序 • 全球化就绪: 与 ASP.NET AJAX 应用程序环游地球 • WCF 整合: 使用 WCF 和 .NET Framework 3.5 进行 HTTP 编程 • SQL Server:...

    MSDN杂志 2008年第二期

    测试运行: Microsoft UI 自动化库 &lt;br&gt;• 基础: Visual Studio 2008 中 WCF 的新增功能 &lt;br&gt;• .NET 相关问题: 流管道 &lt;br&gt;• { End Bracket }: 招聘顶尖人才 &lt;br&gt;• 编者按: 通过一流的设计解决语言...

    MSDN杂志2008年四月刊

    基础: WCF 中的代码访问安全性,第 1 部分 在本文中,我们将讨论 Windows Communication Foundation (WCF) 中的代码访问安全性,并提供一种解决方案来针对 WCF 服务启用部分信任的客户端。 测试运行: 使用 LINQ ...

    MSDN杂志 2008年新产品特刊

    本期的主要内容: &lt;br&gt;移动 WCF: 使用 .NET Compact Framework 编写 IM 应用程序 .NET Compact Framework 3.5 是 Windows Communication Foundation (WCF) 的功能子集,可以用来控制 Windows Mobile 设备与...

    [超全]C# 2010(C# 4.0)VS2010最新学习资料

    最新版Visual Studio 2010 MSDN原版(内置Key/序列号) 全系列下载 MSDN Visual Studio 2010 全系列 RTM 英文原版光盘镜像下载 Visual Assist X for Visual Studio 10.6 最新版 VS6.0~VS2008 + VS2010版下载 ...

    C#与.NET3.5高级程序设计

    他曾为MSDN网站和MacTech网站撰写了有关各种操作系统平台上.NET技术的文章,并经常在业界主要技术会议上发表f演讲和开设技术讲座。除本书外,他还撰写了COM and Net interoperabity和Visual Basic ...

Global site tag (gtag.js) - Google Analytics