【问题标题】:DLL Redirect - Build Number MismatchDLL 重定向 - 内部版本号不匹配
【发布时间】:2021-03-18 17:52:30
【问题描述】:

我似乎无法在 VS2010 ConsoleApplication 中加载 System.Net.Http.Formatting.dll,即使我已经设置了重定向。这是错误

System.IO.FileNotFoundException: Could not load file or assembly 'System.Net.Http.Formatting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.
File name: 'System.Net.Http.Formatting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'
   at uk.ac.salford.accman.backend.service.Models.WebService.AlmaRestClient.CreateOrUpdateUser(Operator user, Account account, JanusUser janusUser)
   at Salford.AccMan.BackEnd.Processes.Janus.UpdateALMAUser.ProcessTransaction(Transaction& transaction) in C:\VS2010\Accman.NET\AccManBackEndLibrary\Processes\Janus\UpdateALMAUser.cs:line 121
   at Salford.AccMan.BackEnd.Processes.TransactionServiceProcess.Process() in C:\VS2010\Accman.NET\AccManBackEndLibrary\TransactionServiceProcess.cs:line 147

=== Pre-bind state information ===
LOG: DisplayName = System.Net.Http.Formatting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
 (Fully-specified)
LOG: Appbase = file:///C:/VS2010/.NET Code Library/ConsoleSchedulerService/ConsoleSchedulerService/bin/Debug/
LOG: Initial PrivatePath = NULL
Calling assembly : AccManLibrary, Version=3.0.4.0, Culture=neutral, PublicKeyToken=null.
===
LOG: This bind starts in LoadFrom load context.
WRN: Native image will not be probed in LoadFrom context. Native image will only be probed in default load context, like with Assembly.Load().
LOG: Using application configuration file: C:\VS2010\.NET Code Library\ConsoleSchedulerService\ConsoleSchedulerService\bin\Debug\ConsoleSchedulerService.exe.Config
LOG: Using host configuration file: 
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.0.0.0 redirected to 4.0.21112.0.
LOG: Post-policy reference: System.Net.Http.Formatting, Version=4.0.21112.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
LOG: Attempting download of new URL file:///C:/VS2010/.NET Code Library/ConsoleSchedulerService/ConsoleSchedulerService/bin/Debug/System.Net.Http.Formatting.DLL.
LOG: Attempting download of new URL file:///C:/VS2010/.NET Code Library/ConsoleSchedulerService/ConsoleSchedulerService/bin/Debug/System.Net.Http.Formatting/System.Net.Http.Formatting.DLL.
LOG: Attempting download of new URL file:///C:/VS2010/.NET Code Library/ConsoleSchedulerService/ConsoleSchedulerService/bin/Debug/redirect/System.Net.Http.Formatting.DLL.
LOG: Attempting download of new URL file:///C:/VS2010/.NET Code Library/ConsoleSchedulerService/ConsoleSchedulerService/bin/Debug/redirect/System.Net.Http.Formatting/System.Net.Http.Formatting.DLL.
LOG: Attempting download of new URL file:///C:/VS2010/.NET Code Library/ConsoleSchedulerService/ConsoleSchedulerService/bin/Debug/System.Net.Http.Formatting.EXE.
LOG: Attempting download of new URL file:///C:/VS2010/.NET Code Library/ConsoleSchedulerService/ConsoleSchedulerService/bin/Debug/System.Net.Http.Formatting/System.Net.Http.Formatting.EXE.
LOG: Attempting download of new URL file:///C:/VS2010/.NET Code Library/ConsoleSchedulerService/ConsoleSchedulerService/bin/Debug/redirect/System.Net.Http.Formatting.EXE.
LOG: Attempting download of new URL file:///C:/VS2010/.NET Code Library/ConsoleSchedulerService/ConsoleSchedulerService/bin/Debug/redirect/System.Net.Http.Formatting/System.Net.Http.Formatting.EXE.
LOG: Attempting download of new URL file:///C:/VS2010/Accman.NET/AccManBackEndLibrary/bin/System.Net.Http.Formatting.DLL.
WRN: Comparing the assembly name resulted in the mismatch: Build Number
LOG: Attempting download of new URL file:///C:/VS2010/Accman.NET/AccManBackEndLibrary/bin/System.Net.Http.Formatting/System.Net.Http.Formatting.DLL.
LOG: Attempting download of new URL file:///C:/VS2010/Accman.NET/AccManBackEndLibrary/bin/System.Net.Http.Formatting.EXE.
LOG: Attempting download of new URL file:///C:/VS2010/Accman.NET/AccManBackEndLibrary/bin/System.Net.Http.Formatting/System.Net.Http.Formatting.EXE.

所以,据我了解,它正在寻找 System.Net.Http.Formatting,Version=4.0.0.0,但我的项目包含版本 4.0.21112.0,所以我设置了重定向(堆栈中也提到了跟踪,所以我知道重定向至少正在被拾取)

我的重定向是:

      <dependentAssembly>
    <assemblyIdentity name="System.Net.Http.Formatting" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.21112.0" />
  </dependentAssembly>

它在 C:/VS2010/Accman.NET/AccManBackEndLibrary/bin 中找到较新的 DLL,但抱怨内部版本号不正确。但是,据我所知,整个版本号是正确的。

如果我运行 Powershell 命令来获取确切的版本号,它会返回相同的版本号:

PS F:\> (get-item C:\VS2010\Accman.NET\AccManBackEndLibrary\bin\System.Net.Http.Formatting.dll).VersionInfo

ProductVersion   FileVersion      FileName
--------------   -----------      --------
4.0.21112.0      4.0.21112.0      C:\VS2010\Accman.NET\AccManBackEndLibrary\bin\System.Net.Http.Formatting.dll

请帮忙!我做错了什么?

谢谢

【问题讨论】:

    标签: c# dll


    【解决方案1】:

    您可以从项目中删除 dll 引用并手动重新添加您拥有的 dll。
    (解决方案资源管理器 -> 参考 -> 添加 -> 浏览)
    应该解决问题。

    【讨论】:

    • 谢谢,您的意思是从 Visual Studio 中的项目中删除引用吗?
    • 抱歉回复延迟。不幸的是,这似乎并没有解决问题,但我将删除所有内容并仔细检查任何地方都没有任何挥之不去的引用,然后再尝试重新添加它们(手动)而不是使用 Nuget,看看我是否可以让它工作。谢谢
    • 不,同样的问题。我什至在手动添加引用之前从 packages.config 中删除了引用。我没有得到的是它确认它正在寻找(通过重定向)版本 4.0.21112.0,然后你可以看到它在哪里找到DLL,在正确的位置,具有完全相同的版本。但它仍然说内部版本号不匹配......而且该文件夹中没有该 DLL 的多个版本。我很困惑……
    • 已修复。我实际上有两个可能的答案,我将发布它们以防他们帮助其他人,但我已将您的答案标记为有用,因为它帮助我进一步获得了非常感谢您对此的帮助:)
    【解决方案2】:

    回答 1

    对,如果您想使用与 .NET 4 捆绑在一起的 System.Net.Http.Formatting、System.Net.Http 和 System.Net.WebRequest 版本,请先回答,这是我的(旧版)项目用途。

    • 删除对这些包的每个引用(包括 packages.config 和 Nuget 包管理器)。此外,如果您的问题是因为您像我一样从 Nuget 安装了 NewtonSoft.Json,请也将其删除。

    • 按照@user5226582 的建议手动添加它们(右键单击项目中的引用 -> 添加引用 -> Assemblies -> Extensions。注意我是从捆绑的 Extensions Assemblies 中选择它们的。

    • 此外,如果您正在使用 NewtonSoft.Json.dll,请执行相同的操作。这也在程序集 -> 扩展中。

    • 从 app.config 中删除与这些文件相关的任何 bindingRedirects

    现在这是一个公平的竞争环境,因为所有 DLL 都来自同一个地方,并且应该相互兼容。

    答案 2

    System.Net.Http 已被 Microsoft.Net.Http 取代。删除任何现有的引用和相关的 bindingRedirects(如答案 1 所示)。

    • 使用Nuget安装Microsoft.Net.Http
    • 使用 Nuget 安装 NewtonSoft.Json(如果使用)

    这似乎对我有用

    【讨论】:

      【解决方案3】:

      包文件版本和程序集版本不一定对齐:

      https://stackoverflow.com/a/51736117/3383751

      【讨论】:

        猜你喜欢
        • 2011-10-07
        • 2011-05-08
        • 2017-04-09
        • 2020-02-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-03
        相关资源
        最近更新 更多