Luke1988
Luke1988

Reputation: 2098

Migration to .NET Core 3.1 - Netwonsoft missing

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

Upvotes: 0

Views: 5040

Answers (1)

Md Monjur Ul Hasan
Md Monjur Ul Hasan

Reputation: 1791

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.

Upvotes: 4

Related Questions