Asp.net core 3.0 how to register custom model binder

0

I'm trying to register a custom model binder in asp.net(core 3.0) startup.cs based on the following examples:

Link 1

Link 2

The code below is not working in asp.net core 3.0, can someone please help - thanks!

 services.AddMvc()
    .AddMvcOptions(options =>
    {
        IHttpRequestStreamReaderFactory readerFactory = services.BuildServiceProvider().GetRequiredService<IHttpRequestStreamReaderFactory>();
        options.ModelBinderProviders.Insert(0, new NtBodyModelBinderProvider(options.InputFormatters, readerFactory));
    });

I'M GETTING THE FOLLOWING ERROR WHEN I TRY TO RUN THE APPLICATION.

System.AggregateException HResult=0x80131500 Message=Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Mvc.ModelBinding.IModelBinderProvider Lifetime: Scoped ImplementationType: CustomerCash.External.Api.ModelBinders.NtBodyModelBinderProvider': Unable to resolve service for type 'System.Collections.Generic.IList1[Microsoft.AspNetCore.Mvc.Formatters.IInputFormatter]' while attempting to activate 'CustomerCash.External.Api.ModelBinders.NtBodyModelBinderProvider'.) (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Mvc.ModelBinding.IModelBinder Lifetime: Scoped ImplementationType: CustomerCash.External.Api.ModelBinders.NtBodyModelBinder': Unable to resolve service for type 'System.Collections.Generic.IList1[Microsoft.AspNetCore.Mvc.Formatters.IInputFormatter]' while attempting to activate 'CustomerCash.External.Api.ModelBinders.NtBodyModelBinder'.) Source=Microsoft.Extensions.DependencyInjection StackTrace: at Microsoft.Extensions.DependencyInjection.ServiceProvider..ctor(IEnumerable1 serviceDescriptors, ServiceProviderOptions options) at Microsoft.Extensions.DependencyInjection.ServiceCollectionContainerBuilderExtensions.BuildServiceProvider(IServiceCollection services, ServiceProviderOptions options) at Microsoft.Extensions.DependencyInjection.DefaultServiceProviderFactory.CreateServiceProvider(IServiceCollection containerBuilder) at Microsoft.Extensions.Hosting.Internal.ServiceFactoryAdapter1.CreateServiceProvider(Object containerBuilder) at Microsoft.Extensions.Hosting.HostBuilder.CreateServiceProvider() at Microsoft.Extensions.Hosting.HostBuilder.Build() at CustomerCash.External.Api.Program.Main(String[] args) in C:\Development\JamesHay\jhcode\CustomerCashExternal\src\CustomerCash.External.Api\Program.cs:line 11

This exception was originally thrown at this call stack: [External Code]

Inner Exception 1: InvalidOperationException: Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Mvc.ModelBinding.IModelBinderProvider Lifetime: Scoped ImplementationType: CustomerCash.External.Api.ModelBinders.NtBodyModelBinderProvider': Unable to resolve service for type 'System.Collections.Generic.IList`1[Microsoft.AspNetCore.Mvc.Formatters.IInputFormatter]' while attempting to activate 'CustomerCash.External.Api.ModelBinders.NtBodyModelBinderProvider'.

Inner Exception 2: InvalidOperationException: Unable to resolve service for type 'System.Collections.Generic.IList`1[Microsoft.AspNetCore.Mvc.Formatters.IInputFormatter]' while attempting to activate 'CustomerCash.External.Api.ModelBinders.NtBodyModelBinderProvider'.

c#
asp.net
asked on Stack Overflow Apr 5, 2020 by Sam J • edited Apr 5, 2020 by Sam J

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0