WCF 服務與 ASP.NETWCF Services and ASP.NET

本主題討論裝載 Windows Communication Foundation (WCF) 服務的並行與 ASP.NET 裝載,並且在 ASP.NET 相容性模式中。This topic discusses hosting Windows Communication Foundation (WCF) services side-by-side with ASP.NET and hosting them in ASP.NET compatibility mode.

將 WCF 與 ASP.NET 並存裝載Hosting WCF Side-by-Side with ASP.NET

網際網路資訊服務 (IIS) 裝載的 WCF 服務可以位於與。ASPX 頁面和 ASMX Web 服務位於單一、 共同的應用程式定義域。WCF services hosted in Internet Information Services (IIS) can be located with .ASPX pages and ASMX Web services inside of a single, common Application Domain. ASP.NET 提供通用的基礎結構服務,例如 AppDomain 管理與 WCF 和 ASP.NET HTTP 執行階段動態編譯。ASP.NET provides common infrastructure services such as AppDomain management and dynamic compilation for both WCF and the ASP.NET HTTP runtime. WCF 的預設組態是由並行與 ASP.NET。The default configuration for WCF is side-by-side with ASP.NET.

WCF 服務和 ASP.NET: 共用狀態WCF Services and ASP .NET: sharing state

ASP.NET HTTP 執行階段會處理 ASP.NET 要求,但不會參與處理要求的 WCF 服務,即使這些服務裝載於相同的 AppDomain 因為 ASP.NET 內容。The ASP.NET HTTP runtime handles ASP.NET requests but does not participate in the processing of requests destined for WCF services, even though these services are hosted in the same AppDomain as is the ASP.NET content. 相反地,WCF 服務模型會攔截訊息定址到 WCF 服務,並透過 WCF 傳輸/通道堆疊路由傳送。Instead, the WCF Service Model intercepts messages addressed to WCF services and routes them through the WCF transport/channel stack.

並存模型的結果如下:The results of the side-by-side model are as follows:

  • ASP.NET 和 WCF 服務可以共用 AppDomain 狀態。ASP.NET and WCF services can share AppDomain state. 因為這兩個架構可以共存於相同的 AppDomain,WCF 也可以共用 AppDomain 狀態 (包括靜態變數、 事件等等) 的 asp.net。Because the two frameworks can coexist in the same AppDomain, WCF can also share AppDomain state with ASP.NET (including static variables, events, and so on).

  • WCF 服務的行為一致、 獨立的裝載環境和傳輸。WCF services behave consistently, independent of hosting environment and transport. ASP.NET HTTP 執行階段主要用來與 IIS/ASP.NET 裝載環境和 HTTP 通訊搭配使用。The ASP.NET HTTP runtime is intentionally coupled to the IIS/ASP.NET hosting environment and HTTP communication. 相反地,WCF 旨在各種裝載環境有一致的行為 (WCF 行為一致兩者 IIS 內部與外部) 與各種傳輸 (裝載於 IIS 7.0 和更新版本的服務會公開時,所有端點都有一致的行為即使一部分的這些端點使用 HTTP 以外的通訊協定)。Conversely, WCF is designed to behave consistently across hosting environments (WCF behaves consistently both inside and outside of IIS) and across transport (a service hosted in IIS 7.0 and later has consistent behavior across all endpoints it exposes, even if some of those endpoints use protocols other than HTTP).

  • 在 AppDomain 中,HTTP 執行階段所實作的功能會套用到 ASP.NET 內容,但不是至 WCF。Within an AppDomain, features implemented by the HTTP runtime apply to ASP.NET content but not to WCF. 許多 HTTP 特定的 ASP.NET 應用程式平台的功能不適用於裝載於 AppDomain,其中包含 ASP.NET 內容內部的 WCF 服務。Many HTTP-specific features of the ASP.NET application platform do not apply to WCF Services hosted inside of an AppDomain that contains ASP.NET content. 下列為這些功能的範例:Examples of these features include the following:

    • HttpContext:Current一律null時從 WCF 服務內存取。HttpContext: Current is always null when accessed from within a WCF service. 使用RequestContext改為。Use RequestContext instead.

    • 檔案為基礎的授權: WCF 安全性模型不允許存取控制清單 (ACL) 套用至服務的.svc 檔案中,決定是否授權服務要求時。File-based authorization: The WCF security model does not allow for the access control list (ACL) applied to the .svc file of the service when deciding if a service request is authorized.

    • 以組態為基礎的 URL 授權: 同樣地,WCF 安全性模型不會遵守任何在 System.Web 之指定的 URL 為基礎的授權規則<授權 > 組態項目。Configuration-based URL Authorization: Similarly, the WCF security model does not adhere to any URL-based authorization rules specified in System.Web’s <authorization> configuration element. 如果服務駐留在受到 ASP URL 空間 WCF 要求會忽略這些設定。網路的 URL 授權規則。These settings are ignored for WCF requests if a service resides in a URL space secured by ASP.NET’s URL authorization rules.

    • HttpModule 擴充性: WCF 裝載基礎結構會攔截 WCF 要求時PostAuthenticateRequest,就會引發事件,並不會傳回至 ASP.NET HTTP 管線處理。HttpModule extensibility: The WCF hosting infrastructure intercepts WCF requests when the PostAuthenticateRequest event is raised and does not return processing to the ASP.NET HTTP pipeline. 會被編碼為管線的階段攔截要求的模組不會攔截 WCF 要求。Modules that are coded to intercept requests at later stages of the pipeline do not intercept WCF requests.

    • ASP.NET 模擬: 根據預設,WCF 要求一律執行為 IIS 處理序識別,即使 ASP.NET 設定,讓使用 System.Web 的模擬<y impersonate ="true"/ > 組態選項。ASP.NET impersonation: By default, WCF requests always runs as the IIS process identity, even if ASP.NET is set to enable impersonation using System.Web’s <identity impersonate="true" /> configuration option.

這些限制僅適用於 IIS 應用程式中裝載的 WCF 服務。These restrictions apply only to WCF services hosted in IIS application. ASP.NET 內容的行為不受 WCF 存在。The behavior of ASP.NET content is not affected by the presence of WCF.

需要傳統上由 HTTP 管線提供功能的 WCF 應用程式應該考慮使用 WCF 對等用法,會受到主機與傳輸影響:WCF applications that require functionality traditionally provided by the HTTP pipeline should consider using the WCF equivalents, which are host and transport independent:

或者,您可以考慮在 WCF 的 ASP.NET 相容性模式中執行您的服務。Alternatively, you can consider running your services in WCF’s ASP.NET compatibility mode.

在 ASP.NET 相容性模式中裝載 WCF 服務Hosting WCF Services in ASP.NET Compatibility Mode

雖然 WCF 模型設計成各種裝載環境與傳輸有一致的行為,經常會有應用程式不需要這麼大的彈性的案例。Although the WCF model is designed to behave consistently across hosting environments and transports, there are often scenarios where an application does not require this degree of flexibility. WCF 的 ASP.NET 相容性模式是適用於不需要能夠裝載在 IIS 外部或透過 HTTP 以外的通訊協定進行通訊,但卻使用所有的 ASP.NET Web 應用程式平台功能的情況。WCF’s ASP.NET compatibility mode is suitable for scenarios that do not require the ability to host outside of IIS or to communicate over protocols other than HTTP, but that use all of features of the ASP.NET Web application platform.

不同於預設的並行設定,其中 WCF 裝載基礎結構會攔截 WCF 訊息,並將其傳送至 HTTP 管線外,在 ASP.NET 相容性模式中執行的 WCF 服務充分參與 ASP.NET HTTP 要求的生命週期。Unlike the default side-by-side configuration, where the WCF hosting infrastructure intercepts WCF messages and routes them out of the HTTP pipeline, WCF services running in ASP.NET Compatibility Mode participate fully in the ASP.NET HTTP request lifecycle. 在相容性模式中,WCF 服務會使用透過 HTTP 管線IHttpHandler類似的方式要求的處理 ASPX 頁面和 ASMX Web 服務的實作。In compatibility mode, WCF services use the HTTP pipeline through an IHttpHandler implementation, similar to the way requests for ASPX pages and ASMX Web services are handled. 如此一來,WCF 運作方式完全相同 asmx 相對於在下列 ASP.NET 功能:As a result, WCF behaves identically to ASMX with respect to the following ASP.NET features:

  • HttpContext: 在 ASP.NET 相容性模式中執行 WCF 服務可以存取Current和其相關聯的狀態。HttpContext: WCF services running in ASP.NET Compatibility Mode can access Current and its associated state.

  • 檔案為基礎的授權: 在 ASP.NET 相容性模式中執行的 WCF 服務是安全的方法藉由附加至服務的.svc 檔案的檔案系統存取控制清單 (Acl)。File-based authorization: WCF services running in ASP.NET compatibility mode can be secure by attaching file system access control lists (ACLs) to the service’s .svc file.

  • 可設定 URL 授權: ASP。在 ASP.NET 相容性模式中執行 WCF 服務時,WCF 要求會強制執行網路的 URL 授權規則。Configurable URL authorization: ASP.NET’s URL authorization rules are enforced for WCF requests when the WCF service is running in ASP.NET Compatibility Mode.

  • HttpModuleCollection 擴充性: 在 ASP.NET 相容性模式中執行的因為 WCF 服務會充分參與 ASP.NET HTTP 要求的生命週期,在 HTTP 管線中設定任何 HTTP 模組是能夠之前和之後叫用服務,WCF 要求上運作。HttpModuleCollection extensibility: Because WCF services running in ASP.NET Compatibility Mode participate fully in the ASP.NET HTTP request lifecycle, any HTTP module configured in the HTTP pipeline is able to operate on WCF requests both before and after service invocation.

  • ASP.NET 模擬: WCF 服務執行使用目前的身分識別的 ASP.NET 模擬執行緒,如果已啟用 ASP.NET 模擬應用程式可能會與 IIS 處理序身分識別不同。ASP.NET Impersonation: WCF services run using the current identity of the ASP.NET impersonated thread, which may be different than the IIS process identity if ASP.NET impersonation has been enabled for the application. 如果同時針對特定服務作業啟用了 ASP.NET 模擬與 WCF 模擬,服務實作最終會執行使用 WCF 從取得的身分識別。If ASP.NET impersonation and WCF impersonation are both enabled for a particular service operation, the service implementation ultimately runs using the identity obtained from WCF.

WCF 的 ASP.NET 相容性模式會啟用應用程式層級透過下列組態 (位於應用程式的 Web.config 檔案中):WCF’s ASP.NET compatibility mode is enabled at the application level through the following configuration (located in the application’s Web.config file):

<system.serviceModel>  
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />  
</system.serviceModel>  

這個值預設為"true"若未指定。This value defaults to "true" if not specified. 此值設定為"false"表示所有應用程式中執行的 WCF 服務會在 ASP.NET 相容性模式中執行。Setting this value to "false" indicates that all WCF services running in the application will not run in ASP.NET Compatibility Mode.

由於 ASP.NET 相容性模式所隱含的要求處理語意在本質上不同於 WCF 預設值,個別的服務實作能夠控制是否針對 ASP.NET 應用程式裡面執行已啟用相容性模式。Because ASP.NET Compatibility Mode implies request processing semantics that are fundamentally different from the WCF default, individual service implementations have the ability to control whether they run inside of an application for which ASP.NET Compatibility Mode has been enabled. 服務可以透過 AspNetCompatibilityRequirementsAttribute 來指出是否支援 ASP.NET 相容性模式。Services can use the AspNetCompatibilityRequirementsAttribute to indicate whether they support ASP.NET Compatibility Mode. 這個屬性的預設值為 AllowedThe default value for this attribute is Allowed.

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

public class CalculatorService : ICalculatorSession

{//Implement calculator service methods.}

下表說明整個應用程式相容性模式設定如何與個別服務所陳述的支援層級互動:The following table illustrates how the application-wide compatibility mode setting interacts with the individual service’s stated level of support:

整個應用程式的相容性模式設定Application-wide Compatibility Mode setting [AspNetCompatibilityRequirementsMode][AspNetCompatibilityRequirementsMode]

設定Setting
觀察結果Observed Result
aspNetCompatibilityEnabled ="true"aspNetCompatibilityEnabled = "true" Required 服務成功啟動。Service activates successfully.
aspNetCompatibilityEnabled ="true"aspNetCompatibilityEnabled = "true" Allowed 服務成功啟動。Service activates successfully.
aspNetCompatibilityEnabled ="true"aspNetCompatibilityEnabled = "true" NotAllowed 當服務接收訊息時,發生啟動錯誤。An activation error occurs when the service receives a message.
aspNetCompatibilityEnabled ="false"aspNetCompatibilityEnabled = "false" Required 當服務接收訊息時,發生啟動錯誤。An activation error occurs when the service receives a message.
aspNetCompatibilityEnabled ="false"aspNetCompatibilityEnabled = "false" Allowed 服務成功啟動。Service activates successfully.
aspNetCompatibilityEnabled ="false"aspNetCompatibilityEnabled = "false" NotAllowed 服務成功啟動。Service activates successfully.

注意

IIS 7.0 和 WAS 允許 WCF 服務透過 HTTP 以外的通訊協定進行通訊。IIS 7.0 and WAS allows WCF services to communicate over protocols other than HTTP. 不過,在應用程式啟用 ASP.NET 相容性模式中執行的 WCF 服務不允許公開非 HTTP 端點。However, WCF services running in applications that have enabled ASP.NET compatibility mode are not permitted to expose non-HTTP endpoints. 此類組態會在服務接收其第一則訊息時,產生啟動例外狀況。Such a configuration generates an activation exception when the service receives its first message.

如需有關如何啟用 WCF 服務的 ASP.NET 相容性模式的詳細資訊,請參閱AspNetCompatibilityRequirementsModeASP.NET 相容性範例。For more information about enabling ASP.NET compatibility mode for WCF services, see AspNetCompatibilityRequirementsMode and the ASP.NET Compatibility sample.

另請參閱See Also

AspNetCompatibilityRequirementsAttribute
Windows Server App Fabric 裝載功能Windows Server App Fabric Hosting Features