2012-08-23 36 views
14

您好,感謝您的幫助, 我知道這個問題或類似的問題已經發布,經常與MVC 3應用。但是,每當我嘗試使用.net 4.0目標框架從Visual Web Developer Express 2010內啓動任何應用程序時,我都會收到此錯誤消息。System.MethodAccessException:嘗試通過安全透明方法訪問安全關鍵方法在所有應用程序上都失敗

確切的錯誤信息是:

嘗試通過安全透明方法 'System.Runtime.Diagnostics.DiagnosticTrace..ctor(System.String,的System.Guid)' 來訪問安全臨界方法「系統。 Runtime.Diagnostics.EtwDiagnosticTrace..ctor(System.String,System.Guid)'failed'。

程序集'System.ServiceModel.Internals,版本= 4.0.0.0,Culture = neutral,PublicKeyToken = 31bf3856ad364e35'用AllowPartiallyTrustedCallersAttribute標記,並使用2級安全透明度模型。級別2的透明度會導致AllowPartiallyTrustedCallers程序集中的所有方法在默認情況下變爲安全透明,這可能是此異常的原因。

堆棧跟蹤:

[MethodAccessException: Attempt by security transparent method 'System.Runtime.Diagnostics.DiagnosticTrace..ctor(System.String, System.Guid)' to access security critical method 'System.Runtime.Diagnostics.EtwDiagnosticTrace..ctor(System.String, System.Guid)' failed. 

Assembly 'System.ServiceModel.Internals, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' is marked with the AllowPartiallyTrustedCallersAttribute, and uses the level 2 security transparency model. Level 2 transparency causes all methods in AllowPartiallyTrustedCallers assemblies to become security transparent by default, which may be the cause of this exception.] 
    System.Runtime.Diagnostics.DiagnosticTrace..ctor(String traceSourceName, Guid etwProviderId) +24 
    System.ServiceModel.Activation.FxTrace.InitializeTracing() +90 
    System.ServiceModel.Activation.FxTrace.get_Trace() +84 
    System.ServiceModel.ServiceHostingEnvironment.EnsureInitialized() +59 
    System.ServiceModel.ServiceHostingEnvironment.OnEnsureInitialized(Object state) +4 
    System.ServiceModel.AspNetPartialTrustHelpers.PartialTrustInvoke(ContextCallback callback, Object state) +52 
    System.ServiceModel.ServiceHostingEnvironment.SafeEnsureInitialized() +65 
    System.ServiceModel.Activation.HttpModule.ProcessRequest(Object sender, EventArgs e) +58 
    System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +143 
    System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +69 

我在這裏的損失是完全。如果我從頭創建任何基本應用程序並嘗試啓動它,即使沒有添加任何內容,我也會得到此錯誤屏幕。我試圖從

<compilation debug="true" targetFramework="4.0" /> 

標籤卸下 目標框架=「4.0」屬性,沒有結果運行它。我曾嘗試刪除該屬性和添加

<startup useLegacyV2RuntimeActivationPolicy="true"> 
    <supportedRuntime version="v2.0.50727"/> 
    <supportedRuntime version="v4.0.30319"/> 
</startup> 

的建議在此線程:BadImageFormatException - Is it possible to specify which .NET version is used to run an executable? 我試圖修復.NET 4.0框架安裝,因爲我聽說,可能是一個問題。 我剛剛安裝了Visual Web開發人員,以便從Microsoft Visual Studio 2010移除的項目上工作,因爲我的試用版已過期,因此我們將不勝感激任何幫助或潛在的解決方法。

回答

14

今天我遇到了這個問題。據我瞭解,問題是.NET 4.0程序集現在默認爲安全關鍵。安全透明代碼不可能調用安全關鍵代碼。

在我的情況下,解決方案與在我的應用程序中只有幾個程序集上定義的AllowPartiallyTrustedCallersAttribute有關。我不得不從所有程序集中刪除這個屬性,或者將它添加到所有程序集中。

+0

我已經在其他地方看過類似的東西,雖然我無法成功實施您的修復程序,但我相信這是正確的迴應。 – Christian

+0

從AssemblyInfo.cs中刪除AllowPartiallyTrustedCallersAttribute適用於我的解決方案。我從一個沒有使用最新的.Net框架版本的較舊的程序集中獲取錯誤。 – Apogee

+0

在將一個大型項目更新到.Net 4後,我遇到了這個問題。它是一個用AllowPartiallyTrustedCallersAttribute裝飾的ORM庫(Entity Spaces)。評論它,一切都已修復。謝謝! –

0

顯然,這至少使人們有可能一個潛在的解決方法對我來說,推出Visual Web Developer中的應用程序是這樣的:

  • 從 元件,其拆下targetFramework =「4.0」屬性位於元素內。

  • 如果是Silverlight應用程序右鍵單擊 ApplicationName.Web標題選擇屬性,然後將 目標框架更改爲3.5。如果它是一個asp.net應用程序,右鍵單擊頂部項目標題並選擇屬性,然後將目標框架設置爲 爲3.5。

這一切都暗示了他們對我的.Net 4.0框架安裝有問題。但是,由於我已經安裝並卸載並至少安裝了兩次.Net 4.0框架,這是我現在可以考慮的唯一選擇。如果有人爲我提供了一個更好的答案/更持久的解決方案,對我而言,我會將其標記爲答案。與此同時,我將嘗試啓動「安裝其他框架」,並再次重新安裝.Net 4.0定位包和.Net 4.0運行時,以查看它是否可行。

2

您可能需要將以下內容添加到您的AssemblyInfo.cs文件中,以便所涉及的程序集相同。

[assembly: SecurityTransparent()] 
+0

這只是爲我工作。我有一個C#類庫項目(.Net 4.0)和一個Windows Forms VB項目。我在C#庫項目的AssemblyInfo.cs中添加了「[assembly:SecurityTransparent()]」,問題消失了。 –

+0

也爲我工作。 TNKS。 –

1

欣賞我非常遲到了這裏,但在VS2013和與.NET 4.5.1的ClickOnce項目遭受同樣的問題,我固定的這個問題:

  1. 打開項目問題
  2. 打開安全選項卡
  3. 更改信任設置的ClickOnce項目的屬性爲「這是一個部分信任的應用程序」
  4. 更改信任設置回「這是一個完全信任的應用程序」

希望這可以幫助別人!

相關問題