標籤:style blog http io ar color os 使用 sp
摘要 : 從IIS 7 開始, IIS增加了對非HTTP協議的支援. 因此, 自IIS 7之後, 可以將NetTcpBinding等非HTTP協議的Bindings直接寄宿在IIS上面. 本文將介紹如何在IIS上配置WCF NetTcpBinding, 並且對其工作的方式進行比較深入的討論.
Windows Process Activation Service
是IIS監聽在HTTP協議上的工作方式. 對HTTP協議的監聽是在核心模式下的HTTP.sys的協助下完成
從IIS7開始,IIS還加入了對非HTTP協議的支援。對於那些採用非HTTP協議,但是又需要部署在IIS裡面,從而利用IIS優秀的管理功能的服務來說,比如WCF服務,可謂一大福音。IIS7可以支援多種非HTTP協議,比如net.tcp,net.msmq, net.pipe等。由於HTTP.sys並不會監聽非HTTP協議的連接埠. 對於非HTTP協議, 則有各自的Windows Serivce來進行監聽. 例如Net.Tcp協議, 則是由Net.Tcp Port Sharing Service和Net.Tcp Listener Adapter服務進行監聽, 並且寄宿在SMSvcHost.exe中.
進過拆分之後, WAS不僅處理HTTP請求,還可以處理非HTTP協議的請求。HTTP請求是由Http.sys截獲的,並且在傳遞給WAS之前,就已經傳遞給w3svc中的HTTP管理器,但是,其他請求都是通過WAS接聽程式的適配器介面轉寄給組態管理員和進程管理器的,而沒有經過w3svc。
關於 WAS的介紹可以參考這裡 :
http://msdn.microsoft.com/en-us/library/ms734677(v=vs.110).aspx
http://blogs.msdn.com/b/swiss_dpe_team/archive/2008/02/08/iis-7-support-for-non-http-protocols.aspx
為了讓IIS支援net.tcp,必須先安裝WAS(Windows Process Activation Service),即windows進程啟用服務。
開啟控制台--程式和功能--開啟或關閉windows功能,安裝WAS,
安裝完畢後在Services視窗中可以到到如下服務:Windows Process Activation Service;Net.Msmq Listener Adapter;Net.Pipe Listener Adapter;Net.Tcp Listener Adapter;Net.Tcp Port Sharing Service.這幾個服務。確定Net.Tcp Listener Adapter 與Net.Tcp Port Sharing Service是否已經啟動。
同樣, 也需要檢查WCF是否啟用了Non-Http的支援. .Net Framework 3.5和 .Net Framework 4.5分別有各自的支援.
不過上述的Services只會存在一個, 如果伺服器上只有.Net Framework 3.5本版的被啟用, 則這些Services則只能使用3.5版本的DLL. 如果.Net Framework 4.5版本一旦被啟用, 無論是否同時啟用了3.5版本, 則會使用4.5版本. 可以通過服務所使用的SMSvcHost.exe來判斷實際使用了那一個版本.
IIS 上的配置
在IIS中,選中你的網站,然後在右邊的操作功能表列中單擊綁定,會彈出一個“網站綁定”視窗,點擊添加,類型選擇net.tcp
選擇你的網站,點擊“進階設定”,彈出的的表單中,在“已啟用的協議”一欄中手動添加:net.tcp. 做完這一步之後, 該網站即增加了對net.tcp協議的支援.
樣本
樣本是用VS2012建立的WCF模板.
1 public class Service1 : IService1 2 { 3 public string GetData(int value) 4 { 5 return string.Format("You entered: {0}", value); 6 } 7 8 public CompositeType GetDataUsingDataContract(CompositeType composite) 9 {10 if (composite == null)11 {12 throw new ArgumentNullException("composite");13 }14 if (composite.BoolValue)15 {16 composite.StringValue += "Suffix";17 }18 return composite;19 }20 }
下面是使用了NetTcpBinding的WCF Service的配置.
<?xml version="1.0"?><configuration> <appSettings> <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" /> </appSettings> <system.web> <compilation debug="true" targetFramework="4.5" /> <httpRuntime targetFramework="4.5"/> </system.web> <system.serviceModel> <bindings> <netTcpBinding> <binding name="NewBinding1" /> </netTcpBinding> </bindings> <services> <service name="NetTcpSite.Service1"> <endpoint binding="netTcpBinding" bindingConfiguration="NewBinding1" contract="NetTcpSite.IService1" /> <endpoint address="mex" binding="mexTcpBinding" bindingConfiguration="" contract="IMetadataExchange" /> </service> </services> <behaviors> <serviceBehaviors> <behavior> <!-- To avoid disclosing metadata information, set the values below to false before deployment --> <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/> <!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information --> <serviceDebug includeExceptionDetailInFaults="false"/> </behavior> </serviceBehaviors> </behaviors> <protocolMapping> <add binding="basicHttpsBinding" scheme="https" /> </protocolMapping> <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /> </system.serviceModel> <system.webServer> <modules runAllManagedModulesForAllRequests="true"/> <directoryBrowse enabled="true"/> </system.webServer></configuration>
由於WCF 4.5對預設配置進行了最佳化. 所以即使沒有關於HTTP協議的任何配置, WCF Service預設情況下也能通過瀏覽器的方式訪問到WSDL.
如果要訪問NetTcpBinding的WCF Service則需要一個同樣採用Net.TCP協議的用戶端. 這裡, 我使用了VS 2012來完成.
下面是WCF用戶端的的配置和代碼.
class Program { static void Main(string[] args) { using (NetTcpService.Service1Client proxy = new Service1Client()) { Console.WriteLine(proxy.GetData(10)); } Console.ReadLine(); } }
<?xml version="1.0" encoding="utf-8" ?><configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> <system.serviceModel> <bindings> <netTcpBinding> <binding name="NetTcpBinding_IService1" /> </netTcpBinding> </bindings> <client> <endpoint address="net.tcp://localhost/NetTcpSite/Service1.svc" binding="netTcpBinding" bindingConfiguration="NetTcpBinding_IService1" contract="NetTcpService.IService1" name="NetTcpBinding_IService1"> <identity> <servicePrincipalName value="host/LIGHTWEAPON.fareast.corp.microsoft.com" /> </identity> </endpoint> </client> </system.serviceModel></configuration>
我們進一步來觀察是哪一個進程監聽在Net.Tcp佔用的808連接埠上. 從上可以驗證出, 監聽在808連接埠上的進程是2060. 這個進程ID是SMSvcHost.exe, 也就是上面提到的各種Services的宿主進程.
WCF : 如何將NetTcpBinding寄宿在IIS7上