Thursday 6 August 2015

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

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'.
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.
using System.Web;
using System.ServiceModel.Activation;
3. Decorate the WCF Service class with the AspNetCompatibilityRequirements attribute.
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
public class Service : IService
{
    public string GetName()
    {
        return HttpContext.Current.User.Identity.Name;
    }
}

No comments:

Post a Comment