WCF是Windows平台下程序间通讯的应用程序框架。整合和 .net Remoting,WebService,Socket的机制,是用来开发windows平台上分布式开发的最佳选择。wcf程序的运行需要一个宿主ServiceHost,我们可以选用控制台应用程序,也可以选择IIS寄宿,还可以选择windows 服务寄宿。相较与控制台程序,IIS,和Windows服务比较稳定。而且大家不会时不时的去重启下IIS下的网站,或者windows服务。
在IIS下寄宿Wcf
我们新建一个类库项目
在项目下添加一个ICalculator接口类,实现wcf 服务契约,操作契约接口
using System.ServiceModel; namespace IISServices { [ServiceContract(Name = "CalculatorService")] public interface ICalculator { [OperationContract] double Add(double x, double y); [OperationContract] double Subtract(double x, double y); [OperationContract] double Multiply(double x, double y); [OperationContract] double Divide(double x, double y); } }
新建一个服务类CalculatorService,实现服务契约接口ICalculator
namespace IISServices { public class CalculatorService : ICalculator { public double Add(double x, double y) { return x + y; } public double Subtract(double x, double y) { return x - y; } public double Multiply(double x, double y) { return x * y; } public double Divide(double x, double y) { return x / y; } } }
添加一个文件,文件名为CalculatorService.svc就是我们用来寻找服务对外暴漏的入口。只需要添加一行代码就可以。当我们访问服务的时候IIS会寻找我们这个svc文件来找到我们提供的服务。
<%@ServiceHost Service="IISServices.CalculatorService"%>
添加一个web.Config文件,添加system.serviceModel节点的配置信息。里面不需要配置我们访问服务的地址,因为IIS下我们网站的地址就是我们访问服务的地址。
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <behaviors> <serviceBehaviors> <behavior name="metadataBehavior"> <serviceMetadata httpGetEnabled="true"/> </behavior> </serviceBehaviors> </behaviors> <services> <service behaviorConfiguration="metadataBehavior" name="IISServices.CalculatorService"> <endpoint binding="wsHttpBinding" contract="IISServices.ICalculator" /> </service> </services> </system.serviceModel> </configuration>
项目详细如下,另外应用里面需要添加System.ServiceModel这个dll引用,wcf的大部分实现都在这个类库里面:
我们在IIS下面新建一个网站,根目录只需要添加web.Config,svc服务文件即可,bin下面放我们生成的IISServices.dll如下:
网站访问端口我们配置为82,启动网站。
在我们需要引用服务的类库或exe上添加服务引用http://localhost:82/CalculatorService.svc,就可以找到我们需要的服务了。
在Windows服务下寄宿wcf服务
我们新建一个控制台应用程序Service。添加下面这三个类库引用
-
System.ServiceModel.dll
-
System.ServiceProcess.dll
-
System.Configuration.Install.dll
将Programs.cs修改为Service.cs,添加代码如下
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ComponentModel; using System.ServiceModel; using System.ServiceProcess; using System.Configuration; using System.Configuration.Install; 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(double n1, double n2); [OperationContract] double Multiply(double n1, double n2); [OperationContract] double Divide(double n1, double n2); } // Implement the ICalculator service contract in a service class. public class CalculatorService : ICalculator { // Implement the ICalculator methods. public double Add(double n1, double n2) { double result = n1 + n2; return result; } public double Subtract(double n1, double n2) { double result = n1 - n2; return result; } public double Multiply(double n1, double n2) { double result = n1 * n2; return result; } public double Divide(double n1, double n2) { double result = n1 / n2; return result; } } public class CalculatorWindowsService : ServiceBase { public ServiceHost serviceHost = null; public CalculatorWindowsService() { // Name the Windows Service ServiceName = "WCFWindowsServiceSample"; } public static void Main() { ServiceBase.Run(new CalculatorWindowsService()); } // Start the Windows service. protected override void OnStart(string[] args) { if (serviceHost != null) { serviceHost.Close(); } // Create a ServiceHost for the CalculatorService type and // provide the base address. serviceHost = new ServiceHost(typeof(CalculatorService)); // Open the ServiceHostBase to create listeners and start // listening for messages. serviceHost.Open(); } protected override void OnStop() { if (serviceHost != null) { serviceHost.Close(); serviceHost = null; } } } // Provide the ProjectInstaller class which allows // the service to be installed by the Installutil.exe tool [RunInstaller(true)] public class ProjectInstaller : Installer { private ServiceProcessInstaller process; private ServiceInstaller service; public ProjectInstaller() { process = new ServiceProcessInstaller(); process.Account = ServiceAccount.LocalSystem; service = new ServiceInstaller(); service.ServiceName = "WCFWindowsServiceSample"; Installers.Add(process); Installers.Add(service); } } }