天天看點

WCF The service cannot be activated because it does not support ASP.NET compatibility 3、在 ASP.NET 相容模式中承載 WCF 服務

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

具體錯誤資訊如下:

解決方案:

1、web.config中增加

注意這個是放在<system.servicemodel>中的。

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

WCF The service cannot be activated because it does not support ASP.NET compatibility 3、在 ASP.NET 相容模式中承載 WCF 服務

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

WCF The service cannot be activated because it does not support ASP.NET compatibility 3、在 ASP.NET 相容模式中承載 WCF 服務

小注:

1、servicehostingenvironment.aspnetcompatibilityenabled 屬性:

擷取一個值,該值訓示此服務是否在 asp.net http 應用程式管道的上下文中運作。

如果為目前 appdomain 啟用了 asp.net 相容性,則為 true;否則為 false。 預設值為 false。 

2、aspnetcompatibilityrequirementsmode 枚舉:

指定 windows communication foundation (wcf) 服務是否(或能否)以與 asp.net 相容的模式運作。

WCF The service cannot be activated because it does not support ASP.NET compatibility 3、在 ASP.NET 相容模式中承載 WCF 服務

盡管 wcf 模型旨在跨宿主環境和傳輸保持行為的一緻性,但經常在一些方案中,應用程式中不要求這種程度的靈活性。 wcf 的 asp.net 相容模式适用于具有以下特點的方案:不需要具有在 iis 外部承載或通過 http 之外的協定進行通信的能力,但使用 asp.net web 應用程式平台的所有功能。

基于檔案的授權:在 asp.net 相容模式中運作的 wcf 服務可以通過将檔案系統通路控制清單 (acl) 附加到服務的 .svc 檔案來獲得保護。

可配置的 url 授權:當 wcf 服務在 asp.net 相容模式中運作時,将對 wcf 請求強制執行 asp.net 的 url 授權規則。

asp.net 模拟:wcf 服務使用 asp.net 模拟線程的目前辨別來運作,如果已為應用程式啟用 asp.net 模拟,則該辨別可能與 iis 程序辨別不同。 如果為某個特定服務操作同時啟用 asp.net 模拟和 wcf 模拟,則服務模拟最終使用從 wcf 獲得的辨別來運作。

可通過下面的配置(位于應用程式的 web.config 檔案中)在應用程式級别上啟用 wcf 的 asp.net 相容模式:

如果沒有指定,則此值預設為“true”。 若将此值設定為“false”,則訓示在應用程式中運作的所有 wcf 服務将不在 asp.net 相容模式中運作。

[aspnetcompatibilityrequirements(requirementsmode = aspnetcompatibilityrequirementsmode.allowed)]

public class calculatorservice : icalculatorsession

{//implement calculator service methods.}

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

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

[aspnetcompatibilityrequirementsmode]

設定

觀察到的結果

aspnetcompatibilityenabled = “true”

<a target="_blank" href="https://msdn.microsoft.com/zh-cn/library/system.servicemodel.activation.aspnetcompatibilityrequirementsmode.aspx">required</a>

服務成功激活。

<a target="_blank" href="https://msdn.microsoft.com/zh-cn/library/system.servicemodel.activation.aspnetcompatibilityrequirementsmode.aspx">allowed</a>

<a target="_blank" href="https://msdn.microsoft.com/zh-cn/library/system.servicemodel.activation.aspnetcompatibilityrequirementsmode.aspx">notallowed</a>

服務接收消息時發生激活錯誤。

aspnetcompatibilityenabled = “false”

WCF The service cannot be activated because it does not support ASP.NET compatibility 3、在 ASP.NET 相容模式中承載 WCF 服務

說明

iis 7.0 和 was 允許 wcf 服務通過 http 之外的協定進行通信。 但是,在已啟用 asp.net 相容模式的應用程式中運作的 wcf 服務不允許公開非 http 終結點。 在服務接收其第一條消息時,這種配置會生成激活異常。

繼續閱讀