首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> 關於.NET

IIS 7.0 WAS 架構深度解析:裝載非 HTTP 協定的 WCF 服務完整指南

     編輯:關於.NET

Windows Process Activation Service (WAS) 是 IIS 7.0 核心元件,將啟動程序從 HTTP 綁定解耦,支援 TCP、Named Pipes、MSMQ 等非 HTTP 協定裝載 WCF 服務,同時保留 IIS 的進程管理、回收、健康監控等企業級特性。

IIS 6.0 vs 7.0 架構演進

元件IIS 6.0IIS 7.0 (WAS)
監聽器w3svc (僅 HTTP)w3svc (HTTP) + SMSvcHost.exe (TCP/Pipe/MSMQ)
啟動管理w3svc 內建WAS (獨立 NT 服務)
設定儲存Metabase (XML)applicationHost.config
非 HTTP 支援無原生支援

WAS 核心三大模組

  • Configuration Manager:讀取 applicationHost.config 應用程式集區與站點設定。
  • Process Manager:對應集區至工作進程 (w3wp.exe),依需求啟動新實例。
  • Listener Adapter Interface:非託管介面,讓外部監聽器 (如 WCF 啟動服務) 將啟動請求提交給 WAS。

非 HTTP 啟動流程 (以 net.tcp 為例)

  1. 監聽器啟動:NetTcpActivator (在 SMSvcHost.exe) 向 WAS 註冊,取得應用程式位址與 Listener Channel ID。
  2. 接收訊息:監聽器在 TCP 埠接收幀,解析目標 URI,對應至 Listener Channel ID。
  3. 請求啟動:呼叫 WebhostOpenListenerChannelInstance,傳遞 Channel ID 與資料 Blob (含應用程式鍵)。
  4. 工作進程初始化:
    • WAS 啟動 w3wp.exe → 載入 CLR (透過 aspnet_isapi.dll) → 建立預設 AppDomain。
    • Process Protocol Handler (PPH) 讀取 web.config 協定處理常設定 (如 net.tcp 對應 TcpProcessProtocolHandler)。
    • PPH 透過 IAdphManager 啟動應用程式專屬 AppDomain 與 AppDomain Protocol Handler (ADPH)。
  5. 資料傳遞:監聽器將訊息直接傳給已啟動的 AppDomain (WAS 不參與訊息傳遞),WCF 管道接手處理。

裝載 WCF 服務實作步驟

1. 服務與 .svc 端點

[ServiceContract] public interface IDemoService { string Ping(string data); }
public class DemoService : IDemoService { public string Ping(string d) => "pinged with: " + d; }


<% @ServiceHost Service="DemoService" %>

2. Web.config 設定 (HTTP 端點)

<system.serviceModel>
  <services>
    <service name="DemoService" behaviorConfiguration="Behavior">
      <endpoint address="" binding="wsHttpBinding" contract="IDemoService" />
    </service>
  </services>
  <behaviors>
    <serviceBehaviors>
      <behavior name="Behavior">
        <serviceMetadata httpGetEnabled="true" />
      </behavior>
    </serviceBehaviors>
  </behaviors>
</system.serviceModel>

3. 啟用非 HTTP 協定 (applicationHost.config)

<site name="Default Web Site">
  <bindings>
    <binding protocol="http" bindingInformation="*:80:" />
    <binding protocol="net.tcp" bindingInformation="808:*" />
    <binding protocol="net.pipe" bindingInformation="*" />
    <binding protocol="net.msmq" bindingInformation="localhost" />
  </bindings>
  <application path="/WasDemo" enabledProtocols="http,net.tcp,net.pipe,net.msmq">
    <virtualDirectory path="/" physicalPath="C:\WasDemo" />
  </application>
</site>

4. 服務端點擴充

<endpoint address="" binding="netTcpBinding" contract="IDemoService" />
<endpoint address="" binding="netNamedPipeBinding" contract="IDemoService" />
<endpoint address="net.msmq://localhost/private/wasdemo/service.svc"
          binding="netMsmqBinding" contract="IServiceMsmq" />

生命週期管理與自動化部署

  • WAS 依閒置時間 (預設 20 分鐘) 回收 AppDomain;組態變更、重新編譯超過閾值亦觸發回收。
  • 不適合有狀態服務 (Session/單例);最適合 InstanceContextMode.PerCall 無狀態服務。
  • 使用 ServiceHostFactoryBase 自訂 Factory 掛接 Opening/Closing 事件處理啟動/關閉邏輯。
  • 自動化工具:appcmd.exe、Microsoft.Web.Administration (ServerManager API)、PowerShell/WMI。

擴充性與限制

  • WAS 可擴充支援自訂傳輸 (需實作 PPH/ADPH/監聽器),SDK 提供 UDP 範例。
  • Windows Vista 連線限制:Ultimate/Business 為 10,Home 版為 3;Server 2008 無限制。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved