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 也可以使用 (包括靜態變數、 事件等等) 的 ASP.NET 共用 AppDomain 狀態。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. ASP.NET 應用程式平台的許多 HTTP 特定功能並不適用於在 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 安全性模型不允許套用至服務的.svc 檔案,決定是否被授權服務要求時存取控制清單 (ACL)。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 要求會忽略這些設定。NET 的 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 的<identity 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 服務時,NET 的 URL 授權規則會強制執行的 WCF 要求。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 模擬執行緒,可能會不同於 IIS 處理序身分識別,如果應用程式啟用 ASP.NET 模擬。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 ="trueaspNetCompatibilityEnabled = "true" Required 服務成功啟動。Service activates successfully.
aspNetCompatibilityEnabled ="trueaspNetCompatibilityEnabled = "true" Allowed 服務成功啟動。Service activates successfully.
aspNetCompatibilityEnabled ="trueaspNetCompatibilityEnabled = "true" NotAllowed 當服務接收訊息時,發生啟動錯誤。An activation error occurs when the service receives a message.
aspNetCompatibilityEnabled ="falseaspNetCompatibilityEnabled = "false" Required 當服務接收訊息時,發生啟動錯誤。An activation error occurs when the service receives a message.
aspNetCompatibilityEnabled ="falseaspNetCompatibilityEnabled = "false" Allowed 服務成功啟動。Service activates successfully.
aspNetCompatibilityEnabled ="falseaspNetCompatibilityEnabled = "false" NotAllowed 服務成功啟動。Service activates successfully.

注意

IIS 7.0 和 WAS 允許透過 HTTP 以外的通訊協定進行通訊的 WCF 服務。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