ASP.NET Runtime Support for WCF Services

Service orientation describes platform-neutral and message-based software, making HTTP an ideal protocol for service-oriented applications and ASP.NET the ideal platform for hosting true service-oriented WCF applications. WCF services can be deployed in an ASP.NET application running in side-by-side mode, which means that they execute outside the ASP.NET integrated pipeline, or WCF can run as an integrated part of the ASP.NET pipeline through ASP.NET Compatibility mode. ASP.NET Compatibility mode enables integration with ASP.NET platform services, but as a side effect can also introduce tight coupling with the ASP.NET runtime. Although these conditions can be less than ideal for intranet software, where you might want to use TCP/IP for performance reasons, the HTTP protocol is an ideal transport for Web services deployed in a true service-oriented fashion to remote clients not tied to a specific platform. ASP.NET Compatibility mode has nothing to do with consumers of the service; it only specifies the compatibility level of the service runtime. When compatibility mode is enabled in the ASP.NET web.config file, WCF services run in ASP.NET Integrated Pipeline mode. ASP.NET Integrated Pipeline mode lets the service run within the ASP.NET pipeline and have full access to HTTP runtime objects through the HttpContext.Current property. For example, you might want to use ASP.NET authentication and the HttpContext.Current.User property to access the ASP.NET user principal, or you might want direct access to HTTP headers within certain Web services. When developing services for an AJAX application, it’s ideal to run in ASP.NET Compatibility mode.

Tip

Tip

Tying your service to the HTTP protocol does not detract from its service-oriented nature because the HTTP protocol is the one protocol that can cross nearly all platform and network boundaries with ease.

To set WCF to enable compatibility mode and run in the ASP.NET integrated pipeline, include the serviceHostingEnvironment element with the aspNetCompatibilityEnabled attribute set to true. Here is an example:

<system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
    <!-- WCF configuration here. -->
</system.serviceModel>

Tip

Tip

The requirement for an explicit compatibility setting is built into WCF to maintain a loose coupling with the transport mechanism. WCF doesn’t have to use the HTTP transport. It can also use TCP, MSMQ, or other transport protocols. When developing primarily for the ASP.NET AJAX application client, you can add this dependency on the ASP.NET runtime by enabling or even requiring ASP.NET Compatibility mode.

Without ASP.NET Compatibility mode, a WCF service running in the ASP.NET application does not run within the ASP.NET HTTP pipeline. WCF services run in side-by-side mode. The Web request is intercepted by the WCF runtime and processed completely outside the ASP.NET HTTP pipeline. In side-by-side mode, HttpContext.Current is always null, and the WCF service cannot access the ASP.NET infrastructure. If nothing is specified in web.config, side-by-side is the default mode.

For your service to run in ASP.NET Compatibility mode, it must be marked with the AspNetCompatibilityRequirements attribute. The following attribute applied to a service class (the implementation class, not the interface) allows the service to be activated in ASP.NET Compatibility mode:

[AspNetCompatibilityRequirements(
    RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

Possible values for RequirementsMode are Allowed, NotAllowed, and Required. You want to use the value Allowed if your service supports activation in ASP.NET Integrated Pipeline mode and in side-by-side execution outside the ASP.NET pipeline. Allowed is the preferred configuration for maximum flexibility. However, if your service’s execution requires the ASP.NET runtime (for example, through extensive use of the HTTP protocol), use the Required value. Finally, if you develop a service that won’t be compatible with the ASP.NET integrated HTTP pipeline, use the NotAllowed value to be explicit. If you do not specify ASP.NET compatibility requirements, the default behavior is the same as specifying NotAllowed.

Important

Important

You must allow ASP.NET compatibility through the AspNetCompatibilityRequirements attribute on the service class (not the service interface, as other WCF attributes). If you don’t, the service will not be activated and will throw an exception.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset