简体   繁体   English

WCF aspNetCompatibilityEnabled =“true”引发异常(无法加载)

[英]WCF aspNetCompatibilityEnabled=“true” raise an exception (failed to load)

I need to have sessions enables in my WCF service. 我需要在我的WCF服务中启用会话。 so I have to: 所以我必须要:

<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>

When I do that I get an exception: 当我这样做时,我得到一个例外:

The service cannot be activated because it does not support ASP.NET compatibility. 无法激活该服务,因为它不支持ASP.NET兼容性。 ASP.NET compatibility is enabled for this application. 为此应用程序启用了ASP.NET兼容性。 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' 在web.config中关闭ASP.NET兼容模式或将AspNetCompatibilityRequirements属性添加到服务类型,其RequirementsMode设置为“允许”或“必需”

This is my web.config: 这是我的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"/>

Example: 例:

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

Have you tried adding the following atribute to your service class? 您是否尝试将以下属性添加到服务类?

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

You can then use the HttpContext to access the Session like this: 然后,您可以使用HttpContext访问Session,如下所示:

 HttpContext.Current.Session

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM