【问题标题】:WCF aspNetCompatibilityEnabled="true" raise an exception (failed to load)WCF aspNetCompatibilityEnabled="true" 引发异常(加载失败)
【发布时间】:2011-10-16 15:39:52
【问题描述】:

我需要在我的 WCF 服务中启用会话。 所以我必须:

<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>

当我这样做时,我得到一个异常:

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

这是我的 web.config:

<compilation debug="true">
  <assemblies>
    <add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
    <add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
  </assemblies>
</compilation>

<services>
  <service behaviorConfiguration="ExecutionEngine.AccountsBehavior" name="WebService.Services.Accounts">
    <endpoint address="" binding="wsHttpBinding" contract="WebService.Services.IAccounts" bindingConfiguration="SafeServiceConf">
      <identity>
        <dns value="localhost"/>
      </identity>
    </endpoint>
    <endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange"/>
  </service>
</services>



<behaviors>
  <serviceBehaviors>
    <behavior name="defaultBehavior">
      <serviceDebug includeExceptionDetailInFaults="true" />
      <serviceMetadata httpGetEnabled="true" />
    </behavior>
    <behavior name="">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="false" />
    </behavior>
    <behavior name="ExecutionEngine.AccountsBehavior">
      <serviceMetadata httpGetEnabled="true"/>
      <serviceDebug includeExceptionDetailInFaults="true" />
      <serviceCredentials>
        <userNameAuthentication
             userNamePasswordValidationMode="Custom"
             customUserNamePasswordValidatorType="WebService.Services.Security.CustomValidator,WebService" />
      </serviceCredentials>
    </behavior>
  </serviceBehaviors>
</behaviors>



<bindings>





  <wsHttpBinding>
    <binding name="SafeServiceConf" maxReceivedMessageSize="65536">
      <readerQuotas maxStringContentLength="65536" maxArrayLength="65536"
         maxBytesPerRead="65536" />
      <security mode="TransportWithMessageCredential">
        <message clientCredentialType="UserName" />
      </security>
    </binding>
    <binding name="CrmServiceEndpoint" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false">
      <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
      <reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="false" />
      <security mode="Message">
        <transport clientCredentialType="Windows" proxyCredentialType="None" realm="" />
        <message clientCredentialType="Certificate" negotiateServiceCredential="false" algorithmSuite="Default" establishSecurityContext="false" />
      </security>
    </binding>
    <binding name="CrmServiceEndpointSSL" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false">
      <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
      <reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="false" />
      <security mode="TransportWithMessageCredential">
        <transport clientCredentialType="None" proxyCredentialType="None" realm="" />
        <message clientCredentialType="Certificate" negotiateServiceCredential="true" algorithmSuite="Default" establishSecurityContext="false" />
      </security>
    </binding>
  </wsHttpBinding>

</bindings>


<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>

【问题讨论】:

    标签: c# asp.net wcf session


    【解决方案1】:

    例子:

    namespace WcfService1
    {
        [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
        public class Service1 : IService1
        {
    

    【讨论】:

    • 谢谢。你能告诉我如何启用会话和访问它吗?
    【解决方案2】:

    您是否尝试将以下属性添加到您的服务类?

    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    

    然后您可以像这样使用 HttpContext 访问 Session:

     HttpContext.Current.Session
    

    【讨论】:

    • 如果您发布代码、XML 或数据示例,在文本编辑器中突出显示这些行,然后单击编辑器上的“代码示例”按钮 ({ })工具栏以很好地格式化和语法突出显示它!
    • 谢谢!这是我在 stackoverflow 上的第一篇文章 :) 下次我会做的!
    猜你喜欢
    • 2011-11-14
    • 2021-04-15
    • 2012-11-06
    • 2020-03-23
    • 2012-09-27
    • 1970-01-01
    • 1970-01-01
    • 2012-09-12
    • 2011-01-14
    相关资源
    最近更新 更多