55

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

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

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

4 回答 4

109

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

[AspNetCompatibilityRequirements(
        RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

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

于 2012-08-10T15:02:15.933 回答
56

它会工作:

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

<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="false" multipleSiteBindingsEnabled="true" /> 
</system.serviceModel>
于 2014-04-22T11:01:35.657 回答
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;
    }
}
于 2016-09-19T12:14:23.010 回答
0

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

1.对于您的服务等级:

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

2.对于web.config

<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
于 2018-04-09T23:25:19.797 回答