2011-10-16 29 views
6

我需要在我的WCF服務中啓用會話。 所以我必須:WCF aspNetCompatibilityEnabled =「true」引發異常(未加載)

<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/> 

當我這樣做,我得到一個異常:

該服務無法啓動,因爲它不支持ASP.NET 兼容性。此應用程序啓用了ASP.NET兼容性。 在web.config中關閉ASP.NET兼容性模式或 RequirementsMode設置爲「允許」或「必需的」添加 AspNetCompatibilityRequirements屬性的服務類型

這是我的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"/> 

回答

7

實施例:

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

感謝。你能告訴我如何啓用會話並訪問它嗎? – SexyMF

2

你有沒有嘗試將下面的屬性添加到你的服務類?

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 

然後可以使用的HttpContext訪問會話這樣的:

HttpContext.Current.Session 
+0

如果您發佈代碼,XML或數據樣本,**請**在文本編輯器中突出顯示這些行,然後單擊編輯器工具欄上的「代碼樣本」按鈕(「{}」)以精確地設置格式並進行語法突出顯示! –

+0

謝謝!這是我在stackoverflow上的第一篇文章:)我會在下次做! –