Why does AspNetCompatibilityRequirementsMode.Allowed fix this error?

Posted on

Problem :

I was searching around trying to solve a problem I am having with WCF. I am very new to WCF so I wasn’t sure exactly what was going on.

I am using Visual Studio 2010 and did New Web Site->WCF Service. I created my service and in the config file, if I set aspNetCompatibilityEnabled="true", I would get this error when going to the service through my web browser.

The service cannot be activated because it does not support ASP.NET compatibility.
ASP.NET compatibility is enabled for this application. Turn off ASP.NET compatibility mode in the web.config
or add the AspNetCompatibilityRequirements attribute to the service type with RequirementsMode 
setting as 'Allowed' or 'Required'.

I don’t understand what this means. Why aspNetCompatibilityEnabled="true" cause this error when [AspNetCompatibilityRequirements(RequirementsMode=AspNetCompatibilityRequirementsMode.Allowed)] fixes it.

To me, they sound like they do the same thing. Also, without that attribute silverlight was not able to call my WCF methods. Why is that?

Here is my config file if necessary:

<?xml version="1.0"?>

    <compilation debug="true" targetFramework="4.0" />
    <customErrors mode="Off"/>
        <binding name="LargeBuffer" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" />
      <service name="Services.Exporter">
        <endpoint address="" binding="basicHttpBinding" bindingConfiguration="LargeBuffer"
          contract="Services.IExporter" />
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
      multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true" />
    <modules runAllManagedModulesForAllRequests="true"/>

So my question is, why does adding the Compatibility attribute fix that? Also, why was it necessary for silverlight to have it?

Solution :

When you set aspNetCompatibilityEnabled to true in your config file, you are stating that your services will participate in the ASP.NET pipeline; so items like ASP.NET session are available. You need to decorate your services appropriately if this is the case, since ASP.NET Compatibility Mode is set to false by default.

So by decorating your service implementation with a RequirementsMode of Allowed, you’re stating a happy middle ground that basically says your service doesn’t care what the aspNetCompatibility mode is (true or false). If your RequirementsMode is Required, then you need to have the config aspNetCompatibilityEnabled set to true; the opposite is true if your RequirementsMode is set to NotAllowed.

(If you go with the happy middle ground of RequirementsMode of Allowed, you can check in your service implementation if aspNetCompatibilityEnabled is enabled or not by checking the static ServiceHostingEnvironment.AspNetCompatibilityEnabled property.)

Silverlight must have a dependency on the ASP.NET pipeline (I’m not a Silverlight developer), which is why you need to enable this compatibility mode in your config and on your services in order for them to be called by Silverlight apps.

Check out MSDN’s documentation on this here. The thing to know is that if you don’t need ASP.NET pipeline goodies, then you don’t need to decorate your services or set the aspNetCompatibilityEnabled setting in your config (they’re turned off by default).

Leave a Reply

Your email address will not be published. Required fields are marked *