WCF服务允许其他应用程序访问或使用它们.可以通过多种方式使用WCF服务,具体取决于主机类型.在这里,我们将解释为以下每个热门托管选项使用WCF服务的逐步方法 :
使用IIS 5/6中托管的WCF服务
使用自托管的WCF服务
使用Windows激活服务托管的WCF服务
使用Windows服务中托管的WCF服务
下面将详细讨论IIS 5/6中托管的WCF服务的使用过程.此外,讨论还包括如何创建代理和控制台应用程序.
步骤1 : 一旦服务托管在IIS中,我们就必须在客户端应用程序中使用它.在创建客户端应用程序之前,我们需要为服务创建代理.客户端应用程序使用此代理与服务进行交互.要创建代理,请运行Visual Studio 2008命令提示符.使用服务实用程序,我们可以创建代理类及其配置信息.
svcutilhttp://localhost/IISHostedService/Service.svc
执行此命令后,我们将在默认位置生成两个文件.
MyService.cs : WCF服务的代理类
output.config : 有关服务的配置信息
第2步 : 现在,我们将开始使用Visual Studio 2008(客户端应用程序)创建控制台应用程序.
第3步 : 添加引用'System.ServiceModel';这是WCF的核心dll.
第4步 : 创建一个Proxy类.
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace MyServiceClient { Class Program { Static void Main(string[] args) { // Creating Proxy for the MyService ServiceClient Client = newServiceClient(); Console.WriteLine("Client calling the service..."); Console.WriteLine("Hello Ram"); Console.Read(); } } }
输出显示如下 :
在这里,消费自托管WCF服务的整个过程将逐步解释,并在必要时提供充足的编码和屏幕截图.
第1步&减去;托管服务,现在我们需要为客户端实现代理类.有不同的方法来创建代理.
使用SvcUtil.exe,我们可以创建代理类及其配置文件使用端点.
向客户端应用程序添加服务引用.
实施ClientBase<T>class
在这三种方法中,实现ClientBase< T>是最好的做法.如果您使用其他两种方法,我们需要在每次在Service实现中进行任何更改时创建代理类.但ClientBase< T>的情况并非如此.它将仅在运行时创建代理,因此它将处理所有事情.
为此,创建一个代理类,其中包括System.ServiceModel和MyCalculatorService的引用.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; using MyCalculatorService; namespace MyCalculatorServiceProxy { // WCF create proxy for ISimpleCalculator using ClientBase Public class MyCalculatorServiceProxy : ClientBase<ISimpleCalculator>, ISimpleCalculator { Public int Add(int num1, int num2) { //Call base to do funtion returnbase.Channel.Add(num1, num2); } } }
现在,创建一个控制台应用程序,其中包括System.ServiceModel和MyCalculatorServiceProxy的引用.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; using MyCalculatorServiceProxy; namespace MyCalculatorServiceClient { classProgram { Static void Main(string[] args) { MyCalculatorServiceProxy.MyCalculatorServiceProxy proxy = newMyCalculatorServiceProxy.MyCalculatorServiceProxy(); Console.WriteLine("Client is running at " + DateTime.Now.ToString()); Console.WriteLine("Sum of two numbers. 5 + 5 =" + proxy.Add(5,5)); Console.ReadLine(); } } }
第2步 : 应将端点(与服务相同)信息添加到客户端应用程序的配置文件中.
<?xmlversion = "1.0"encoding = "utf-8" ?> <configuration> <system.serviceModel> <client> <endpoint address ="http://localhost:8090/MyCalculatorServiceProxy/ISimpleCalculator" binding = "wsHttpBinding" contract "MyCalculatorServiceProxy.ISimpleCalculator"> </endpoint> </client> </system.serviceModel> </configuration>
第3步 : 在运行客户端应用程序之前,您需要运行该服务.下面显示的是客户端应用程序的输出.
使用WAS托管的WCF服务是一个简单的过程,只涉及几个步骤.步骤如下 :
将代理类和配置文件添加到客户端应用程序.
为MathServiceClient创建对象并调用方法.
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespaceWASHostedClient { classProgram { staticvoid Main(string[] args) { MathServiceClient client = newMathServiceClient(); Console.WriteLine("Sum of two number 5,6"); Console.WriteLine(client.Add(5, 6)); Console.ReadLine(); } } }
输出如下所示.
下面详细介绍了如何使用Windows服务中托管的WCF服务的分步过程.编码和说明.
成功托管后,我们可以创建一个服务的代理类,并在客户端应用程序中开始使用.在这里,它显示IIS主机类型消耗.
使用System添加ServiceModel的引用.
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespaceWindowServiceClient { classProgram { staticvoid Main(string[] args) { //Creating Proxy for the MyService MyServiceClient client = newMyServiceClient(); Console.WriteLine("Client calling the service..."); Console.WriteLine("Sum of two numbers 5,6"); Console.WriteLine(client.Add(5, 6)); Console.WriteLine("Subtraction of two numbers 6,5"); Console.WriteLine(client.Sub(6, 5)); Console.WriteLine("Multiplication of two numbers 6,5"); Console.WriteLine(client.Mul(6, 5)); Console.WriteLine("Division of two numbers 6,3"); Console.WriteLine(client.Div(6, 3)); Console.Read(); } } }
输出显示如下 :