【问题标题】:.NET Core 3.1 Api Using NewtonsoftJson causes Exception.NET Core 3.1 Api 使用 NewtonsoftJson 导致异常
【发布时间】:2021-10-02 21:12:57
【问题描述】:

这是我在 Startup.cs 中对 NewtonsoftJson 的声明

services.AddControllers()
.AddNewtonsoftJson(options =>
{
    options.SerializerSettings.ContractResolver = new DefaultContractResolver();
    options.SerializerSettings.Culture = new CultureInfo(culture);
});

我已经将 Microsoft.AspNetCore.Mvc.NewtonsoftJson 包添加到我的项目中。但是,当我尝试运行它时,我得到下面指向上面代码的异常。我也应该加载 PresentationFramework 吗?我不记得需要为我的其他 API 使用它

System.IO.FileNotFoundException: '无法加载文件或程序集 'PresentationFramework,版本=4.0.0.0,文化=中性, PublicKeyToken=31bf3856ad364e35'。系统找不到文件 指定。'

编辑:

我已经更新了下面的代码。但是,错误仍然出现:

var culture = "en-US";
CultureInfo.CurrentCulture = new CultureInfo(culture, false);
services.AddControllers()
    .AddNewtonsoftJson(options =>
    {
        options.SerializerSettings.ContractResolver = new DefaultContractResolver();
        options.SerializerSettings.Culture = new CultureInfo(CultureInfo.CurrentCulture.Name);
    });

编辑 2:

我已经注释掉了 AddNewtonSoftJson 部分并保留了 services.AddControllers。这是导致异常的部分。

编辑 3:

这就是我加载应用设置的方式。

var builder = new ConfigurationBuilder()
    .SetBasePath(hostingEnvironment.ContentRootPath)
    .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
    configuration = builder.Build();

这是堆栈跟踪:

在 System.ModuleHandle.ResolveTypeHandleInternal(RuntimeModule 模块, Int32 typeToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext) 在 System.Reflection.RuntimeModule.ResolveType(Int32 metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments) 在 System.Reflection.CustomAttribute.FilterCustomAttributeRecord(元数据令牌 caCtorToken, MetadataImport& 范围, RuntimeModule decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, ListBuilder1& derivedAttributes, RuntimeType& attributeType, IRuntimeMethodInfo& ctor, Boolean& ctorHasParameters, Boolean& isVarArg) at System.Reflection.CustomAttribute.AddCustomAttributes(ListBuilder1& 属性,RuntimeModule 装饰模块,Int32 decoratedMetadataToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, ListBuilder1 derivedAttributes) at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeModule decoratedModule, Int32 decoratedMetadataToken, Int32 pcaCount, RuntimeType attributeFilterType) at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeAssembly assembly, RuntimeType caType) at System.Reflection.RuntimeAssembly.GetCustomAttributes(Type attributeType, Boolean inherit) at System.Attribute.GetCustomAttributes(Assembly element, Type attributeType, Boolean inherit) at System.Attribute.GetCustomAttribute(Assembly element, Type attributeType, Boolean inherit) at System.Reflection.CustomAttributeExtensions.GetCustomAttribute[T](Assembly element) at Microsoft.AspNetCore.Mvc.ApplicationParts.ApplicationPartFactory.GetApplicationPartFactory(Assembly assembly) at Microsoft.AspNetCore.Mvc.ApplicationParts.ApplicationPartManager.PopulateDefaultParts(String entryAssemblyName) at Microsoft.Extensions.DependencyInjection.MvcCoreServiceCollectionExtensions.GetApplicationPartManager(IServiceCollection services) at Microsoft.Extensions.DependencyInjection.MvcCoreServiceCollectionExtensions.AddMvcCore(IServiceCollection services) at Microsoft.Extensions.DependencyInjection.MvcServiceCollectionExtensions.AddControllersCore(IServiceCollection services) at Microsoft.Extensions.DependencyInjection.MvcServiceCollectionExtensions.AddControllers(IServiceCollection services) at SDR_Filestore.Api.Startup.ConfigureServices(IServiceCollection services) in C:\Users\user1\source\repos\DotNetCore\appo_one\Fileserver.Api\Startup.cs:line 83 at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.InvokeCore(Object instance, IServiceCollection services) at Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.<>c__DisplayClass9_0.<Invoke>g__Startup|0(IServiceCollection serviceCollection) at Microsoft.AspNetCore.Hosting.StartupLoader.ConfigureServicesDelegateBuilder1.c__DisplayClass15_0.g__RunPipeline|0(IServiceCollection 服务)在 Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.Invoke(对象 例如,IServiceCollection 服务)在 Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.c__DisplayClass8_0.b__0(IServiceCollection 服务)在 Microsoft.AspNetCore.Hosting.StartupLoader.ConfigureServicesDelegateBuilder`1.c__DisplayClass14_0.g__ConfigureServicesWithContainerConfiguration|0(IServiceCollection 服务)在 Microsoft.AspNetCore.Hosting.ConventionBasedStartup.ConfigureServices(IServiceCollection 服务)

【问题讨论】:

  • 你是如何设定文化价值的?
  • 在.AddNewtonsoftJson 之外使用new CultureInfo(culture) 有效吗?
  • 我正在从我的配置中获得文化。我已经检查过那里已经有一个值了。
  • 我如何在外面设置文化?
  • 你是如何加载你的配置的?

标签: c# asp.net-core .net-core


【解决方案1】:

等待此错误:System.IO.FileNotFoundException: 'Could not load file or assembly 'PresentationFramework, Version=4.0.0.0

它看起来像一个完整的 .Net 参考,不是 .net 核心。 PresentationFramework 是 2008 年与 Full .Net 3.0 一起推出的 WPF。您能否运行 ProcessMonitor 并告诉我们 Visual Studio 在哪里寻找该 DLL?

在运行 Visual Studio 解决方案之前快速启动 ProcessMonitor 跟踪,然后在 Visual Studio 引发错误后快速停止 ProcessMonitor 跟踪。然后在 ProcessMonitor 中进行搜索 获取 DLL 的名称并查看它是从哪里加载的。

您可能会发现问题在于您的 .Net Core 3.1 引用之一也在其中打包了一个 WPF 应用程序。这导致了 PresentationFramework 被请求。

参考,我自己的赏金答案:

【讨论】:

    【解决方案2】:

    可能是 nuget 包的问题,​​请尝试删除包并重新安装它

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-05
      • 1970-01-01
      • 2020-12-07
      相关资源
      最近更新 更多