WCF web service error: The service cannot be activated because it does not support ASP.NET compatibility

asp.netWcfRest

asp.net Problem Overview


I am trying to create a restful wcf web service. When I try to connect to the service through the client I get the following error:

>>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'.

Others have had problems, but they fixed it through changes to their web.config. I have implemented their fix, but still the problem exists. here is my web.config:

<system.serviceModel>
    <behaviors>
      <endpointBehaviors>
        <behavior name="WebBehavior" >
           <webHttp />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="MyServiceBehavior">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service behaviorConfiguration="MyServiceBehavior" name="myfirstwcf">
        <endpoint address="ws" binding="basicHttpBinding" 
                  contract="Imyfirstwcf" />
        <endpoint address="ws2" binding="wsHttpBinding" 
                  contract="Imyfirstwcf" />
        <endpoint address="" behaviorConfiguration="WebBehavior" 
                  binding="webHttpBinding" 
                  contract="Imyfirstwcf" />
        <endpoint address="mex" binding="mexHttpBinding" 
                  contract="IMetadataExchange" />
      </service>
    </services>
    <serviceHostingEnvironment aspNetCompatibilityEnabled= "true"
      multipleSiteBindingsEnabled="true"  />
  </system.serviceModel>

asp.net Solutions


Solution 1 - asp.net

On your main service you could mark your service as:

[AspNetCompatibilityRequirements(
        RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

From http://forums.silverlight.net/t/21944.aspx

Solution 2 - asp.net

it will work :

you have change this lines in code or add the line in web.config:

<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="false" multipleSiteBindingsEnabled="true" /> 
</system.serviceModel>

Solution 3 - asp.net

If someone has a lot of services and services are created using custom ServiceHostFactory, then AspNetCompatibilityRequirementsAttribute can also be set in CreateServiceHost method.

Example:

public class HostFactory : ServiceHostFactory
{
    protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
    {
        var host = new ServiceHost(serviceType, baseAddresses);
		//other relevent code to configure host's end point etc
        if (host.Description.Behaviors.Contains(typeof(AspNetCompatibilityRequirementsAttribute)))
        {
            var compatibilityRequirementsAttribute = host.Description.Behaviors[typeof(AspNetCompatibilityRequirementsAttribute)] as AspNetCompatibilityRequirementsAttribute;
            compatibilityRequirementsAttribute.RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed;
        }
        else
        {
            host.Description.Behaviors.Add(new AspNetCompatibilityRequirementsAttribute() { RequirementsMode =AspNetCompatibilityRequirementsMode.Allowed});
        }
        return host;
    }
}

Solution 4 - asp.net

Actually, as per the latest documentation you need to do 2 things,

1.For your service class:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(Namespace = "url")]
public class Service : IService
{
}

2.For web.config

<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1186651View Question on Stackoverflow
Solution 1 - asp.netWaqar JanjuaView Answer on Stackoverflow
Solution 2 - asp.netArun josephView Answer on Stackoverflow
Solution 3 - asp.netAbdul RaufView Answer on Stackoverflow
Solution 4 - asp.nettRuEsAtMView Answer on Stackoverflow