gpt4 book ai didi

c# - 为什么 AspNetCompatibilityRequirementsMode.Allowed 会修复此错误?

转载 作者:IT王子 更新时间:2023-10-29 04:19:04 26 4
gpt4 key购买 nike

我四处寻找,试图解决我在使用 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'.

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

对我来说,他们听起来像是在做同样的事情。此外,如果没有该属性,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>

所以我的问题是,为什么添加兼容性属性可以解决这个问题?另外,为什么 silverlight 需要它?

最佳答案

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

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

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

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

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

关于c# - 为什么 AspNetCompatibilityRequirementsMode.Allowed 会修复此错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9793233/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com