.Net Standard 2软件包中的Newtonsoft.Json依赖项导致ASP.Net Core MVC 2.0 app

时间:2018-03-16 13:17:53

标签: asp.net-core

对于长标题感到抱歉!我对dotnet核心很陌生,所以我怀疑我在这里遇到依赖问题,但我不能解决原因。这是:

我已将.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并选择了一个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。

这在我看来并不理想,但我的发展仍在继续。我相当肯定我的顾客不会给猴子,但是我做了;如果可能的话,我更愿意使用最新版本的一切,但如果有问题,我认为实用主义会胜出。

2 个答案:

答案 0 :(得分:0)

我遇到了同样的问题。

我正在维护一个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。

答案 1 :(得分:0)

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

.net核心mvc中间件依赖于此版本,我在其他项目中引用了更新版本。