Migration to .NET Core 3.1 - Netwonsoft missing

空扰寡人 提交于 2020-08-20 11:12:19

问题


I just migrated to ASP.NET Core 3.1 from 2.2 and I am getting this error:

System.NotSupportedException: The collection type 'System.Collections.Generic.Dictionary`2[System.Object,System.Object]' is not supported.
   at System.Text.Json.JsonClassInfo.GetElementType(Type propertyType, Type parentType, MemberInfo memberInfo, JsonSerializerOptions options)
   at System.Text.Json.JsonClassInfo.CreateProperty(Type declaredPropertyType, Type runtimePropertyType, Type implementedPropertyType, PropertyInfo propertyInfo, Type parentClassType, JsonConverter converter, JsonSerializerOptions options)
   at System.Text.Json.JsonClassInfo.AddProperty(Type propertyType, PropertyInfo propertyInfo, Type classType, JsonSerializerOptions options)
   at System.Text.Json.JsonClassInfo.AddPolicyProperty(Type propertyType, JsonSerializerOptions options)
   at System.Text.Json.JsonClassInfo..ctor(Type type, JsonSerializerOptions options)
   at System.Text.Json.JsonSerializerOptions.GetOrAddClass(Type classType)
   at System.Text.Json.WriteStackFrame.Initialize(Type type, JsonSerializerOptions options)
   at System.Text.Json.JsonSerializer.WriteAsyncCore(Stream utf8Json, Object value, Type inputType, JsonSerializerOptions options, CancellationToken cancellationToken)
   at Microsoft.AspNetCore.Mvc.Formatters.SystemTextJsonOutputFormatter.WriteResponseBodyAsync(OutputFormatterWriteContext context, Encoding selectedEncoding)
   at Microsoft.AspNetCore.Mvc.Formatters.SystemTextJsonOutputFormatter.WriteResponseBodyAsync(OutputFormatterWriteContext context, Encoding selectedEncoding)

However, I installed this package Microsoft.AspNetCore.Mvc.NewtonsoftJson and also added services.AddControllers().AddNewtonsoftJson() in Startup

So why Newtonsoft is still ignored and System.Text.Json is used?

EDIT: Code throwing error:

public async Task<Dictionary<object, object>> GetTemplatesDictAsync(
           int? from = 0,
            int? take = 100,
            string search = null)
        {
            var _templates = await _repository.GetAllAsync(from, take, search, );

            var _dict = _templates.ToDictionary(t => (object)t.id, t => (object)t);

            // also append a property with original list
            _dict.Add("list", _templates);

            return _dict;
        }

Note: I changed Dictionary<object, object> to Dictionary<string, object> and code works. Question, though, is why Newtonsoft.Json is not used.

EDIT2:

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddAuthentications(Configuration);
            services.AddAutoMapping();
            services.AddMemoryCache();
            services.AddOData();

            // 3.1:
            services.AddControllers()
                        .AddNewtonsoftJson()
                    ;

           // used in 2.2:
           // var mvcCoreBuilder = services.AddMvcCore();
            // mvcCoreBuilder
            //     .AddFormatterMappings()
            //     .AddJsonFormatters()


Thanks


回答1:


In .NET Core 3+ projects, you have a different set of calls to replace MVC. So you’ll probably have one of the following :

services.AddControllers().AddNewtonsoftJson();
services.AddControllersWithViews().AddNewtonsoftJson();
services.AddRazorPages().AddNewtonsoftJson();

If this does not work, please tell us a little more where you are using this.

You can find a more step by step approach at here It worked for me.



来源:https://stackoverflow.com/questions/61204435/migration-to-net-core-3-1-netwonsoft-missing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!