C# 为什么 AspNetCompatibilityRequirementsMode.Allowed 修复了这个错误?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9793233/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why does AspNetCompatibilityRequirementsMode.Allowed fix this error?
提问by Justin
I was searching around trying to solve a problem I am having with WCF. I am very new to WCF so I wasn't sure exactly what was going on.
我正在四处寻找试图解决我在 WCF 中遇到的问题。我对 WCF 很陌生,所以我不确定到底发生了什么。
I am using Visual Studio 2010 and did New Web Site->WCF Service. I created my service and in the config file, if I set aspNetCompatibilityEnabled="true", I would get this error when going to the service through my web browser.
我正在使用 Visual Studio 2010 并且做了新网站-> WCF 服务。我创建了我的服务,并且在配置文件中,如果我设置了aspNetCompatibilityEnabled="true",则在通过 Web 浏览器访问该服务时会出现此错误。
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'.
I don't understand what this means. Why aspNetCompatibilityEnabled="true"cause this error when [AspNetCompatibilityRequirements(RequirementsMode=AspNetCompatibilityRequirementsMode.Allowed)]fixes it.
我不明白这是什么意思。为什么aspNetCompatibilityEnabled="true"在[AspNetCompatibilityRequirements(RequirementsMode=AspNetCompatibilityRequirementsMode.Allowed)]修复它时会导致此错误。
To me, they sound like they do the same thing. Also, without that attribute silverlight was not able to call my WCF methods. Why is that?
对我来说,他们听起来像是在做同样的事情。此外,如果没有该属性,silverlight 无法调用我的 WCF 方法。这是为什么?
Here is my config file if necessary:
如果需要,这是我的配置文件:
<?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>
So my question is, why does adding the Compatibility attribute fix that? Also, why was it necessary for silverlight to have it?
所以我的问题是,为什么添加 Compatibility 属性可以解决这个问题?另外,为什么 Silverlight 必须拥有它?
采纳答案by David Hoerster
When you set aspNetCompatibilityEnabledto truein your config file, you are stating that your services will participate in the ASP.NET pipeline; so items like ASP.NET session are available. You need to decorate your services appropriately if this is the case, since ASP.NET Compatibility Mode is set to false by default.
当您设置aspNetCompatibilityEnabled要true在您的配置文件,就表示您的服务将参加ASP.NET管道; 所以像 ASP.NET 会话这样的项目是可用的。如果是这种情况,您需要适当地装饰您的服务,因为 ASP.NET 兼容模式默认设置为 false。
So by decorating your service implementation with a RequirementsModeof Allowed, you're stating a happy middle ground that basically says your service doesn't care what the aspNetCompatibilitymode is (true or false). If your RequirementsModeis Required, then you need to have the config aspNetCompatibilityEnabledset to true; the opposite is true if your RequirementsModeis set to NotAllowed.
因此,通过使用RequirementsModeof装饰您的服务实现Allowed,您就在陈述一个快乐的中间立场,基本上说您的服务不关心aspNetCompatibility模式是什么(真或假)。如果您RequirementsMode是Required,那么您需要将配置aspNetCompatibilityEnabled设置为 true;如果您RequirementsMode设置为 ,则相反NotAllowed。
(If you go with the happy middle ground of RequirementsMode of Allowed, you can check in your service implementation if aspNetCompatibilityEnabled is enabled or not by checking the static ServiceHostingEnvironment.AspNetCompatibilityEnabledproperty.)
(如果您采用了 Allowed 的RequirementsMode 的中间立场,您可以通过检查静态ServiceHostingEnvironment.AspNetCompatibilityEnabled属性来检查您的服务实现是否启用了 aspNetCompatibilityEnabled 。)
Silverlight must have a dependency on the ASP.NET pipeline (I'm not a Silverlight developer), which is why you need to enable this compatibility mode in your config and on your services in order for them to be called by Silverlight apps.
Silverlight 必须依赖于 ASP.NET 管道(我不是 Silverlight 开发人员),这就是为什么您需要在配置和服务中启用此兼容模式,以便 Silverlight 应用程序调用它们。
Check out MSDN's documentation on this here. The thing to know is that if you don't need ASP.NET pipeline goodies, then you don't need to decorate your services or set the aspNetCompatibilityEnabled setting in your config (they're turned off by default).
在此处查看 MSDN 的相关文档。要知道的是,如果您不需要 ASP.NET 管道好东西,那么您不需要装饰您的服务或在您的配置中设置 aspNetCompatibilityEnabled 设置(默认情况下它们是关闭的)。

