Posted by: Cirilo Meggiolaro | 04/28/2009

Tip of the day #196 – Enabling a WCF service to run in ASP.NET compatibility mode

By default a WCF service exposed via http protocol is not enabled to use some features from the ASP.NET pipeline such as file and url authorization and session state. That happens because WCF services have two different run modes:

  • Mixed transport mode: This is the default mode and it is completely independent of the transport and environment;
  • ASP.NET compatibility mode: Allows the WCF service to use the features from the ASP.NET pipeline.

How to…

To achieve that you may define in the web.config the aspNetCompatibilityEnabled attribute flag from the serviceHostingEnvironment element as shown below:

     <serviceHostingEnvironment aspNetCompatibilityEnabled=”true”/>

In your service implementation you must use the System.ServiceModel.Activation.AspNetCompatibilityRequirements attribute to check whether the service is running in compatibility mode or not before the execution of the method body.

The code snippet below shows a service implementation that requires the compatibility mode to be enabled. Since it’s enabled the code will use a value stored in a session object:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
public class FooService : IFoo
    public decimal M()
        if (HttpContext.Current.Session[“StoredValue”] != null)
            return (decimal)HttpContext.Current.Session[“StoredValue”];

        return 0.0M;


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s


%d bloggers like this: