在WCF初探-8:WCF服務承載 (上)中,我們對宿主的概念、環境、特點做了文字性的介紹和概括,接下來我們將通過實例對這幾種寄宿方式進行介紹。為了更好的說明各寄宿環境特點,本實例采用Http和net.tcp兩種服務通訊方式,同時寄宿在不同的宿主中。程序結構如下:
服務契約的接口和實現代碼如下:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; namespace Service { [ServiceContract] public interface IServiceCalculator { [OperationContract] double Add(double n1, double n2); } }

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; namespace Service { [ServiceContract] public interface IServiceMessage { [OperationContract] string ReturnMessage(); } }

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Service { public class ServiceCalculator:IServiceCalculator { public double Add(double n1, double n2) { return n1 + n2; } } }

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Service { public class ServiceMessage:IServiceMessage { public string ReturnMessage() { return "調用服務計算結果如下"; } } }
在 IIS 中承載 WCF 服務和WAS 中承載 WCF 服務
1. 完成IISHost代碼
- 引用Service程序集
- 添加ServiceCalculator.svc新文件,代碼如下
<%@ ServiceHost Language="C#" Debug="true" Service="Service.ServiceCalculator" %>
- 添加ServiceCalculator.svc新文件,代碼如下
<%@ ServiceHost Language="C#" Debug="true" Service="Service.ServiceMessage" %>
- 配置服務文件代碼如下,這里我將配置兩個服務一個是ServiceMessage用於Http通訊,一個是ServiceCalculator用於net.tcp通訊,如果不清楚服務配置,請參照WCF初探-6:WCF服務配置和WCF初探-7:WCF服務配置工具使用

<?xml version="1.0" encoding="utf-8"?> <configuration> <system.serviceModel> <services> <service name="Service.ServiceMessage" behaviorConfiguration="mexBehavior"> <endpoint address="" binding="wsHttpBinding" contract="Service.IServiceMessage" /> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> </service> <service name="Service.ServiceCalculator" behaviorConfiguration="mexBehavior"> <endpoint address="" binding="netTcpBinding" bindingConfiguration="PortSharingBinding" contract="Service.IServiceCalculator" /> <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" /> </service> </services> <bindings> <netTcpBinding> <binding name="PortSharingBinding" portSharingEnabled="true"> <security mode="None" /> </binding> </netTcpBinding> </bindings> <behaviors> <serviceBehaviors> <behavior name="mexBehavior"> <serviceMetadata httpGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="false" /> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel> </configuration>
2. 寄宿服務
- 生成IISHost程序,將bin文件目錄、ServiceCalculator.svc、ServiceMessage.svc、Web.config拷貝到新建的WCFHost文件夾中
- 新建網站配置該程序以便承載服務。
-
點擊IIS菜單的應用程序池,找到WCFHost程序池,將.net framework版本設置為v4.0,托管管道模式設置為集成
-
在瀏覽器中輸入http://localhost:1234/ServiceMessage.svc可以看到服務發布成功
- 在瀏覽器中輸入http://localhost:1234/ServiceCalculator.svc可以看到服務寄宿失敗
這是因為ServiceCalculator.svc啟用的是net.tcp通訊,而在IIS中啟用net.tcp通訊就必須依靠Windows 進程激活服務(也稱為 WAS)
- 要使用WAS寄宿程序,就需要配置幾個地方
在控制面板->程序和功能->打開或關閉windows功能勾選以下幾個功能,安裝WCF 激活組件
配置承載服務的WCFHost網站,添加net.tcp通訊。
點擊網站的高級設置,在已啟用的協議后追加net.tcp協議
- 在瀏覽器中重新輸入http://localhost:1234/ServiceCalculator.svc,可以看到服務寄宿成功
3. 客戶端驗證服務
- 啟動Visual Studio 命令提示(2010)命令行工具,輸入wcftestclient命令調用WCF服務測試客戶端
在托管應用程序中承載 WCF 服務
1. 完成AppHost代碼
- 添加對service程序集的引用,配置文件App.config代碼如下

<?xml version="1.0" encoding="utf-8"?> <configuration> <system.serviceModel> <services> <service name="Service.ServiceMessage" behaviorConfiguration="mexBehavior"> <host> <baseAddresses> <add baseAddress="http://localhost:2234/ServiceMessage/"/> </baseAddresses> </host> <endpoint address="" binding="wsHttpBinding" contract="Service.IServiceMessage" /> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> </service> <service name="Service.ServiceCalculator" behaviorConfiguration="mexBehavior"> <host> <baseAddresses> <add baseAddress="http://localhost:1235/ServiceCalculator/"/> <add baseAddress="net.tcp://localhost:1234/ServiceCalculator"/> </baseAddresses> </host> <endpoint address="" binding="netTcpBinding" bindingConfiguration="PortSharingBinding" contract="Service.IServiceCalculator" > <identity> <dns value="localhost"/> </identity> </endpoint> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> </service> </services> <bindings> <netTcpBinding> <binding name="PortSharingBinding" portSharingEnabled="true" > <security mode="None" /> </binding> </netTcpBinding> </bindings> <behaviors> <serviceBehaviors> <behavior name="mexBehavior"> <serviceMetadata httpGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="false" /> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel> </configuration>
- Program.cs代碼如下

using System; using System.Collections.Generic; using System.Linq; using System.Text; using Service; using System.ServiceModel; namespace AppHost { class Program { static void Main(string[] args) { try { ServiceHost MessageHost = new ServiceHost(typeof(ServiceMessage)); ServiceHost CalculatorHost = new ServiceHost(typeof(ServiceCalculator)); MessageHost.Open(); CalculatorHost.Open(); Console.WriteLine("服務已經啟動。。。"); Console.ReadLine(); MessageHost.Close(); CalculatorHost.Close(); } catch (Exception ex) { Console.WriteLine(ex.Message); Console.Read(); } } } }
2. 寄宿服務
- 生成AppHost工程,找到bin目錄下的AppHost.exe,點擊運行,查看到服務寄宿成功
3. 客戶端驗證服務
- 啟動Visual Studio 命令提示(2010)命令行工具,輸入wcftestclient命令調用WCF服務測試客戶端。分別添加服務地址:
http://localhost:2234/ServiceMessage/
http://localhost:1235/ServiceCalculator/
在托管 Windows 服務中承載 WCF 服務
1. 完成NTHost代碼
- 添加windows服務程序services1.cs,在設計界面上單擊右鍵添加安裝程序ProjectInstaller.cs,在ProjectInstaller.cs設計界面上有serviceProcessInstaller1和serviceInstaller1兩個安裝組件,分別設置他們的屬性
-
添加配置文件App.config代碼,代碼如下:

<?xml version="1.0" encoding="utf-8"?> <configuration> <system.serviceModel> <services> <service name="Service.ServiceMessage" behaviorConfiguration="mexBehavior"> <host> <baseAddresses> <add baseAddress="http://localhost:2234/ServiceMessage/"/> </baseAddresses> </host> <endpoint address="" binding="wsHttpBinding" contract="Service.IServiceMessage" /> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> </service> <service name="Service.ServiceCalculator" behaviorConfiguration="mexBehavior"> <host> <baseAddresses> <add baseAddress="http://localhost:1235/ServiceCalculator/"/> <add baseAddress="net.tcp://localhost:1234/ServiceCalculator"/> </baseAddresses> </host> <endpoint address="" binding="netTcpBinding" bindingConfiguration="PortSharingBinding" contract="Service.IServiceCalculator" > <identity> <dns value="localhost"/> </identity> </endpoint> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> </service> </services> <bindings> <netTcpBinding> <binding name="PortSharingBinding" portSharingEnabled="true" > <security mode="None" /> </binding> </netTcpBinding> </bindings> <behaviors> <serviceBehaviors> <behavior name="mexBehavior"> <serviceMetadata httpGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="false" /> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel> </configuration>
- Service1.cs代碼如下:

using System.ServiceProcess; using Service; using System.ServiceModel; namespace NTHost { public partial class Service1 : ServiceBase { public Service1() { InitializeComponent(); } ServiceHost MessageHost = null; ServiceHost CalculatorHost = null; protected override void OnStart(string[] args) { MessageHost = new ServiceHost(typeof(ServiceMessage)); CalculatorHost = new ServiceHost(typeof(ServiceCalculator)); MessageHost.Open(); CalculatorHost.Open(); } protected override void OnStop() { MessageHost.Close(); CalculatorHost.Close(); MessageHost = null; CalculatorHost = null; } } }
2. 寄宿服務
- 生成NTHost工程,安裝windows服務程序NTHost.exe,在命令行中輸入
Cd C:\Windows\Microsoft.NET\Framework\v4.0.30319,回車后輸入installutil.exe 程序生成的bin目錄絕對地址\NTHost.exe –i,回車后安裝服務程序,程序注冊成功后啟動服務。
在開始菜單輸入services.msc命令,打開服務管理程序將NTServiceHost服務設置為啟動
3. 客戶端驗證服務
- 啟動Visual Studio 命令提示(2010)命令行工具,輸入wcftestclient命令調用WCF服務測試客戶端。分別添加服務地址:
http://localhost:2234/ServiceMessage/
http://localhost:1235/ServiceCalculator/