I’m documenting all the steps to upgrade a .NET 3.1 WebAPI to .NET for a work project. Having watched several videos and found that many were missing steps which made the learning process quite difficult.
Below are the steps I used to upgrade my .NET WebAPI from .NET 3.1 to .NET 6. There are several changes on how the application starts up so let’s get in to it shall we. 🙂
Tip: Before we even begin, the step that many of the videos left out was that you have to remove the <Startup></Startup> reference in your .csproj file. Don’t forget this step or else you’ll get an error.
Upgrading a .NET WebAPI project from .NET 3.1 to .NET 6 involves several steps, from updating the target framework to modifying certain parts of the code to be compatible with the new version. .NET 6 introduces many performance improvements, new features, and simplifies the project structure, making the upgrade worthwhile. Here’s a step-by-step guide to help you through the process:
1. Prerequisites
- Install the latest version of the .NET 6 SDK from the .NET website.
- Update your development environment, such as Visual Studio, to a version that supports .NET 6 (Visual Studio 2022 or later).
- To see which version you have installed you can type the command: C:>
dotnet --list-sdks
2. Update the Target Framework
- Open your project file (
.csproj
), and change theTargetFramework
value fromnetcoreapp3.1
tonet6.0
.xmlCopy code<PropertyGroup> <TargetFramework>net6.0</TargetFramework> </PropertyGroup>
- You should also remove the
<Startup></Startup>
reference as it is no longer need and will cause errors if you leave it after the upgrade.
3. Update Package References
- Update all NuGet package references to their latest versions compatible with .NET 6. This can be done manually in the
.csproj
file or through the NuGet Package Manager in Visual Studio.
4. Update Program and Startup Configuration
.NET 6 introduces a new minimal hosting model, simplifying the startup process. Depending on your application’s complexity, you may choose to migrate to the new model or keep the existing Startup.cs
structure.
- For minimal hosting model (Recommended for new features):
- Combine
Program.cs
andStartup.cs
into a newProgram.cs
using the minimal hosting model.
var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddControllers(); // Configure other services like Swagger, CORS, Authentication here var app = builder.Build(); // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { app.UseDeveloperExceptionPage(); app.UseSwagger(); app.UseSwaggerUI(); } app.UseHttpsRedirection(); app.UseAuthorization(); app.MapControllers(); app.Run();
- Combine
- For traditional Startup configuration: Ensure compatibility but consider migrating in the future for a more streamlined configuration.
5. Migrate Configuration Files
- If your project uses
appsettings.json
and other configuration files, these should work without changes. However, verify any framework-specific settings for compatibility with .NET 6.
6. Fix Breaking Changes and Use New Features
- Review the official .NET 6 migration guide for breaking changes and obsolete APIs. You may need to modify parts of your code to address these changes.
- Consider adopting new .NET 6 features and improvements, such as improved performance, C# 10 language enhancements, and minimal APIs for streamlined projects.
7. Test the Application
- Thoroughly test your application to ensure that everything works as expected. Pay special attention to areas where you made changes or updates for compatibility.
- Utilize unit and integration tests if available, and consider adding them if not already in place to ensure your application’s functionality across updates.
8. Deploy
- Once testing is complete and you’re confident in the application’s stability, proceed with deployment using your preferred method (e.g., IIS, Docker, Kestrel).
This guide outlines the general process for upgrading a .NET WebAPI from .NET 3.1 to .NET 6. Depending on the specifics of your application, additional steps or modifications may be necessary. Always backup your project before starting the upgrade process, and consider testing the upgrade in a separate branch or environment to mitigate any potential issues.