【问题标题】:Newtonsoft.Json dependency not publishing with the right version on "dotnet publish -o <other folder>"Newtonsoft.Json 依赖项未在“dotnet publish -o <其他文件夹>”上以正确的版本发布
【发布时间】:2022-03-31 02:19:28
【问题描述】:

问题

所以基本上,我在 DLL 地狱中。

我有一个 dotnet 5.0 + react 项目,其中一个 C# 依赖项(我假设 Microsoft.AspNetCore.Mvc.NewtonsoftJson)需要另一个 Newtonsoft.Json

在 bin 文件夹中的 release 或 debug 中构建和发布时,项目运行良好,所有依赖项和 .dll 均已正确加载。但是,一旦我为dotnet publish 使用选项-o,在已发布文件夹中创建的Newtonsoft.Json.dll 的版本为9.0.0.0,在尝试在生产中运行服务器时会引发错误,因为它期望版本为 12.x.x.x.

我尝试过的

目前我有一个修复,即独立构建 Newtsoft.Json 的 dll 文件的正确版本(12.x.x.x 或更高版本),并在投入生产时将其放入已发布的文件夹中,但显然我想了解其他可能的修复或更直接的修复,而无需在生产中部署时执行额外步骤,并帮助将来面临同样问题的人们。

  • 我已尝试在 .csproj 中使用正确版本显式包含依赖项,但依赖项仍以 9.0.0.0 版本输出。
  • 另一个解决方法是将 Microsoft.AspNetCore.Mvc.NewtonsoftJson 降级到接受 9.0.0.0 的版本,但会打开其他蠕虫病毒
  • 我已尝试清除我的 nuget 包,但也没有用。
  • I have tried this very recent stack overflow solution OP 遇到了类似的问题,但是他们将 False 添加到他们的测试项目的修复对我不起作用,因为我的测试项目不使用 Newtonsoft.Json。

文件

  • .csproj 中列出的依赖项
  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.JsonPatch" Version="5.0.11" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="5.0.10" />
    <PackageReference Include="Microsoft.AspNetCore.SpaServices.Extensions" Version="5.0.6" />
    <PackageReference Include="Microsoft.AspNetCore.ApiAuthorization.IdentityServer" Version="5.0.6" />
    <PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="5.0.6" />
    <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="5.0.6" />
    <PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="5.0.6" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="5.0.6" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.6" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="5.0.6" />
    <PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
    <PackageReference Include="Newtonsoft.Json.Bson" Version="1.0.2" />
    <PackageReference Include="System.Data.SqlClient" Version="4.8.3" />
  </ItemGroup>
  • 输出的 Newtonsoft.Json.dll
// C:\Users\***\published\Newtonsoft.Json.dll
// Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed
// Global type: <Module>
// Architecture: AnyCPU (64-bit preferred)
// Runtime: v4.0.30319
// This assembly is signed with a strong name key.
// Hash algorithm: SHA1
// Public key: 0024000004800000940000000602000000240000525341310004000001000100f561df277c6c0b497d629032b410cdcf286e537c054724f7ffa0164345f62b3e642029d7a80cc351918955328c4adc8a048823ef90b0cf38ea7db0d729caf2b633c3babe08b0310198c1081995c19029bc675193744eab9d7345b8a67258ec17d112cebdbbb2a281487dceeafb9d83aa930f32103fbe1d2911425bc5744002c7

using System;
using System.Diagnostics;
using System.Reflection;
using System.Resources;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(/*Could not decode attribute arguments.*/)]
[assembly: AssemblyInformationalVersion("9.0.1")]
[assembly: NeutralResourcesLanguage("en-US")]
[assembly: TargetFramework(".NETStandard,Version=v1.0")]
[assembly: AssemblyTitle("Json.NET .NET Standard 1.0")]
[assembly: InternalsVisibleTo("Newtonsoft.Json.Schema, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f561df277c6c0b497d629032b410cdcf286e537c054724f7ffa0164345f62b3e642029d7a80cc351918955328c4adc8a048823ef90b0cf38ea7db0d729caf2b633c3babe08b0310198c1081995c19029bc675193744eab9d7345b8a67258ec17d112cebdbbb2a281487dceeafb9d83aa930f32103fbe1d2911425bc5744002c7")]
[assembly: InternalsVisibleTo("Newtonsoft.Json.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f561df277c6c0b497d629032b410cdcf286e537c054724f7ffa0164345f62b3e642029d7a80cc351918955328c4adc8a048823ef90b0cf38ea7db0d729caf2b633c3babe08b0310198c1081995c19029bc675193744eab9d7345b8a67258ec17d112cebdbbb2a281487dceeafb9d83aa930f32103fbe1d2911425bc5744002c7")]
[assembly: InternalsVisibleTo("Newtonsoft.Json.Dynamic, PublicKey=0024000004800000940000000602000000240000525341310004000001000100cbd8d53b9d7de30f1f1278f636ec462cf9c254991291e66ebb157a885638a517887633b898ccbcf0d5c5ff7be85a6abe9e765d0ac7cd33c68dac67e7e64530e8222101109f154ab14a941c490ac155cd1d4fcba0fabb49016b4ef28593b015cab5937da31172f03f67d09edda404b88a60023f062ae71d0b2e4438b74cc11dc9")]
[assembly: AssemblyDescription("Json.NET is a popular high-performance JSON framework for .NET")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Newtonsoft")]
[assembly: AssemblyProduct("Json.NET")]
[assembly: AssemblyCopyright("Copyright © James Newton-King 2008")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyFileVersion("9.0.1.19813")]
[assembly: CLSCompliant(true)]
[assembly: AssemblyVersion("9.0.0.0")]

【问题讨论】:

  • 有线bug,查了所有MS文档,很难找到线索

标签: c# dll build dependencies production-environment


【解决方案1】:

希望我能解释为什么会发生这种情况,但我实际上不明白。一条线索是检查/obj 文件夹中的project.assets.json,其中依赖项实际上需要版本9 中的Newtonsoft(对确切的版本号进行全文搜索)。

然后,如果您已识别软件包 - 在我们的例子中是 Microsoft.Extensions.DependencyModel - 您可以升级软件包,以便它使用更新的 Newtonsoft 版本。在我们的例子中,我们明确地在升级版本中添加了这个包。或者,将“PrivateAssets=true”设置为依赖项似乎也可以。

如果有些包的版本为 9,有些包的版本为其他包,我们实际上注意到行为不是确定性的,因此发布的 DLL 有时是版本 9,有时是版本 13。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-17
    • 2021-10-17
    • 2019-09-18
    • 2018-02-08
    • 1970-01-01
    • 2023-04-01
    • 2015-09-04
    • 2021-07-29
    相关资源
    最近更新 更多