【问题标题】:Newtonsoft.Json dependency in a .Net Standard 2 package causes runtime error in ASP.Net Core MVC 2.0 app.Net Standard 2 包中的 Newtonsoft.Json 依赖项导致 ASP.Net Core MVC 2.0 应用程序中的运行时错误
【发布时间】:2018-03-16 13:17:53
【问题描述】:

抱歉,标题太长了!我对 dotnet core 很陌生,所以我怀疑我在这里遇到了依赖问题,但我不太明白为什么。如下:

我已将 .Net 4.5.2 库重建为 .Net Standard 2.0 库。该库依赖于 Newtonsoft.Json 11.0.1。我将整个项目打包为(本地)nuget 包,因为这似乎是使其可用于多个项目的适当方式。这是回购:https://github.com/oliver-wood/realex-hpp-dotnet-standard

这个想法是在 dotnet core 2.0 web 项目中引用这个包。所以我启动了 Visual Studio 并选择了一个普通的 ASP.Net Core MVC 2.0 应用程序。快速调试表明它启动正常。 https://github.com/oliver-wood/realex-test-dotnetcore

然后我安装了 nuget 包,Web 项目构建没有问题。但是,一旦我开始调试,就会出现运行时错误:

System.TypeLoadException has been thrown
"Could not load type 'System.Runtime.Serialization.SerializationBinder' 
from assembly 'netstandard, Version=2.0.0.0, Culture=neutral, 
PublicKeyToken=cc7b13ffcd2ddd51'."

Picture of Runtime error

at Newtonsoft.Json.JsonSerializer..ctor()
at Newtonsoft.Json.JsonSerializer.Create(JsonSerializerSettings settings)
at Microsoft.AspNetCore.Mvc.ViewFeatures.CookieTempDataProvider..ctor(IDataProtectionProvider dataProtectionProvider, ILoggerFactory loggerFactory, IOptions`1 options)
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, ServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitSingleton(SingletonCallSite singletonCallSite, ServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, ServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitSingleton(SingletonCallSite singletonCallSite, ServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, ServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitSingleton(SingletonCallSite singletonCallSite, ServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitIEnumerable(IEnumerableCallSite enumerableCallSite, ServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, ServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitSingleton(SingletonCallSite singletonCallSite, ServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, ServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitSingleton(SingletonCallSite singletonCallSite, ServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceProvider.<>c__DisplayClass22_0.<RealizeService>b__0(ServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType)
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
at Microsoft.AspNetCore.Builder.MvcApplicationBuilderExtensions.UseMvc(IApplicationBuilder app, Action`1 configureRoutes)
at realex_test_dotnetcore.Startup.Configure(IApplicationBuilder app, IHostingEnvironment env) in /Users/oliver/Dev/realex-test-dotnetcore/realex-test-dotnetcore/Startup.cs:line 41
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at Microsoft.AspNetCore.Hosting.ConventionBasedStartup.Configure(IApplicationBuilder app)
at Microsoft.AspNetCore.Hosting.Internal.AutoRequestServicesStartupFilter.<>c__DisplayClass0_0.<Configure>b__0(IApplicationBuilder builder)
at Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()
at Microsoft.AspNetCore.Hosting.WebHostBuilder.Build()
at realex_test_dotnetcore.Program.BuildWebHost(String[] args) in /Users/oliver/Dev/realex-test-dotnetcore/realex-test-dotnetcore/Program.cs:line 21
at realex_test_dotnetcore.Program.Main(String[] args) in /Users/oliver/Dev/realex-test-dotnetcore/realex-test-dotnetcore/Program.cs:line 17

从堆栈往下看,Json.Net 是牵连的,看起来它是通过 Startup.cs 调用 app.UseMvc() 在后台调用的。

看起来很像 Newtonsoft.Json 和内置的 dotnet 核心库之间存在冲突,但我不确定如何缩小和纠正它。任何关于如何解决或解决问题的想法将不胜感激!

在下面的 @will-x 评论后于 3 月 17 日编辑

我已经在 netstandard 1.6 上重建了 nuget 包。这涉及安装更多的软件包来填补 corefx 中的空白,并适当地重写一些代码。 Newtonsoft.Json 仍然是最新版本,11.0.1。

这在我看来并不理想,但可以让我继续发展。我很确定我的客户不会给猴子,但我会;如果可能的话,我更愿意使用所有东西的最新版本,但如果有问题,我想实用主义会胜出。

【问题讨论】:

  • 您是否在 .NET Standard 项目中手动创建了 nuspec?看起来你的项目文件中有包信息,但你也有这个 nuspec。
  • 你是对的。我最初确实这样做了。好地方!但是,我最终还是让 VS2017 创建了包。我已经删除了该文件并重建了所有内容。很高兴我这样做了,但问题仍然存在:(
  • 你在做完这个之后有没有升级包版本?否则,新包将不会被拉取,应用程序将继续使用已下载的旧版本。
  • 谢谢@ChrisPratt。是的,我确保增加版本号。没有骰子。

标签: asp.net-core


【解决方案1】:

我也遇到过同样的问题。

我正在维护一个 asp.net core 2.0 项目,它引用了两个我也维护的 nuget 包。 asp.net 核心项目的稳定(生产)构建基于 dotnet sdk 2.1.4,两个 nuget 包针对 .netstandard1.6。最近asp项目升级到dotnet sdk 2.1.101,nuget包也升级到目标.netstandard2.0。 asp 项目编译正常,但是当在调试中运行时,我看到了像你这样的确切异常。我还认为它与 Newtonsoft.Json 有牵连,所以我将 Newtonsoft.Json 版本从 11.0.1 降级到 10.0.3。之后项目在调试中运行,但在发布过程中抛出异常。 dotnet发布过程的视图预编译步骤发生异常:

Could not load file or assembly 'System.Runtime.InteropServices.RuntimeInformation, Version=4.0.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
 at Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.Internal.AssemblyMetadataGenerator.AddAssemblyMetadata(CSharpCompiler compiler, CSharpCompilation compilation, CompilationOptions compilationOptions)
 at Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.Internal.PrecompileRunCommand.CompileViews(ViewCompilationInfo[] results, String assemblyname)
 at Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.Internal.PrecompileRunCommand.Execute()
 at Microsoft.Extensions.CommandLineUtils.CommandLineApplication.Execute(String[] args)
 at Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.Internal.PrecompilationApplication.Execute(String[] args)

然后我重建了 nuget 包以明确引用 .netstandard 库 2.0.0(而不是默认的 2.0.1)。有了这个,asp项目使用最新的NewtonSoft.Json包在调试模式下成功运行。但是,视图预编译仍然失败。这是我放弃的时候。

目前,asp 项目正在引用针对 .netstandard 1.6 的旧版本的 nuget 包。这允许 asp 项目升级继续进行。但它与 .netstandard 2.0 包的兼容性仍然存在阴影。

如果对您来说很紧急(例如在我们的案例中,由于在 2.0.6 之前为 aspnetcore 运行时发现了安全漏洞),也许您可​​以尝试将您的 nuget 包向下编译为 .netstandard 1.6。

【讨论】:

  • @wil-x 感谢您的洞察力。你是对的,我可以克服运行时错误并继续开发,这是我的主要目标!
  • @wil-x 正如 MickyD 所说,我相信你同意,这不是“答案”,或者至少我们不希望它是,所以我可以' t 将其标记为这样。而且我什至不能投票赞成,因为我在这里没有足够的声誉,但你仍然赢得了虚拟品脱:)
【解决方案2】:

我遇到了同样的问题,并通过确保我对我的项目的所有 NewtonSoft.Json 依赖项都是版本 10.0.1 来解决它。

.net 核心 mvc 中间件依赖于这个版本,我在其他项目中引用了一个较新的版本。

【讨论】:

    猜你喜欢
    • 2017-07-29
    • 2020-01-20
    • 1970-01-01
    • 2020-07-08
    • 1970-01-01
    • 1970-01-01
    • 2018-03-30
    • 2019-01-07
    • 1970-01-01
    相关资源
    最近更新 更多