2012-03-20 94 views
26

我正在四處搜尋,試圖解決我在使用WCF時遇到的問題。我對WCF很陌生,所以我不確定到底發生了什麼。爲什麼AspNetCompatibilityRequirementsMode.Allowed修復此錯誤?

我正在使用Visual Studio 2010並做了新網站 - > WCF服務。我創建了我的服務,並且在配置文件中,如果我設置了aspNetCompatibilityEnabled="true",則在通過我的Web瀏覽器訪問服務時會出現此錯誤。

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'. 

我不明白這是什麼意思。爲什麼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> 

所以我的問題是,爲什麼不將Compatibility屬性解決呢?另外,爲什麼Silverlight需要它?

回答

36

當您在配置文件中將aspNetCompatibilityEnabled設置爲true時,您表示您的服務將參與ASP.NET管道;所以像ASP.NET會話這樣的項目是可用的。如果是這種情況,您需要適當修飾您的服務,因爲ASP.NET兼容模式默認設置爲false。所以

用的Allowed一個RequirementsMode裝飾你的服務實現,你在陳述一個幸福的中間地帶,基本上說,你的服務不關心aspNetCompatibility模式是什麼(true或false)。如果您的RequirementsModeRequired,那麼您需要將config aspNetCompatibilityEnabled設置爲true;如果您的RequirementsMode設置爲NotAllowed,則情況正好相反。

(如果你用的允許RequirementsMode的快樂中間地帶去,你可以,如果aspNetCompatibilityEnabled通過檢查靜態ServiceHostingEnvironment.AspNetCompatibilityEnabled屬性啓用或不在服務實現查詢。)

的Silverlight必須對依賴ASP.NET管道(我不是Silverlight開發人員),這就是爲什麼您需要在您的配置和服務中啓用此兼容模式才能讓Silverlight應用程序調用它們。

查看關於此here的MSDN文檔。要知道的是,如果你不需要ASP.NET管道的好東西,那麼你不需要裝飾你的服務,或者在你的配置中設置aspNetCompatibilityEnabled設置(默認關閉它們)。

+0

問題,如果將它設置爲true,那麼這將是什麼合適的裝飾? – Justin 2012-03-20 19:32:18

+0

這取決於您的服務是否需要ASP.NET管道提供的項目。如果你真的不在乎,那麼你將它設置爲'RequirementsMode.Allowed'。如果你依賴這些組件(比如Session),那麼你應該把它設置爲'RequirementsMode.Required'。 – 2012-03-20 19:37:31

相關問題