2012-09-10 198 views
27

我在Windows 8上的Visual Studio 2012中創建了Windows窗體應用程序,目標.NET框架爲4.5,並且我想使用Datagramsocket庫的Windows SDK,所以我在編譯時將目標操作系統版本更改爲8。類型'System.IDisposable'在未引用的程序集中定義

我收到以下錯誤:

"Error 1 The type 'System.IDisposable' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'."

+0

Wince。如果我是你,我會考慮有一個dll來包裝4.0組件,並使用4.5代碼中的組件來保持哪個系統是整齊和整齊的。 –

+3

@TonyHopkinson 4.5是一個就地升級,所以你總是會使用4.5'System'。 –

+0

@大衛,這個數據包thingy不同意你,我很高興。 :) –

回答

1

它看起來像你的應用程序試圖加載System.Runtime 4.0版,但你沒有安裝該版本。你說你已經安裝了.NET 4.5,所以你可以在運行時爲redirect assembly bindings提供一些配置。

嘗試添加以下內容到App.configWeb.config文件:

<runtime> 
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> 
    <dependentAssembly> 
     <assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" /> 
     <bindingRedirect oldVersion="4.0.0.0" newVersion="4.5.0.0" /> 
    </dependentAssembly> 
    </assemblyBinding> 
</runtime> 

我沒有測試過這個確切的情況,但是這種配置有助於我類似的情況。

28

如果你是針對Windows 8,MSDN documentation有如下說明:

That said, your desktop app can’t consume much of anything from the Windows Runtime until you prepare your project with one essential reference. The Windows Runtime defines some standard classes and interfaces in System.Runtime , such as IEnumerable , that are used throughout the Windows Runtime libraries. By default, your managed desktop app won’t be able to find these types, and so you must manually reference System.Runtime before you can do anything meaningful with Windows Runtime classes. To create this manual reference:

  1. Navigate to your managed desktop app project in the Solution Explorer.
  2. Right-click the References node and click Add Reference.
  3. Click the Browse tab.
  4. Click Browse….
  5. Navigate to the System.Runtime.dll façade. You can generally find this in a path similar to: %ProgramFiles(x86)%\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\Facades\System.Runtime.dll

希望有所幫助。

+0

這是很棒的分析。爲鏈接下載缺少的DLL文件,檢查出答案http://stackoverflow.com/a/14517992/3918598 – user3918598

+0

謝謝內森。我將該文件複製到我的web項目bin文件夾中,以便它可以與解決方案打包在一起。添加參考>解決方案>項目的Bin文件夾。 – JoshYates1980

19

如果你遇到了這個問題,4.5.2和團結,微軟已經發布了補丁,知識庫文章: http://support.microsoft.com/kb/2971005

+3

這工作。在Windows 10下,我必須安裝更新後的.NET Framework 4.5.2開發包。 – erdinger

+0

感謝喬希,但是我正在運行VS2015企業更新2,我仍然遇到此問題。項目的目標是4.5.2。如果我手動添加在web.config中的引用它的作品。由於我正在開展一個小組項目,而其他人沒有遇到這個問題,所以我最好不要添加這個作爲修補程序。其他人是否找到了解決方法或將我指向正確的方向? –

+0

與我們的Windows Server 2012構建機器一樣,安裝.NET Framework 4.5.2開發包散列解決了問題 – Jamby

1

雖然有TFS工作建立代理我不斷看到這個錯誤之下,但僅在Windows安裝了4.5.2的2012 R2服務器。我們的Visual Studio機器都沒有編譯錯誤。

"Error 1 The type 'System.IDisposable' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'."

其他人指出的原因與便攜式類庫有關,正如其他人指出的那樣。但至於解決方案,一些建議參考System.Runtime.dll門面%ProgramFiles(x86)%\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\Facades\System.Runtime.dll我不同意,因爲路徑和版本可能會在其他機器上不同。

取而代之,我發現的最佳解決方案是在生成機器上安裝Windows SDK

我發現從這裏後here和真正明確說明該解決方案,爲什麼作爲喬希提到發生here

0

這個問題,我在與Web API項目和幾個庫項目的解決方案有這個問題,其中一個圖書館項目正在構建中,錯誤表示Unity屬性不是「有效」的屬性,然後其中一個錯誤說The type 'System.Attribute' is defined in an assembly that is not referenced. You must add a reference to assembly System.Runtime.

建議的「更新」4.5.2開發包是什麼我已經安裝了,所以沒有幫助我。在預感上,我想也許它只是一個版本不匹配。所以我研究了每個項目的屬性,其中一個基本庫的目標是4.5,而另一個目標是4.5.2。我改變了那個也瞄準了4.5.2並且錯誤消失了。

0

我在VS 2015的某些解決方案中遇到了這個問題,甚至在一個工作站上的相同解決方案中,但在另一個工作站上卻沒有。將.NET版本更改爲4.6後,出現錯誤。

解決方案很簡單:關閉解決方案並刪除隱藏。與文件夾位於與解決方案相同的文件夾中。

按照其他答案中的建議添加缺少的引用也解決了該問題,但即使再次刪除引用後,該錯誤仍會解決。

1

我將Unity 4.0.1添加到以.NET Framework 4.6.1爲目標的項目後,TeamCity中出現此錯誤消息。我最終將C:\ Program Files(x86)\ Reference Assemblies \ Microsoft \ Framework.NETFramework中缺失的文件夾從我的開發機器複製到構建服務器。兩者都在運行Windows 7.構建成功後。

enter image description here

+0

這可能不是最好的解決方法..(複製文件) –

1

我只是碰到了這個問題與Windows 10,TeamCity的和VS2015:

症狀:

的Visual Studio生成項目很好,但TeamCity的存在編譯問題的鬥爭。

原因:

增加了一個新的項目,以現有的解決方案 - 忘記改變TargetRuntimeVersion。

解決方案:

卸載項目該項目

編輯.csproj的文件

變化:

<TargetFrameworkVersion>v4.5.1</TargetFrameworkVersion> 

,無論你的其他項目,或任何安裝在服務器。

對我來說是:

<TargetFrameworkVersion>v4.5</TargetFrameworkVersion> 

希望這有助於其他人遇到此。

0

基於TFS 2015年生成服務器

我不知道如果此錯誤消息是通用的,答案/解決方案可能是爲每個唯一的錯誤是唯一的。但我有類似的錯誤the type 'System.IDisposable' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'

我意識到該項目使用Microsoft.Practices.Unity和接口IUnityContainer,它實現IDisposable。事實證明,TFS構建定義NuGet安裝程序構建步驟未配置,它負責在TFS構建服務器上恢復Microsoft.Practices.Unity NuGet包。如果遇到此錯誤,請檢查您的TFS構建定義,並將NuGet安裝程序構建步驟路徑指向您的解決方案完整路徑,包括。 sln分機號

enter image description here

相關問題