2022年WCF入门教程 .pdf

上传人:Che****ry 文档编号:27880016 上传时间:2022-07-26 格式:PDF 页数:10 大小:79.16KB
返回 下载 相关 举报
2022年WCF入门教程 .pdf_第1页
第1页 / 共10页
2022年WCF入门教程 .pdf_第2页
第2页 / 共10页
点击查看更多>>
资源描述

《2022年WCF入门教程 .pdf》由会员分享,可在线阅读,更多相关《2022年WCF入门教程 .pdf(10页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。

1、WCF 入门教程这一系列文章的内容是从MSDN 中 COPY 过来的,讲述的是最简单的WCF 程序示例: 如何在控制台应用程序实现和承载WCF 服务,以及如何创建、配置和使用WCF 客户端。文章主体可分为两部分,分别介绍服务器端和客户端的编程实现。细分的话,可以分为六项任务。服务器端定义 WCF 服务协定(任务一)这是创建基本Windows Communication Foundation (WCF) 服务和可以使用该服务的客户端所需的六项任务中的第一项任务。创建基本WCF 服务时,第一项任务是为与外界共享的服务创建协定,并在其中描述如何与该服务进行通信。具体步骤为:1、 创建新的控制台应用程

2、序项目。在“ 新建项目 ” 对话框中,选中 “Visual Basic”或“Visual C#”,并选择 “ 控制台应用程序” 模板,并命名为Service 。 使用默认的位置。2、将默认的Service命名空间更改为Microsoft.ServiceModel.Samples。3、为项目提供对System.ServiceModel命名空间的引用:右击“ 解决方案资源管理器” 中的“Service ”项目,选择 “ 添加引用 ” 项,在弹出的对话框中的“.NET” 选项卡里的 “ 组件名称 ” 中选择“System.ServiceModel” ,然后单击 “ 确定 ” 。下面是编程步骤:1、为

3、 System.ServiceModel命名空间添加一个using语句。using System.ServiceModel; 2、 创 建 一 个 新 的ICalculator 接 口 , 并 将 ServiceContractAttribute属 性 应 用 于 该 接 口 , 并将Namespace 值设置为 “http:/Microsoft.ServiceModel.Samples”。 此命名空间指定该服务在计算机上的路径,并构成该服务的基址部分。请注意,在通过采用方括号表示法的属性来批注接口或类时,该属性类可以从其名称中去掉“Attribute”部分。ServiceContract(N

4、amespace = http:/Microsoft.ServiceModel.Samples) public interface ICalculator 3、在接口中创建方法声明,并将 OperationContractAttribute属性应用于每个要作为公共WCF 协定的一部分公开的方法。OperationContract double Add(double n1, double n2); OperationContract double Subtract(double n1, double n2); OperationContract double Multiply(double n1

5、, double n2); OperationContract 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 10 页 - - - - - - - - - double Divide(double n1, double n2); 下面是创建服务协定的完整代码段:using System; / Add the using statement for the Sytem.ServiceModel namespace using System.ServiceModel; na

6、mespace Microsoft.ServiceModel.Samples / Define a service contract. ServiceContract(Namespace = http:/Microsoft.ServiceModel.Samples) public interface ICalculator / Create the method declaration for the contract. OperationContract double Add(double n1, double n2); OperationContract double Subtract(d

7、ouble n1, double n2); OperationContract double Multiply(double n1, double n2); OperationContract double Divide(double n1, double n2); . (整个服务器端的代码未完,等讲完任务三时,再给出完整代码)实现 WCF 服务协定(任务二)1、创建一个新CalculatorService类,该类从用户定义的ICalculator接口继承而来并实现该接口定义的协定功能。public class CalculatorService : ICalculator2、实现每个算术运算

8、符的功能。public double Add(double n1, double n2) double result = n1 + n2; Console.WriteLine(Received Add(0,1), n1, n2); / Code added to write output to the console window. Console.WriteLine(Return: 0, result); return result ; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - -

9、第 2 页,共 10 页 - - - - - - - - - public double Subtract(double n1, double n2) double result = n1 - n2; Console.WriteLine(Received Subtract(0,1), n1, n2); Console.WriteLine(Return: 0, result); return result; public double Multiply(double n1, double n2) double result = n1 * n2; Console.WriteLine(Receive

10、d Multiply(0,1), n1, n2); Console.WriteLine(Return: 0, result); return result; public double Divide(double n1, double n2) double result = n1 / n2; Console.WriteLine(Received Divide(0,1), n1, n2); Console.WriteLine(Return: 0, result); return result; 在创建和实现了服务协定后,下一步是运行该服务。运行服务由三个步骤组成:配置、承载和打开服务。配置、承载

11、和运行服务(任务三)为服务配置基址为服务的基址创建 Uri 实例。此 URI 指定 HTTP 方案、本地计算机、端口号8000 ,以及服务协定中为服务命名空间指定的服务路径ServiceModelSample/Services。Uri baseAddress = new Uri(http:/localhost:8000/ServiceModelSamples/Service);承载服务1. 创建一个新的 ServiceHost 实例以承载服务。必须指定实现服务协定和基址的类型。对于此示例,我们将基址指定为http:/localhost:8000/ServiceModelSamples/Serv

12、ices,并将CalculatorService指定为实现服务协定的类型。ServiceHost selfHost = new ServiceHost(typeof(CalculatorService), baseAddress); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页,共 10 页 - - - - - - - - - 2. 添加一个捕获 CommunicationException 的 try -catch 语句,并在接下来的三个步骤中将该代码添加到try 块中。

13、3. 添加公开服务的终结点。为此,必须指定终结点公开的协议、绑定和终结点的地址。此例中,将 ICalculator 指定为协定, 将 WSHttpBinding 指定为绑定, 并将 CalculatorService 指定为地址。在这里请注意,我们指定的是相对地址。终结点的完整地址是基址和终结点地址的组合。 在此例中,完整地址是 http:/localhost:8000/ServiceModelSamples/Services/CalculatorService。selfHost.AddServiceEndpoint( typeof(ICalculator), new WSHttpBindin

14、g(), CalculatorService); 4. 启用元数据交换。为此,必须添加服务元数据行为。首先创建一个 ServiceMetadataBehavior 实例,将 HttpGetEnabled 属性设置为true ,然后为服务添加新行为。ServiceMetadataBehavior smb = new ServiceMetadataBehavior(); smb.HttpGetEnabled = true; selfHost.Description.Behaviors.Add(smb);5. 打开 ServiceHost并等待传入消息。用户按Enter 键时,关闭ServiceHo

15、st。 selfHost.Open(); Console.WriteLine(The service is ready.); Console.WriteLine(Press to terminate service.); Console.WriteLine(); Console.ReadLine(); / Close the ServiceHostBase to shutdown the service. selfHost.Close(); 下面是服务器端(即“Service ”项目中 program.cs文件中)的完整程序代码:using System; using System.Servi

16、ceModel; using System.ServiceModel.Description; namespace Microsoft.ServiceModel.Samples / Define a service contract. ServiceContract(Namespace = http:/Microsoft.ServiceModel.Samples) public interface ICalculator OperationContract double Add(double n1, double n2); OperationContract double Subtract(d

17、ouble n1, double n2); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 4 页,共 10 页 - - - - - - - - - OperationContract double Multiply(double n1, double n2); OperationContract double Divide(double n1, double n2); / Service class that implements the service contract. / A

18、dded code to write output to the console window. public class CalculatorService : ICalculator public double Add(double n1, double n2) double result = n1 + n2; Console.WriteLine(Received Add(0,1), n1, n2); Console.WriteLine(Return: 0, result); return result; public double Subtract(double n1, double n

19、2) double result = n1 - n2; Console.WriteLine(Received Subtract(0,1), n1, n2); Console.WriteLine(Return: 0, result); return result; public double Multiply(double n1, double n2) double result = n1 * n2; Console.WriteLine(Received Multiply(0,1), n1, n2); Console.WriteLine(Return: 0, result); return re

20、sult; public double Divide(double n1, double n2) double result = n1 / n2; Console.WriteLine(Received Divide(0,1), n1, n2); Console.WriteLine(Return: 0, result); return result; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 5 页,共 10 页 - - - - - - - - - class Program s

21、tatic void Main(string args) / Create a URI to serve as the base address. Uri baseAddress = new Uri(http:/localhost:8000/ServiceModelSamples/Service); / Create ServiceHost ServiceHost selfHost = new ServiceHost(typeof(CalculatorService), baseAddress); try / Add a service endpoint. selfHost.AddServic

22、eEndpoint( typeof(ICalculator), new WSHttpBinding(), CalculatorService); / Enable metadata exchange. ServiceMetadataBehavior smb = new ServiceMetadataBehavior(); smb.HttpGetEnabled = true; selfHost.Description.Behaviors.Add(smb); / Start (and then stop) the service. selfHost.Open(); Console.WriteLin

23、e(The service is ready.); Console.WriteLine(Press to terminate service.); Console.WriteLine(); Console.ReadLine(); / Close the ServiceHostBase to shutdown the service. selfHost.Close(); catch (CommunicationException ce) Console.WriteLine(An exception occurred: 0, ce.Message); selfHost.Abort(); 名师资料总

24、结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 6 页,共 10 页 - - - - - - - - - 若要运行服务,启动项目文件夹下bin 目录中的Service.exe即可。前面三篇文章是讲服务器端的部署和运行,下面讲讲客户端如何配置和使用。创建 WCF 客户端(任务四)1. 通过执行以下步骤,在Visual Studio 2005 中为客户端创建新项目:1. 在包含该服务(之前文章所述的服务)的同一解决方案中的“ 解决方案资源管理器” (位于右上角)中,右击当前解决方案,然后选择“ 添

25、加新项目 ” 。2. 在“ 添加新项目 ” 对话框中, 选择 “Visual Basic”或“Visual C#”,选择 “ 控制台应用程序”模板,然后将其命名为Client 。 使用默认的位置。3. 单击 “ 确定 ” 。2. 为项目提供对System.ServiceModel命名空间的引用:在“ 解决方案资源管理器” 中右击“Service ”项目,从 “.NET” 选项卡上的 “ 组件名称 ” 列中选择 “System.ServiceModel” ,然后单击“ 确定 ” 。3. 为 System.ServiceModel命名空间添加using语句: using System.Servic

26、eModel; 4. 启动在前面的步骤中创建的服务。(即打开在服务器项目中生成的Service.exe 可执行文件)5. 通过执行以下步骤,使用适当的开关运行Service Model Metadata Utility Tool (SvcUtil.exe) 以创建客户端代码和配置文件:1. 通过选择 “ 开始 ” 菜单中的 “Microsoft Windows SDK” 项下的 “CMD Shell ” ,启动Windows SDK 控制台会话。2. 导航到要放置客户端代码的目录。如果使用默认设置创建Client 项目,则目录为C:Documents and SettingsDocuments

27、Visual Studio 2008ProjectsServiceClient。3. 将命令行工具Service Model Metadata Utility Tool (SvcUtil.exe) 与适当的开关一起使用以创建客户端代码。下面的示例生成服务的代码文件和配置文件。svcutil.exe /language:cs /out:generatedProxy.cs /config:app.config http:/localhost:8000/ServiceModelSamples/service 6. 在 Visual Studio 中将生成的代理添加到Client 项目中, 方法是在

28、“ 解决方案资源管理器” 中右击“Client”并选择 “ 添加现有项 ” 。 然后选择在上一步中生成的generatedProxy.cs文件。配置 WCF 客户端(任务五)在 Visual Studio 中,将在前一过程中生成的App.config配置文件添加到客户端项目中。在“ 解决方案资源管理器 ” 中右击该客户端,选择“ 添加现有项 ” ,然后从C:Documents and SettingsDocumentsVisual Studio 2008ProjectsServiceClientbin 目录中选择App.config配置文件。将 app.config添加到项目中后,就算是完成了

29、wcf 客户端的配置。因为具体的配置信息,我们在使用svcutil.exe 工具时,它就帮我们配置好并写入了app.config文件。名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 7 页,共 10 页 - - - - - - - - - 使用 WCF 客户端(任务六)1、为要调用的服务的基址创建 EndpointAddress 实例,然后创建WCF Client对象。/Create an endpoint address and an instance of the WCF Cli

30、ent. 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

31、= 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, val

32、ue2, 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);

33、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(); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 8 页,共 10 页 - - - - - - - - - 下面是客户端的完整代码:usi

34、ng 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/Service

35、ModelSamples/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.Write

36、Line(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

37、 = 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); 名师资料总结 - - -精品资料欢迎下载 - - - - - -

38、 - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 9 页,共 10 页 - - - - - - - - - /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

39、Windows SDK ”项下选择 “CMD Shell”,从而启动 Windows SDK 控制台会话。定位至C:Documents and SettingsDocumentsVisual Studio 2008ProjectsServiceClientobjDebug 目录,键入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. 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 10 页,共 10 页 - - - - - - - - -

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 高考资料

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知得利文库网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号-8 |  经营许可证:黑B2-20190332号 |   黑公网安备:91230400333293403D

© 2020-2023 www.deliwenku.com 得利文库. All Rights Reserved 黑龙江转换宝科技有限公司 

黑龙江省互联网违法和不良信息举报
举报电话:0468-3380021 邮箱:hgswwxb@163.com