WCF The service cannot be activated because it does not support ASP.NET compatibility

衣舞晨風發表於2015-09-24

作者:jiankunking 出處:http://blog.csdn.net/jiankunking


測試釋出到虛擬目錄中時發生的報錯現象,由於不支援asp.net相容性而導致服務無法啟用啟用。

具體錯誤資訊如下:

Server Error in '/Service2' Application.
--------------------------------------------------------------------------------

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'. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.InvalidOperationException: 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'.

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  

Stack Trace: 


[InvalidOperationException: 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'.]
   System.ServiceModel.Activation.HostedAspNetEnvironment.ValidateCompatibilityRequirements(AspNetCompatibilityRequirementsMode compatibilityMode) +120262
   System.ServiceModel.Activation.AspNetCompatibilityRequirementsAttribute.System.ServiceModel.Description.IServiceBehavior.Validate(ServiceDescription description, ServiceHostBase serviceHostBase) +31
   System.ServiceModel.Description.DispatcherBuilder.ValidateDescription(ServiceDescription description, ServiceHostBase serviceHost) +190
   System.ServiceModel.Description.DispatcherBuilder.InitializeServiceHost(ServiceDescription description, ServiceHostBase serviceHost) +109
   System.ServiceModel.ServiceHostBase.InitializeRuntime() +60
   System.ServiceModel.ServiceHostBase.OnBeginOpen() +27
   System.ServiceModel.ServiceHostBase.OnOpen(TimeSpan timeout) +50
   System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout) +318
   System.ServiceModel.HostingManager.ActivateService(String normalizedVirtualPath) +206
   System.ServiceModel.HostingManager.EnsureServiceAvailable(String normalizedVirtualPath) +651

[ServiceActivationException: The service '/Service2/DataService.svc' cannot be activated due to an exception during compilation.  The exception message is: 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'..]
   System.Runtime.AsyncResult.End(IAsyncResult result) +687598
   System.ServiceModel.Activation.HostedHttpRequestAsyncResult.End(IAsyncResult result) +190
   System.ServiceModel.Activation.ServiceHttpHandler.EndProcessRequest(IAsyncResult result) +6
   System.Web.CallHandlerExecutionStep.OnAsyncHandlerCompletion(IAsyncResult ar) +96

 


--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.272 

中文版錯誤資訊:點選開啟連結

解決方案:

1、web.config中增加

<serviceHostingEnvironment aspNetCompatibilityEnabled="false">
注意這個是放在<system.serviceModel>中的。

2、在wcf的服務類加上附加屬性 AspNetCompatibilityRequirements

其實,通過閱讀小注可以發現,下面兩種方式均可:


小注:

1、ServiceHostingEnvironment.AspNetCompatibilityEnabled 屬性

獲取一個值,該值指示此服務是否在 ASP.NET HTTP 應用程式管道的上下文中執行。
如果為當前 AppDomain 啟用了 ASP.NET 相容性,則為 true;否則為 false。 預設值為 false。 

2、AspNetCompatibilityRequirementsMode 列舉:
指定 Windows Communication Foundation (WCF) 服務是否(或能否)以與 ASP.NET 相容的模式執行。


3、在 ASP.NET 相容模式中承載 WCF 服務

儘管 WCF 模型旨在跨宿主環境和傳輸保持行為的一致性,但經常在一些方案中,應用程式中不要求這種程度的靈活性。 WCF 的 ASP.NET 相容模式適用於具有以下特點的方案:不需要具有在 IIS 外部承載或通過 HTTP 之外的協議進行通訊的能力,但使用 ASP.NET Web 應用程式平臺的所有功能。

在預設的並行配置中,承載基礎結構的 WCF 截獲 WCF 訊息並將其路由到 HTTP 管道之外;與此不同的是,在 ASP.NET 相容模式中執行的 WCF 服務完全參與 ASP.NET HTTP 請求生命週期。 在相容模式中,WCF 服務通過IHttpHandler 實現來使用 HTTP 管道,其方式類似於處理 ASPX 頁和 ASMX Web 服務的請求。 因此,WCF 的行為在以下 ASP.NET 功能方面與 ASMX 相同:

  • 在 ASP.NET 相容模式中執行的 HttpContext: WCF 服務可以訪問 Current 以及與其關聯的狀態。

  • 基於檔案的授權:在 ASP.NET 相容模式中執行的 WCF 服務可以通過將檔案系統訪問控制列表 (ACL) 附加到服務的 .svc 檔案來獲得保護。

  • 可配置的 URL 授權:當 WCF 服務在 ASP.NET 相容模式中執行時,將對 WCF 請求強制執行 ASP.NET 的 URL 授權規則。

  • HttpModuleCollection 擴充套件性:由於在 ASP.NET 相容模式中執行的 WCF 服務完全參與 ASP.NET HTTP 請求生命週期,因此在 HTTP 管道中配置的任何 HTTP 模組能夠在服務呼叫前後的 WCF 請求上進行操作。

  • ASP.NET 模擬:WCF 服務使用 ASP.NET 模擬執行緒的當前標識來執行,如果已為應用程式啟用 ASP.NET 模擬,則該標識可能與 IIS 程式標識不同。 如果為某個特定服務操作同時啟用 ASP.NET 模擬和 WCF 模擬,則服務模擬最終使用從 WCF 獲得的標識來執行。

可通過下面的配置(位於應用程式的 Web.config 檔案中)在應用程式級別上啟用 WCF 的 ASP.NET 相容模式:

    <system.serviceModel>        <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />    </system.serviceModel>

如果沒有指定,則此值預設為“true”。 若將此值設定為“false”,則指示在應用程式中執行的所有 WCF 服務將不在 ASP.NET 相容模式中執行。

由於 ASP.NET 相容模式暗含的請求處理語義與 WCF 預設值完全不同,因此單獨的服務實現能夠控制其是否在已啟用 ASP.NET 相容模式的應用程式內執行。 服務可以使用 AspNetCompatibilityRequirementsAttribute 來指示其是否支援 ASP.NET 相容模式。 此特性的預設值為 Allowed

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

public class CalculatorService : ICalculatorSession

{//Implement calculator service methods.}

下表演示應用程式範圍的相容模式設定如何與單獨服務指定的支援級別互動:

應用程式範圍的相容模式設定

[AspNetCompatibilityRequirementsMode]

設定

觀察到的結果

aspNetCompatibilityEnabled = “true

Required

服務成功啟用。

aspNetCompatibilityEnabled = “true

Allowed

服務成功啟用。

aspNetCompatibilityEnabled = “true

NotAllowed

服務接收訊息時發生啟用錯誤。

aspNetCompatibilityEnabled = “false

Required

服務接收訊息時發生啟用錯誤。

aspNetCompatibilityEnabled = “false

Allowed

服務成功啟用。

aspNetCompatibilityEnabled = “false

NotAllowed

服務成功啟用。

說明說明

IIS 7.0 和 WAS 允許 WCF 服務通過 HTTP 之外的協議進行通訊。 但是,在已啟用 ASP.NET 相容模式的應用程式中執行的 WCF 服務不允許公開非 HTTP 終結點。 在服務接收其第一條訊息時,這種配置會生成啟用異常。

有關為 WCF 服務啟用 ASP.NET 相容模式的更多資訊,請參見 AspNetCompatibilityRequirementsMode 和ASP.NET 相容性示例。



相關文章