gpt4 book ai didi

asp.net-core - .Net Standard 2 包中的 Newtonsoft.Json 依赖项导致 ASP.Net Core MVC 2.0 应用程序中的运行时错误

转载 作者:行者123 更新时间:2023-12-04 14:00:16 26 4
gpt4 key购买 nike

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

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

这个想法是在 dotnet core 2.0 web 项目中引用这个包。所以我启动了 Visual Studio 并选择了一个 vanilla 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 核心库之间存在冲突,但我不确定如何缩小和纠正它。关于如何解决或解决解决方案的任何想法,将不胜感激!

3 月 17 日在评论@will-x 后编辑

我已经在 netstandard 1.6 上重建了 nuget 包。这涉及安装更多软件包以填补 corefx 中的空白并适本地重新编写一些代码。 Newtonsoft.Json 仍然是最新版本,11.0.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发布过程的views预编译步骤出现异常:

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 项目在 Debug模式下使用最新的 NewtonSoft.Json 包成功运行。然而, View 预编译仍然失败。这是我放弃的时候。

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

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

关于asp.net-core - .Net Standard 2 包中的 Newtonsoft.Json 依赖项导致 ASP.Net Core MVC 2.0 应用程序中的运行时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49321760/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com