In this article I will explain how to solve the following error in ASP.Net WCF Service when aspNetCompatibilityEnabled attribute is set to true in Web.Config file.
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'.
 
 
The Problem
The following error is thrown by the ASP.Net WCF Service when the aspNetCompatibilityEnabled attribute is set to true in Web.Config file.
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'.
ASP.Net WCF: The service cannot be activated because it does not support ASP.NET compatibility
 
 
The Solution
The above error occurs because in order to make the WCF Service work in ASP.Net compatibility mode, you also need to decorate the WCF Service class with the AspNetCompatibilityRequirements attribute.
Below are the correct steps to make the WCF Service work in ASP.Net compatibility mode,
1. Set aspNetCompatibilityEnabled attribute to true for the serviceHostingEnvironment tag in system.serviceModel section of the Web.Config file.
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true" />
 
2. Import the following namespaces in the WCF Service class.
C#
using System.Web;
using System.ServiceModel.Activation;
 
VB.Net
Imports System.Web
Imports System.ServiceModel.Activation
 
3. Decorate the WCF Service class with the AspNetCompatibilityRequirements attribute.
C#
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
public class Service : IService
{
    public string GetName()
    {
        return HttpContext.Current.User.Identity.Name;
    }
}
 
VB.Net
<AspNetCompatibilityRequirements(RequirementsMode:=AspNetCompatibilityRequirementsMode.Required)> _
Public Class Service
    Implements IService
    Public Function GetName() As String Implements IService.GetName
        Return HttpContext.Current.User.Identity.Name
    End Function
End Class
 
ASP.Net WCF: The service cannot be activated because it does not support ASP.NET compatibility
 
 
 
Downloads