在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); } } }
在App.Config里面添加配置節點ServiceModel如下:

<system.serviceModel> <services> <!-- This section is optional with the new configuration model introduced in .NET Framework 4. --> <service name="Microsoft.ServiceModel.Samples.CalculatorService" behaviorConfiguration="CalculatorServiceBehavior"> <host> <baseAddresses> <add baseAddress="http://localhost:8000/ServiceModelSamples/service"/> </baseAddresses> </host> <!-- this endpoint is exposed at the base address provided by host: http://localhost:8000/ServiceModelSamples/service --> <endpoint address="" binding="wsHttpBinding" contract="Microsoft.ServiceModel.Samples.ICalculator" /> <!-- the mex endpoint is exposed at http://localhost:8000/ServiceModelSamples/service/mex --> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> </service> </services> <behaviors> <serviceBehaviors> <behavior name="CalculatorServiceBehavior"> <serviceMetadata httpGetEnabled="true"/> <serviceDebug includeExceptionDetailInFaults="False"/> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel>
生成文件,注意看程序的入口main函數ServiceBase.Run(new CalculatorWindowsService());在main函數里面程序調用了windows服務。我們這個項目本質上是一個windows服務。windows服務不可以直接運行,需要先安裝。我們使用管理員身份打開cmd.exe
進入我們的.net安裝目錄,我這個是安裝的4.0,所以進入C:\Windows\Microsoft.NET\Framework64\v4.0.30319文件夾,找到該文件夾下面的InstallUtil.exe程序。在cmd里面輸入如下,回車
進入.net安裝文件夾,輸入InstallUtil.exe "生成服務所在的路徑",注意這兒最好給路徑加上引號,沒有引號如果碰到空格可能報錯。
安裝后如果出現下面的successfully,說明服務安裝完成。
然后我們到windows服務里面啟動該服務。
啟動服務后,在需要引用該服務的類庫或者exe程序上添加服務引用,路徑為我們App.config里面的 <add baseAddress="http://localhost:8000/ServiceModelSamples/service"/>基地址
點擊轉到,出現CalculatorService說明我們配置成功。
注:
安裝 Windows 服務: installutil bin\service.exe
卸載Windows服務: installutil /u bin\service.exe 或者 sc delete 服務名
啟動Windows服務: net start WCFWindowsServiceSample
停止Windows服務: type net stop WCFWindowsServiceSample
windows服務的寄宿,參考msdn:https://msdn.microsoft.com/zh-cn/library/ms733069.aspx
本文地址:http://www.cnblogs.com/santian/p/4397235.html
博客地址:一天兩天三天