简体   繁体   中英

Startup.cs migration from .net core 2.2 to .net core 3.1

I am migrating a .net core project to version 3.1, but I'm confused with this part of the startup.cs file:

services.AddControllers().AddNewtonsoftJson(opt =>
{
    opt.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
});

services.AddMvc(opt =>
{
    var policy = new AuthorizationPolicyBuilder()
        .RequireAuthenticatedUser()
        .AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme)
        .Build();
        
    opt.Filters.Add(new AuthorizeFilter(policy));
    opt.Filters.Add<ModelStateValidationFilter>();
    //opt.Filters.Add<DbContextTransactionFilter>(); 
    opt.Filters.Add<ExceptionsFilter>();
})       
    .AddFluentValidation(opt => opt.RegisterValidatorsFromAssemblyContaining<Business.Exceptions.NotFoundException>());

should I keep the addMvc()?

With this, I am getting a 500 internal server error.

AddControllers or AddControllersWithViews has replaced AddMvc in .NET Core 3

Quite a lot has changed in v3 so it would be good to review the migration guides.

Migration guide for v2 to v3

Docs example of changed Startup.cs in v3

If your error message is

Handler was not found for request of type MediatR.IRequestHandler System.InvalidOperationException

You can use MediatR's MediatR.Extensions.Microsoft.DependencyInjection package which includes a .AddMediatR() extension method, allowing you to register all handlers and pre/post-processors in a given assembly.

It will search the assembly for any Handlers, Profiles etc and register them for use.

public void ConfigureServices(IServiceCollection services)
{
  // ...

  // Adds all MediatR handlers from the Assembly containing Startup
  services.AddMediatR(typeof(Startup));
}

MediatR Docs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM