首页 > 解决方案 > 无法加载文件或程序集 Newtonsoft.Json - 旧版应用程序

问题描述

目前,我们的团队正在维护一个基于旧的、预编译的 Web 表单 CMS (iAPPS) 的旧代码库,我们不再支持该代码库。我们正在尝试从这个旧的 CMS 迁移,但在此期间,我们仍然需要继续维护它。目前,我们正在尝试添加一些 CC 网关功能,但来自供应商 (GlobalPay) 的 SDK 需要 Newtonsoft.Json 9.0.0.1,而我们的旧代码使用 4.5.0.0。

每当我们尝试升级 Newtonsoft 时,我们都会收到以下错误:

无法加载文件或程序集“Newtonsoft.Json,Version=4.5.0.0,Culture=neutral,PublicKeyToken=30ad4fe6b2a6aeed”或其依赖项之一。找到的程序集的清单定义与程序集引用不匹配。(来自 HRESULT 的异常:0x80131040)

我们还在构建输出中收到有关使用 app.config 从一个版本重新映射到另一个版本的警告。

我试过用应用程序配置重新映射,没有骰子。我试过升级解决方案中的所有项目,不好。我检查了 .csproj 文件,当我使用 NuGet 下载 Newtonsoft 时,它们实际上更新得很好。

主要问题之一是这个原始代码库实际上甚至没有使用 NuGet。项目的所有 DLL 都是手动管理的。我已经尝试过使用 NuGet 以及手动更新 Newtonsoft DLL。

除此之外,我担心的是,预编译的 CMS 可能以某种方式严格要求 4.5.0.0 并且除非使用该特定版本,否则不会运行。我希望那里的某个人可能会想到一些我在搜索中没有找到的其他路径。我们已经处理这个问题 2 年了,它终于达到了成为我们无法解决的真正障碍的地步。

下面是堆栈跟踪:

[FileLoadException: 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)]
   System.Net.Http.Formatting.JsonMediaTypeFormatter..ctor() +0
   System.Net.Http.Formatting.MediaTypeFormatterCollection.CreateDefaultFormatters() +41
   System.Web.Http.HttpConfiguration.DefaultFormatters() +23
   System.Web.Http.HttpConfiguration..ctor(HttpRouteCollection routes) +44
   System.Web.Http.GlobalConfiguration.<.cctor>b__0() +55
   System.Lazy`1.CreateValue() +429
   System.Lazy`1.LazyInitValue() +158
   System.Lazy`1.get_Value() +79
   System.Web.Http.GlobalConfiguration.get_Configuration() +27
   Bridgeline.iAppsCMSAPI.Web.GlobalAsaxModule.Init(HttpApplication httpApplication) +374
   System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers) +552
   System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context) +181
   System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context) +228
   System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext) +314

[HttpException (0x80004005): 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)]
   System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +10072388
   System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +99
   System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +263

这是我的装配负载跟踪:

=== Pre-bind state information ===
LOG: DisplayName = Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed
 (Fully-specified)
LOG: Appbase = file:///C:/Users/username/repos/iAPPS/CMSFrontEnd/
LOG: Initial PrivatePath = C:\Users\username\repos\iAPPS\CMSFrontEnd\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:\Users\username\repos\iAPPS\CMSFrontEnd\web.config
LOG: Using host configuration file: C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet.config
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Post-policy reference: Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed
LOG: Attempting download of new URL file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/Temporary ASP.NET Files/root/993da2ea/aabe287a/Newtonsoft.Json.DLL.
LOG: Attempting download of new URL file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/Temporary ASP.NET Files/root/993da2ea/aabe287a/Newtonsoft.Json/Newtonsoft.Json.DLL.
LOG: Attempting download of new URL file:///C:/Users/username/repos/iAPPS/CMSFrontEnd/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.

在写这篇文章时,我还尝试手动将我能找到的唯一 4.5.0.0 项目引用更改为 9.0.0.0 文件;那里不好。

没有尝试手动添加 GlobalPay DLL,或者只是告诉我的管理层我们已经正式遇到了这个代码库的僵局,我已经没有想法了。

标签: .netwebformsjson.net

解决方案


您是否在该<runtime>部分检查过您的 web.config?

Newtonsoft 应该有一个条目

<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-12.0.0.0" newVersion="12.0.0.0" />
      </dependentAssembly> 
    </assemblyBinding>

该部分可能指向4.5.0.0您的系统,将其更改为9.0.0.1并查看是否有帮助


推荐阅读