Articles .NET Core 3.1 Migration - Quick Reference for WEB API Projects by Anish Ravindran

emailx45

Местный
Регистрация
5 Май 2008
Сообщения
3,571
Реакции
2,438
Credits
573
.NET Core 3.1 Migration - Quick Reference for WEB API Projects
Anish Ravindran - 07/Jun/2020
[SHOWTOGROUPS=4,20]
Quick basic reference to migrate Web API projects to .NET Core 3.1

In this basic reference for migrating Web API projects to .NET Core 3.1 from the previous major framework, we will cover only the common breaking changes, not each and every aspect of migration to .NET Core 3.1.

Background
The support life span of the previous .NET Core version like 2.2 is stopped, projects need to migrate to the latest stable .NET core version 3.1.

Using the Code

Step 1 - Environment Setup
  • Install Visual Studio 2019 if not available; once installed, you will be having the required framework .NET Core 3.1.
  • Install .NET Core 3.1 SDK if not available in current VS 2019.
  • A quick check for the environment can be done as below:
    Код:
    C:\>dotnet --version
    3.1.100
Step 2 - Framework Upgrade
  1. Open the project in VS 2019 you would like to migrate and change the framework to 3.1 for each project as part of the solution. Select the properties option to change the same.Note
    .NET Core 3.1 requires Visual Studio 2019 version 16.5. Check your version of the Visual Studio. If it's not 16.5, update it and try again.
1591587554864.png

2. Remove the reference to Microsoft.AspNetCore.App; this is obsolete from 3.0 onwards.

1591587579262.png

1591587585134.png

Step 3 - Changes in Startup.cs File

Note
The previous version code and the change is provided sequentially.
  1. Change the compatibility version as given below:
    Код:
    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
    
    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
  2. Change the dependency injection for IHostingEnvironment to IWebHostEnvironment.
    Код:
    public void Configure(IApplicationBuilder app,IHostingEnvironment env)
    Import the below namespace to include the change:
    Код:
    using Microsoft.Extensions.Hosting;
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  3. MVC pipeline change:
    Код:
    app.UseMvc();
    After the change, this will be as below (app.UseMVC() is deprecated):
    Код:
    app.UseRouting();
    app.UseEndpoints(endpoints =>
    {
    endpoints.MapControllers();
    });
Step 4 - Swagger Changes (This is Relevant if Swagger is Used for API)

Import latest Swashbuckle.AspNetCore if you are using old versions using Nuget (the version is 5.4.1 at this time).

1591587701270.png

Library changes:
Код:
services.AddSwaggerGen(x =>
{
x.SwaggerDoc("v1", new Info
{
Title = "My API",
Version = "v1",
});
});

Add the below namespace (Explanation - Microsoft is using OpenAPI specification models):
Код:
using Microsoft.OpenApi.Models;
services.AddSwaggerGen(x =>
{
x.SwaggerDoc("v1", new OpenApiInfo
{
Title = "My API",
Version = "v1",
});
});

Step 5 - CORS Changes (This is Relevant if CORS Policy is Defined)

(Explanation - Mutually exclusive CORS policy from 3.0 onwards, System.InvalidOperationException: 'The CORS protocol does not allow specifying a wildcard (any) origin and credentials at the same time. Configure the CORS policy by listing individual origins if credentials need to be supported.')
Код:
services.AddCors(policy =>
{
policy.AddPolicy("Default", builder =>
{
builder.AllowAnyOrigin()
.AllowAnyHeader()
.AllowCredentials()
.AllowAnyMethod()
.Build();
});
});
Код:
services.AddCors(policy =>
{
policy.AddPolicy("Default", builder =>
{
builder.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod()
.Build();
});
});

Step 6 - Final Clean and Build

Do a solution clean and build.

At this point, your code will be successfully migrated to .NET Core 3.1 and should run as expected.

Reference
History
  • V 1.0 - First version

License
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)
[/SHOWTOGROUPS]