WCF REST (HTTP) application connecting Azure Service Bus using webHttpRelayBinding causes aspNetCompatibilityEnabled error

If you have a WCF application which is connecting to Windows Azure Service Bus over REST using webHttpRelayBinding, it is possible you may hit the following error:

 

[InvalidOperationException: 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 exception call stack may look as below:

 [InvalidOperationException: 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'.]
 System.ServiceModel.Activation.HostedAspNetEnvironment.ValidateCompatibilityRequirements(AspNetCompatibilityRequirementsMode compatibilityMode) +119682
 System.ServiceModel.Activation.AspNetCompatibilityRequirementsAttribute.System.ServiceModel.Description.IServiceBehavior.Validate(ServiceDescription description, ServiceHostBase serviceHostBase) +31
 System.ServiceModel.Description.DispatcherBuilder.ValidateDescription(ServiceDescription description, ServiceHostBase serviceHost) +190
 System.ServiceModel.Description.DispatcherBuilder.InitializeServiceHost(ServiceDescription description, ServiceHostBase serviceHost) +109
 System.ServiceModel.ServiceHostBase.InitializeRuntime() +60
 System.ServiceModel.ServiceHostBase.OnBeginOpen() +27
 System.ServiceModel.ServiceHostBase.OnOpen(TimeSpan timeout) +50
 System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout) +318
 Microsoft.ServiceBus.RelayedSocketListener.Open(TimeSpan timeout) +56
 Microsoft.ServiceBus.Channels.BufferedConnectionListener.Open(TimeSpan timeout) +55
 Microsoft.ServiceBus.Channels.ConnectionAcceptor.Open(TimeSpan timeout) +55
 Microsoft.ServiceBus.Channels.ConnectionDemuxer.StartDemuxing(TimeSpan timeout, OnViaDelegate viaDelegate) +74
 Microsoft.ServiceBus.Channels.ConnectionDemuxer.Open(TimeSpan timeout) +46
 Microsoft.ServiceBus.SocketConnectionTransportManager.OnOpen(TimeSpan timeout) +639
 Microsoft.ServiceBus.Channels.TransportManager.Open(TimeSpan timeout, TransportChannelListener channelListener) +687
 Microsoft.ServiceBus.Channels.TransportManagerContainer.Open(TimeSpan timeout, SelectTransportManagersCallback selectTransportManagerCallback) +286
 Microsoft.ServiceBus.Channels.TransportChannelListener.OnOpen(TimeSpan timeout) +106
 Microsoft.ServiceBus.Channels.ConnectionOrientedTransportChannelListener.OnOpen(TimeSpan timeout) +92
 Microsoft.ServiceBus.SocketConnectionChannelListener`2.OnOpen(TimeSpan timeout) +106
 Microsoft.ServiceBus.Channels.CommunicationObject.Open(TimeSpan timeout) +658
 Microsoft.ServiceBus.Channels.LayeredChannelListener`1.OnOpen(TimeSpan timeout) +89
 Microsoft.ServiceBus.Channels.CommunicationObject.Open(TimeSpan timeout) +658
 System.ServiceModel.Dispatcher.ChannelDispatcher.OnOpen(TimeSpan timeout) +72
 

 

None of the following solution will work in this case:

[1] Adding aspNetCompatibilityEnabled=true in web.config

  • <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="false" />

 

[2] Adding AspNetCompatibilityRequirementsMode.Allowed or AspNetCompatibilityRequirementsMode.Required in your application code as below:

  •     [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
  •     [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]

 

Reason for this problem:

  • Windows Azure Service Bus for HTTP bindings does not use http.sys but rather provides its own transport to listen on the cloud. Because of this there is no integration with ASP.Net pipeline.

 

Alternatively you can create a WCF Web Role application using Windows Azure SDK and connect it to Windows Azure Service Bus and you should not hit the problem. If you create WCF Application without Windows Azure SDK then you are going to hit this error and use below solution to solve this problem.

 

Solution:

  • To solve this problem you just need to remove ASP.NET dependency from your application completely and connect to Service Bus as normal you do, the problem will be resolved.

 

-