Wednesday, January 02, 2013

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

Hi Folks,

I was developing a secure WCF service that allows only authenticated windows users to access this service in IIS 7.5. After disabling the anonymous access i figured that wsHttpBinding will not work because you have to enable it to make the WCF binding working, then i changed the WCF binding to basicHttpBinding and passed the user credentials from the web application by using below line of code:

C# Code:
svc.ClientCredentials.Windows.AllowedImpersonationLevel = TokenImpersonationLevel.Impersonation;

The above line passed the user credentials from the client web application to the service.

Passing the credentials from the client web application wasn't accepted by the WCF service because i was getting another error message from the wcf service:

"The underlying provider failed on Open WCF when hosting in IIS with impersonation is enabled"

Since i have enabled the impersonation to pass the user credentials from the web application to the wcf i was missing the step to add the following configuration in the web.config of the WCF service:

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

"The default is false. When this attribute is set to true, requests to Windows Communication Foundation (WCF) services flow through the ASP.NET HTTP pipeline, and communication over non-HTTP protocols is prohibited"

One last thing you should add in the service class "aspNetCompatibilityEnabled" attribute to comply with configuration entry otherwise still your service won't work because it will use the default IIS account to access your database.

C# Code:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class myService : ImyService
// your implementation to the service.
}


Hope this helps!

References:

1) WCF serviceHostingEnvironment element:
http://msdn.microsoft.com/en-us/library/ms731336(v=VS.110).aspx



Post a Comment