Channels ▼
RSS

Web Development

WCF Web Programming Model



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.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 
Dr. Dobb's TV