Rick Strahl's Weblog  

Wind, waves, code and everything in between...
.NET • C# • Markdown • WPF • All Things Web
Contact   •   Articles   •   Products   •   Support   •   Advertise
Sponsored by:
West Wind WebSurge - Rest Client and Http Load Testing for Windows

AspNetCompatibility in WCF Services – easy to trip up

On this page:

This isn’t the first time I’ve hit this particular wall: I’m creating a WCF REST service for AJAX callbacks and using the WebScriptServiceHostFactory host factory in the service:

<%@ ServiceHost Language="C#" 
        Factory="System.ServiceModel.Activation.WebScriptServiceHostFactory" %>


to avoid all configuration. Because of the Factory that creates the ASP.NET Ajax compatible format via the custom factory implementation I can then remove all of the configuration settings that typically get dumped into the web.config file. However, I do want ASP.NET compatibility so I still leave in:

  <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>

in the web.config file. This option allows you access to the HttpContext.Current object to effectively give you access to most of the standard ASP.NET request and response features. This is not recommended as a primary practice but it can be useful in some scenarios and in backwards compatibility scenerios with ASP.NET AJAX Web Services.

Now, here’s where things get funky. Assuming you have the setting in web.config, If you now declare a service like this:

    [ServiceContract(Namespace = "DevConnections")]
#if DEBUG 
    [ServiceBehavior(IncludeExceptionDetailInFaults = true)]
    public class BasicWcfService

(or by using an interface that defines the service contract)

you’ll find that the service will not work when an AJAX call is made against it. You’ll get a 500 error and a System.ServiceModel.ServiceActivationException System error. Worse even with the IncludeExceptionDetailInFaults enabled you get absolutely no indication from WCF what the problem is.

So what’s the problem?  The issue is that once you specify aspNetCompatibilityEnabled=”true” in the configuration you *have to* specify the AspNetCompatibilityRequirements attribute and one of the modes that enables or at least allows for it. You need either Required or Allow:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]

without it the service will simply fail without further warning.

It will also fail if you set the attribute value to NotAllowed. The following also causes the service to fail as above:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.NotAllowed)]

This is not totally unreasonable but it’s a difficult issue to debug especially since the configuration setting is global – if you have more than one service and one requires traditional ASP.NET access and one doesn’t then both must have the attribute specified. This is one reason why you’d want to avoid using this functionality unless absolutely necessary. WCF REST provides some basic access to some of the HTTP features after all, although what’s there is severely limited.

I also wish that ServiceActivation errors would provide more error information. Getting an Activation error without further info on what actually is wrong is pretty worthless especially when it is a technicality like a mismatched configuration/attribute setting like this.

Posted in ASP.NET  WCF  AJAX  

The Voices of Reason


April 12, 2010

# re: AspNetCompatibility in WCF Services &ndash; easy to trip up

Good WCF 'gotcha' post! WCF is awesome but the myriad of configurations possible make it a bit tedious at times. I am in the process of converting a bunch of ASMX's to WCF and have run into my fair share of "Why doesn't this work?", then I change a setting or two and it just works.

April 14, 2010

# re: AspNetCompatibility in WCF Services &ndash; easy to trip up

Usually the Windows event log gives a more detailed description of why things went wrong when a ServiceActivationException occurs (or any other vague internal WCF exceptions). I hit several weird WCF issues during development and the event log always contained the necessary info to solve the issue.

Note that you even need to add the AspNetCompatibilityRequirements attribute when your service isn't available on a http binding. I had a service that could only be reached via net.tcp but even then you have to add this attribute. Otherwise it fails with the same error.

April 19, 2010

# AspNetCompatibility in WCF Services – easy to trip up - Rick Strahl's Web Log

Thank you for submitting this cool story - Trackback from PimpThisBlog.com

June 07, 2010

# re: AspNetCompatibility in WCF Services &ndash; easy to trip up

Hi Rick,

I have an ajax-enabled WCF service that sometimes does not return to either the success or failure callback. I set up tracing and notice that I'm getting 1 less message when it fails to send back. I'm stumped at this point. I don't know where to look to further investigate the issue to see where the failure is happening.

Basically, I'm returning a list of objects which is being serialized as json. When the object list is small it pretty much always returns. If the list is larger (say around 300 items) then it will only periodically return results. I may need to call the javascript function 4 or 5 times before the call successfully reaches the "OnSuccess" function. However, the server side service functions are executed on every single call. Any insights you could help with would be much appreciated.


Scott Hannen
June 26, 2014

# re: AspNetCompatibility in WCF Services &ndash; easy to trip up

Thank you. Thank you very, very, very much. Really, thank you.

November 25, 2015

# re: AspNetCompatibility in WCF Services &ndash; easy to trip up

Thanks a lot man.

For VB NET this may help too:

West Wind  © Rick Strahl, West Wind Technologies, 2005 - 2024