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

时间:2012-08-10 14:52:40

标签: asp.net wcf rest

我正在尝试创建一个安静的wcf Web服务。当我尝试通过客户端连接到服务时,我收到以下错误:

  
    

无法激活该服务,因为它不支持ASP.NET兼容性。为此应用程序启用了ASP.NET兼容性。在web.config中关闭ASP.NET兼容模式,或者将AspNetCompatibilityRequirements属性添加到服务类型,其RequirementsMode设置为“Allowed”或“Required”。

  

其他人遇到了问题,但他们通过更改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>

4 个答案:

答案 0 :(得分:103)

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

[AspNetCompatibilityRequirements(
        RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

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

答案 1 :(得分:53)

它会起作用:

您已在代码中更改此行或在web.config中添加该行:

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

答案 2 :(得分:1)

如果有人使用自定义ServiceHostFactory创建了大量服务和服务,则AspNetCompatibilityRequirementsAttribute方法也可以设置CreateServiceHost

示例:

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

答案 3 :(得分:0)

实际上,根据最新文档,您需要做两件事,

1.对于您的服务类:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(Namespace = "url")]
public class Service : IService
{
}

2.For web.config

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