为什么AspNetCompatibilityRequirementsMode.Allowed修复此错误?

Jus*_*tin 26 .net c# silverlight wcf web-services

我正在寻找解决我与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'.
Run Code Online (Sandbox Code Playgroud)

我不明白这意味着什么.修复它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>
Run Code Online (Sandbox Code Playgroud)

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

Dav*_*ter 36

当您设置aspNetCompatibilityEnabledtrue在您的配置文件,就表示您的服务将参加ASP.NET管道; 因此可以使用ASP.NET会话等项目.如果是这种情况,您需要适当地装饰您的服务,因为默认情况下ASP.NET兼容模式设置为false.

因此,通过与装饰您的服务实现RequirementsModeAllowed,你指出一个幸福的中间地带,基本上说,你的服务不关心什么aspNetCompatibility模式是(true或false).如果RequirementsModeRequired,那么你需要将配置aspNetCompatibilityEnabled设置为true; 如果RequirementsMode设置为,则相反NotAllowed.

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

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

这里查看MSDN的文档.要知道的是,如果你不需要ASP.NET管道好东西,那么你不需要装饰你的服务或在你的配置中设置aspNetCompatibilityEnabled设置(它们默认是关闭的).