【问题标题】:Why does AspNetCompatibilityRequirementsMode.Allowed fix this error?为什么 AspNetCompatibilityRequirementsMode.Allowed 修复了这个错误?
【发布时间】:2012-04-05 07:11:36
【问题描述】:

我正在四处寻找,试图解决我在使用 WCF 时遇到的问题。我对 WCF 很陌生,所以我不确定到底发生了什么。

我正在使用 Visual Studio 2010 并进行了新网站->WCF 服务。我创建了我的服务并在配置文件中,如果我设置aspNetCompatibilityEnabled="true",我会在通过我的网络浏览器访问服务时收到此错误。

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'.

我不明白这是什么意思。为什么aspNetCompatibilityEnabled="true"[AspNetCompatibilityRequirements(RequirementsMode=AspNetCompatibilityRequirementsMode.Allowed)] 修复它时会导致此错误。

在我看来,它们听起来就像在做同样的事情。此外,如果没有该属性,silverlight 将无法调用我的 WCF 方法。这是为什么呢?

如果需要,这是我的配置文件:

<?xml version="1.0"?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
    <customErrors mode="Off"/>
  </system.web>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="LargeBuffer" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" />
      </basicHttpBinding>
    </bindings>
    <services>
      <service name="Services.Exporter">
        <endpoint address="" binding="basicHttpBinding" bindingConfiguration="LargeBuffer"
          contract="Services.IExporter" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment
      multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
</configuration>

所以我的问题是,为什么添加 Compatibility 属性可以解决这个问题?还有,为什么silverlight有必要拥有它?

【问题讨论】:

    标签: c# .net wcf silverlight web-services


    【解决方案1】:

    当您在配置文件中将aspNetCompatibilityEnabled 设置为true 时,您声明您的服务将参与ASP.NET 管道;所以像 ASP.NET 会话这样的项目是可用的。如果是这种情况,您需要适当地装饰您的服务,因为 ASP.NET 兼容模式默认设置为 false。

    因此,通过使用RequirementsModeAllowed 来装饰您的服务实现,您就是在陈述一个快乐的中间立场,基本上说您的服务并不关心aspNetCompatibility 模式是什么(真或假)。如果您的RequirementsModeRequired,那么您需要将配置aspNetCompatibilityEnabled 设置为true;如果您的RequirementsMode 设置为NotAllowed,则相反。

    (如果您选择 Allowed 的 RequirementsMode 中间立场,您可以通过检查静态 ServiceHostingEnvironment.AspNetCompatibilityEnabled 属性来检查您的服务实现是否启用了 aspNetCompatibilityEnabled。)

    Silverlight 必须依赖于 ASP.NET 管道(我不是 Silverlight 开发人员),这就是为什么您需要在配置和服务中启用此兼容模式才能让 Silverlight 调用它们应用程序。

    查看 MSDN 关于此 here 的文档。要知道的是,如果您不需要 ASP.NET 管道好东西,那么您不需要装饰您的服务或在您的配置中设置 aspNetCompatibilityEnabled 设置(默认情况下它们是关闭的)。

    【讨论】:

    • 问题,如果我将它设置为 true,这将是什么合适的装饰?
    • 这取决于您的服务是否需要 ASP.NET 管道可用的项目。如果您不在乎,则可以将其设置为RequirementsMode.Allowed。如果您依赖这些组件(如 Session),则应将其设置为 RequirementsMode.Required
    猜你喜欢
    • 2016-05-10
    • 1970-01-01
    • 2013-05-16
    • 1970-01-01
    • 2019-10-27
    • 1970-01-01
    • 2022-06-29
    • 2021-04-11
    • 2011-04-14
    相关资源
    最近更新 更多