WCF Web服务错误:服务无法激活,因为它不支持ASP.NET兼容性

我正在尝试创build一个平静的wcf web服务。 当我尝试通过客户端连接到服务时,出现以下错误:

该服务无法激活,因为它不支持ASP.NET兼容性。 此应用程序启用了ASP.NET兼容性。 closuresweb.config中的ASP.NET兼容模式,或将AspNetCompatibilityRequirements属性添加到R​​equirementsMode设置为“允许”或“必需”的服务types。

其他人有问题,但他们修改了他们的web.config。 我已经实施了他们的修复程序,但问题仍然存在。 这里是我的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> 

在您的主要服务上,您可以将您的服务标记为:

 [AspNetCompatibilityRequirements( RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 

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

它会工作:

你已经在代码中改变了这一行,或者在web.config中添加了这一行:

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

如果有人有很多服务和服务是使用自定义的ServiceHostFactory创build的,那么也可以在CreateServiceHost方法中设置AspNetCompatibilityRequirementsAttribute

例:

 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; } }