How to migrate ASP.NET Core 5 code to ASP.NET Core 6
[ad_1]
Microsoft’s ASP.Internet Core 6, which has been obtainable for production use because November 8, introduces a simplified internet hosting product that cuts down the boilerplate code that you would otherwise need to have to create to get your ASP.Internet Main application up and operating. ASP.Internet Main 6 makes a little bit less difficult to create a new world wide web software from scratch, as opposed with ASP.Internet Main 5.
But what if you want to update an ASP.Net Core 5 project to ASP.Web Main 6? In that circumstance, you should be knowledgeable of the code you will need to have to publish to migrate ASP.Internet Main 5 code to ASP.Web Core 6. This article offers many code samples that show how you can do this.
To do the job with the code examples delivered in this post, you should really have Visible Studio 2022 put in in your program. If you never by now have a copy, you can download Visual Studio 2022 here.
Produce an ASP.Internet Core Internet API job in Visible Studio 2022
To start with off, let us produce an ASP.Internet Core job in Visible Studio 2022. Following these actions will produce a new ASP.Net Core Net API 6 undertaking in Visible Studio 2022:
- Start the Visual Studio 2022 IDE.
- Click on on “Create new undertaking.”
- In the “Create new project” window, find “ASP.Internet Main Web API” from the record of templates shown.
- Click Following.
- In the “Configure your new project” window, specify the title and location for the new job.
- Optionally test the “Place solution and venture in the exact directory” test box, based on your preferences.
- Click Future.
- In the “Additional Information” window shown subsequent, guarantee that the verify box that claims “Use controllers…” is checked, as we’ll be employing controllers instead of small APIs in this illustration. Go away the “Authentication Type” established to “None” (default).
- Make certain that the verify boxes “Enable Docker,” “Configure for HTTPS,” and “Enable Open up API Support” are unchecked as we will not be applying any of those people characteristics listed here.
- Click Build.
We’ll use this ASP.Web Core 6 Net API challenge to illustrate migrations of ASP.Net Core 5 code to ASP.Internet Main 6 in the subsequent sections of this report.
The Plan course in ASP.Internet Main 5
The subsequent code snippet illustrates what a common Program class appears to be like like in ASP.Web Main 5.
community class Method
public static void Primary(string[] args)
CreateHostBuilder(args).Build().Operate()
community static IHostBuilder CreateHostBuilder(string[] args)
return Host.CreateDefaultBuilder(args).
ConfigureWebHostDefaults(x => x.UseStartup())
The System course in ASP.Internet Core 6
With the introduction of the simplified internet hosting product in ASP.Internet Core 6, you no for a longer period have to use the Startup class. You can read through extra about this in my before report right here. Here’s how you would generate a typical Software class in ASP.Net Main 6:
var builder = WebApplication.CreateBuilder(args)
// Add products and services to the container
builder.Solutions.AddControllers()
var application = builder.Construct()
// Configure the HTTP ask for pipeline
app.UseAuthorization()
application.MapControllers()
application.Operate()
Incorporate middleware in ASP.Web Core 5
The next code snippet demonstrates how you can incorporate a middleware element in ASP.Web Core 5. In our example, we’ll include the response compression middleware.
general public class Startup
general public void Configure(IApplicationBuilder app)
application.UseResponseCompression()
Insert middleware in ASP.Web Main 6
To add a middleware element in ASP.Internet Core 6, you can use the next code.
var builder = WebApplication.CreateBuilder(args)
var app = builder.Develop()
application.UseResponseCompression()
application.Operate()
Include routing in ASP.Web Main 5
To insert an endpoint in ASP.Net Core 5, you can use the following code.
general public course Startup
general public void Configure(IApplicationBuilder app)
application.UseRouting()
app.UseEndpoints(endpoints =>
endpoints.MapGet("/examination", () => "This is a examination concept.")
)
Increase routing in ASP.Internet Main 6
You can increase an endpoint in ASP.Internet Main 6 utilizing the pursuing code.
var builder = WebApplication.CreateBuilder(args)
var application = builder.Create()
app.MapGet("/examination", () => "This is a exam concept.")
app.Operate()
Be aware that in ASP.Web Main 6 you can incorporate endpoints to WebApplication with out having to make express phone calls to the UseRouting or UseEndpoints extension strategies.
Add providers in ASP.Internet Main 5
The following code snippet illustrates how you can increase providers to the container in ASP.Net Core 5.
community course Startup
general public void ConfigureServices(IServiceCollection solutions)
// Add created-in companies
providers.AddMemoryCache()
services.AddRazorPages()
expert services.AddControllersWithViews()
// Increase a personalized support
services.AddScoped()
Increase expert services in ASP.Web Main 6
To include companies to the container in ASP.Net Core 6, you can use the next code.
var builder = WebApplication.CreateBuilder(args)
// Increase developed-in expert services
builder.Services.AddMemoryCache()
builder.Companies.AddRazorPages()
builder.Solutions.AddControllersWithViews()
// Incorporate a customized assistance
builder.Companies.AddScoped()
var application = builder.Develop()
Check an ASP.Web Core 5 or ASP.Web Core 6 software
You can take a look at an ASP.Internet Core 5 application using both TestServer or WebApplicationFactory. To take a look at employing TestServer in ASP.Net Core 5, you can use the pursuing code snippet.
[Fact]
community async Activity GetProductsTest()
using var host = Host.CreateDefaultBuilder()
.ConfigureWebHostDefaults(builder =>
builder.UseTestServer()
.UseStartup()
)
.ConfigureServices(services =>
solutions.AddSingleton()
)
.Establish()
await host.StartAsync()
var customer = host.GetTestClient()
var reaction = await client.GetStringAsync("/getproducts")
Assert.Equal(HttpStatusCode.Okay, reaction.StatusCode)
The pursuing code snippet displays how you can check your ASP.Net Core 5 application applying WebApplicationFactory.
[Fact]
general public async Endeavor GetProductsTest()
var software = new WebApplicationFactory()
.WithWebHostBuilder(builder =>
builder.ConfigureServices(solutions =>
companies.AddSingleton()
)
)
var client = software.CreateClient()
var response = await consumer.GetStringAsync("/getproducts")
Assert.Equal(HttpStatusCode.Alright, response.StatusCode)
You can use the exact code to exam working with TestServer or WebApplicationFactory in .Net 5 and .Net 6.
Add a logging company in ASP.Internet Main 5
Logging providers in ASP.Internet Core are used to shop logs. The default logging suppliers bundled in ASP.Internet Core are the Debug, Console, EventLog, and EventSource logging companies.
You can use the ClearProviders approach to crystal clear all logging providers and add a unique logging company or your own tailor made logging supplier. The adhering to code snippet illustrates how you can get rid of all ILoggerProvider situations and incorporate the Console logging service provider in ASP.Internet Main 5.
community static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureLogging(logging =>
logging.ClearProviders()
logging.AddConsole()
)
.ConfigureWebHostDefaults(webBuilder =>
webBuilder.UseStartup()
)
Add a logging supplier in ASP.Net Core 6
In ASP.Web Main 6, when you get in touch with WebApplication.CreateBuilder, it adds the Console, Debug, EventLog, and EventSource logging companies. The adhering to code snippet demonstrates how you can obvious the default logging vendors and incorporate only the Console logging company in ASP.Net Main 6.
var builder = WebApplication.CreateBuilder(args)
//Obvious default logging providers
builder.Logging.ClearProviders()
//Code to increase providers to the container
builder.Logging.AddConsole()
var application = builder.Establish()
The code illustrations furnished right here illustrate the distinctive techniques we incorporate middleware, routing, providers, and logging suppliers in ASP.Net Main 5 and in ASP.Internet Core 6, as well as dissimilarities in the Plan course and testing. These snippets must support you when functioning with ASP.Internet Main 6 apps, and get you off to a very good commence when you migrate your ASP.Internet Main 5 purposes to ASP.Web Core 6.
Copyright © 2022 IDG Communications, Inc.
[ad_2]
Source url