2011-10-16 17 views
6

Tôi cần có các phiên cho phép trong dịch vụ WCF của mình. vì vậy tôi phải:WCF aspNetCompatibilityEnabled = "true" đưa ra một ngoại lệ (không tải được)

<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/> 

Khi tôi làm điều đó tôi nhận được một ngoại lệ:

Dịch vụ này không thể được kích hoạt bởi vì nó không hỗ trợ ASP.NET tương thích. Khả năng tương thích ASP.NET được bật cho ứng dụng này. Tắt chế độ tương thích ASP.NET trong web.config hoặc thêm thuộc tính AspNetCompatibilityRequirements với loại dịch vụ với thiết lập RequirementsMode là 'cho phép' hoặc 'bắt buộc'

Đây là web.config của tôi:

<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"/> 

Trả lời

7

Ví dụ:

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

Cảm ơn. Bạn có thể cho tôi biết cách bật phiên và quyền truy cập vào phiên không? – SexyMF

2

Bạn đã thử thêm thuộc tính sau vào lớp dịch vụ chưa?

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 

Sau đó bạn có thể sử dụng HttpContext để truy cập phiên như thế này:

HttpContext.Current.Session 
+0

Nếu bạn đăng mã, XML hoặc mẫu dữ liệu, hãy ** làm nổi bật những dòng đó trong trình soạn thảo văn bản và nhấp vào nút "mẫu mã" ('{}') trên thanh công cụ trình soạn thảo để định dạng và cú pháp tô sáng nó! –

+0

Xin cảm ơn! Đây là bài viết đầu tiên của tôi trên stackoverflow :) Tôi sẽ làm điều đó lần sau! –

Các vấn đề liên quan