Error while downgrading project from .net 5 to .net core 3.1

Tanul 1,281 Reputation points
2021-01-07T20:25:58.847+00:00

Team,

I need to upload my .net core app service on azure stack which only support .net core 3.1.

I've made whole project in .net 5 but after downgrading, application starting throwing errors. I'm using System.Text.Json for Json serialization and in startup.cs I've added this to drop all the null values while deserializing the object stream

public void ConfigureServices(IServiceCollection services){
     services.AddControllers().AddJsonOptions(options =>
                {
                    options.JsonSerializerOptions.IgnoreNullValues = true;
                });
}

This error is coming

Value cannot be null. (Parameter 'configure') in services.AddControllers()

Here is the complete exception

Unhandled exception. System.ArgumentNullException: Value cannot be null. (Parameter 'configure')
   at Microsoft.Extensions.DependencyInjection.AuthorizationServiceCollectionExtensions.AddAuthorizationCore(IServiceCollection services, Action`1 configure)
   at Microsoft.Extensions.DependencyInjection.PolicyServiceCollectionExtensions.AddAuthorization(IServiceCollection services, Action`1 configure)
   at Microsoft.Extensions.DependencyInjection.PolicyServiceCollectionExtensions.AddAuthorization(IServiceCollection services)
   at Microsoft.Extensions.DependencyInjection.MvcCoreMvcCoreBuilderExtensions.AddAuthorizationServices(IServiceCollection services)
   at Microsoft.Extensions.DependencyInjection.MvcCoreMvcCoreBuilderExtensions.AddAuthorization(IMvcCoreBuilder builder)
   at Microsoft.Extensions.DependencyInjection.MvcServiceCollectionExtensions.AddControllersCore(IServiceCollection services)
   at Microsoft.Extensions.DependencyInjection.MvcServiceCollectionExtensions.AddControllers(IServiceCollection services)
   at Project.Startup.ConfigureServices(IServiceCollection services)
   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.ConfigureServicesBuilder.Invoke(Object instance, IServiceCollection services)
   at Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.<>c__DisplayClass8_0.<Build>b__0(IServiceCollection services)
   at Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.UseStartup(Type startupType, HostBuilderContext context, IServiceCollection services)
   at Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.<>c__DisplayClass12_0.<UseStartup>b__0(HostBuilderContext context, IServiceCollection services)
   at Microsoft.Extensions.Hosting.HostBuilder.CreateServiceProvider()
   at Microsoft.Extensions.Hosting.HostBuilder.Build()
   at Project.Program.Main(String[] args)

Can anyone help please.

Thank you

Azure Stack Hub
Azure Stack Hub
An extension of Azure for running apps in an on-premises environment and delivering Azure services in a datacenter.
189 questions
.NET Runtime
.NET Runtime
.NET: Microsoft Technologies based on the .NET software framework.Runtime: An environment required to run apps that aren't compiled to machine language.
1,166 questions
0 comments No comments
{count} vote

6 answers

Sort by: Most helpful
  1. Sean Amos 1 Reputation point
    2021-04-28T21:44:21.49+00:00

    I've run into many of these problems before and I'm quite sure it's because Microsoft doesn't put proper dependency version constraints on their packages.

    All their packages tend to have a lower bound constraint and no upper bound constraint, they should look like (version >=3 && version < 4), or in nuget's notation: [3.0,4.0). The packages MS push just have (version >= 3).

    The effect of this is nuget thinks it's safe to pull in 5.x packages as a transitive dependency of a 3.x package and it blows up at runtime. A 3.x package should only pull in 3.x dependencies.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.