WCF Web Programming Model

New binding capabilities lets WCF support JSON serialization over HTTP


March 14, 2008
URL:http://www.drdobbs.com/architecture-and-design/wcf-web-programming-model/206903700


Starting with ASP.NET 3.5, you can use Windows Communication Foundation (WCF) to build AJAX-callable services. Why weren't WCF services available in ASP.NET AJAX pages before ASP.NET 3.5? The reason is that before .NET 3.5 the WCF platform had no built-in support for taking JSON as input and returning it in output.

The WCF platform that ships with the .NET Framework 3.5 comes with a new binding model aptly named "webHttpBinding." At the end of the day, this new binding empowers WCF to support JSON serialization over HTTP. In addition, it lets you map a URI of your choice to methods and set the format of the message's body and response.

The WCF Web programming model is specifically designed to enable WCF calls from Web clients running JavaScript. But what happens on the server, exactly?

ASP.NET and WCF are co-located in IIS, but it is ASP.NET to receive any calls directed at a WCF service method. Next, the ASP.NET runtime forwards WCF requests to the WCF stack. ASP.NET and WCF services live side by side within the same AppDomain inside of an instance of the worker process.

Some ASP.NET features are not available to WCF services when these services are hosted in IIS. The reason is that WCF services behave independently of the hosting environment and transportation protocols, whereas ASP.NET is intentionally tightly coupled to the IIS environment and HTTP-based communication. The behavior of ASP.NET content is not affected by the presence of WCF; but the overall behavior of WCF changes if it has to take into account the presence of ASP.NET. To work in collaboration with ASP.NET, the WCF runtime must be configured to operate in compatibility mode.

When a WCF service is not working in ASP.NET compatibility mode, it cannot access the HttpContext of the ASP.NET request. The object, in fact, is always null. At the same time, you should note that the WCF runtime supplies the OperationContext object with nearly the same purpose. In addition, no file-based authorization is possible on SVC files if not in compatibility mode and no web.config-based authorization. You can make up for this using the WCF-specific ServiceAuthorization behavior.

WCF requests are intercepted immediately after authentication and never returned to the ASP.NET pipeline. Again, for ASP.NET to control the processing of the WCF request, you need to switch to compatibility mode. Finally, let's talk impersonation. A WCF request always runs through the IIS process identity regardless of ASP.NET impersonation. ASP.NET impersonation settings are taken into account only in compatibility mode. When in compatibility mode, though, WCF impersonation settings, if specified, take precedence.

ASP.NET compatibility mode is helpful when you design a WCF service that you'll never host outside of IIS and always use to communicate over the HTTP protocol. The compatibility mode must be enabled at the application level in the configuration file; it is disabled by default. This setting enables compatibility mode but then this setting affect individual services in a different way. Each WCF service can require, allow, or refuse the compatibility. This is done through the AspNetCompatibilityRequirements attribute set on the service class.

[AspNetCompatibilityRequirements(
    RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class MySampleService : ISampleContract 
{
    :
}

A WCF service that doesn't allow ASP.NET compatibility cannot be invoked via JavaScript in an environment where compatibility is enabled. If you try this, an exception is thrown. It should be noted that a service doesn't allow compatibility mode by default. If compatibility is enabled at the application level, any hosted WCF services must change the value of AspNetCompatibilityRequirements attribute to either Required or Allowed.

Terms of Service | Privacy Statement | Copyright © 2024 UBM Tech, All rights reserved.