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

Tanul 1,251 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.
183 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,140 questions
0 comments No comments
{count} vote

6 answers

Sort by: Most helpful
  1. Vinay 11 Reputation points
    2021-01-09T08:50:42.957+00:00

    Hi @Jerry Cai-MSFT
    Looks like the gods had some mercy on me. after literally a sleepless night I managed to solve it.
    Here are the exact steps :

    Downgrade the Authorization package from <PackageReference Include="Microsoft.AspNetCore.Authorization" Version="5.0.1" /> to <PackageReference Include="Microsoft.AspNetCore.Authorization" Version="3.1.10" />. Which is the current .NETCore Version I am using.
    54952-image.png

    Then called services.AddAuthorizationCore() before services.AddMvc(). As it appeared to me from the error that AddAuthorizationCore() was not getting confiured. This is how your code will look. I don't think you'd need to pass the options and parameters.
    54908-image.png

    Alternatively you could try calling services.AddMvcCore() instead of services.AddMvc() as that worked in one of my attempts, before I tried this solution.

    2 people found this answer helpful.

  2. Jerry Cai-MSFT 986 Reputation points
    2021-01-08T03:43:25.967+00:00

    Hi,TaB

    How did you use the JsonSerialize?A JsonSerializerOptions constructor that specifies a set of defaults is not available in .NET Core 3.1.

    Check the official link:
    JsonSerializerOptions

    You can use the IgnoreNullValues in this way instead:

    var options = new JsonSerializerOptions { IgnoreNullValues = true };  
    var jsonStr = JsonSerializer.Serialize(model,options);  
    

    Best Regards,
    Jerry Cai


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  3. Vinay 11 Reputation points
    2021-01-08T18:58:51.313+00:00

    I also seem to get theis error after migrating from .netcore2.0 to .netcore3.1

    public void ConfigureServices(IServiceCollection services)
    {
    services.AddMvc(); // This is where the exception occurs
    ...
    }

    54906-image.png

    54933-image.png

    54869-image.png

    Strangely enough the same method with parameters was working fine in .netcore2.0
    54884-image.png


  4. Nikita Seplakovs 1 Reputation point
    2021-03-23T08:41:00.66+00:00

    I've just gone through same issue(hell) and finally managed to resolve it.
    I had to manually update package references in my projects (Microsoft.AspNetCore.Authorization specifically) and use 3.1.13 version.

    0 comments No comments

  5. James Milne 1 Reputation point
    2021-04-23T13:42:44.54+00:00

    I encountered a similar issue while preparing to upgrade my project from .netcore 3.1 to .net 5.

    I first updated several EF Framework and System.DirectoryServices nuget packages to 5.0.5. Everything was fine. I then updated Microsoft.Aspnetcore.Authorization to 5.0.5 and then started getting the error "Value cannot be null. (Parameter 'configure')" in startup when configuring services.AddMvc().

    According to Microsoft (https://learn.microsoft.com/en-us/dotnet/core/compatibility/aspnetcore) a breaking change "AddAuthorization overload moved to different assembly" may be the cause.

    As per @Amy Zheng

    Removing the Microsoft.Aspnetcore.Authorization nuget package completely solved the problem for me. It is no longer needed for my custom Authorization middleware.

    0 comments No comments