【问题标题】:Wrong version of .NET assembly getting copied to Web Forms bin directory.NET 程序集的错误版本被复制到 Web 窗体 bin 目录
【发布时间】:2019-03-18 10:33:02
【问题描述】:

我正在开发一个 ASP.NET Web 窗体网站,并且经常遇到如下运行时异常:

Could not load file or assembly 'Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
=== Pre-bind state information ===
LOG: DisplayName = Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed
 (Fully-specified)
LOG: Appbase = file:///C:/Code/MyProject/
LOG: Initial PrivatePath = C:\Code\MyProject\bin
Calling assembly : System.Net.Http.Formatting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\Code\MyProject\web.config
LOG: Using host configuration file: \\foo\bar\Profile.v2\simonm\Documents\IISExpress\config\aspnet.config
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Redirect found in application configuration file: 4.5.0.0 redirected to 11.0.0.0.
LOG: Post-policy reference: Newtonsoft.Json, Version=11.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed
LOG: Attempting download of new URL file:///C:/Users/simonm/AppData/Local/Temp/Temporary ASP.NET Files/vs/8d6ca9e7/1d38a78a/Newtonsoft.Json.DLL.
LOG: Attempting download of new URL file:///C:/Users/simonm/AppData/Local/Temp/Temporary ASP.NET Files/vs/8d6ca9e7/1d38a78a/Newtonsoft.Json/Newtonsoft.Json.DLL.
LOG: Attempting download of new URL file:///C:/Code/MyProject/bin/Newtonsoft.Json.DLL.
WRN: Comparing the assembly name resulted in the mismatch: Major Version
ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated.

它们总是因为“错误”版本的程序集(它似乎总是 Newtonsoft.Json)被复制到 Bin 目录,尽管站点根目录中的 packages.config 具有:

<package id="Newtonsoft.Json" version="11.0.2" targetFramework="net45" />

并且 web.config 包含:

<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-11.0.0.0" newVersion="11.0.0.0" />
</dependentAssembly>

谁能解释一下是什么决定了哪个程序集被复制到Bin 目录,为什么这种情况不断发生以及如何防止它在未来发生?

【问题讨论】:

    标签: asp.net webforms nuget web-config .net-assembly


    【解决方案1】:

    我猜,您使用的一些框架/库正在使用不同版本的 Newtonsoft.Json。首先,检查您的框架/库是否需要不同版本的 Newtonsoft.Json 库。

    当您检测到Newtonsoft.Json 所需的(最低)版本时,请尝试通过Nuget 重新安装此版本。

    【讨论】:

    • 感谢您的回复。你是对的,不同的项目依赖于不同的版本,但我觉得令人困惑的是为什么 MSBuild 使用其他项目的依赖关系来解决 Web 窗体站点的依赖关系,以及它如何决定使用这些项目中的哪个来解决依赖关系。跨度>
    【解决方案2】:

    这似乎是由多种因素造成的,但最终归结为 Bin 目录中有一个 .refresh 文件,该文件指向的程序集版本与 web.config 中引用的版本不同。

    看起来这些 .refresh 文件是在您(手动或自动由于例如Update-Package)添加对 ASP.NET 项目的引用时添加的。如果您查看 .refresh 文件的内容,它只是 .dll 文件的路径。当您构建时,MSBuild(我假设)检查 Bin 目录中的 Foo.dll 是否与 Foo.dll.refresh 引用的那个匹配,如果不匹配,则将 .refresh 文件引用的那个复制到您的 Bin 中。

    我认为这与将 Git 中的分支切换到引用 web.config 中引用的不同版本的分支相结合,再加上 .refresh 文件不在版本控制中,是导致这些问题的原因。

    【讨论】:

      猜你喜欢
      • 2012-02-07
      • 2017-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-10
      • 1970-01-01
      • 2015-12-15
      相关资源
      最近更新 更多