Some notes on upgrading to .NET 7
The official release for .NET 7.0 is in November 2022, though there have been a number of preview versions this year. In order to upgrade services to this, it might be necessary to update Visual Studio and MSBuild first.
Target Framework
To upgrade, set the target framework as follows:
<TargetFramework>net7.0</TargetFramework>
And maybe do the following update all package references to 7.0.0 for Microsoft.AspNetCore and Microsoft.Extensions.
Other target frameworks include:
- net7.0
- net7.0-android
- net7.0-ios
- net7.0-maccatalyst
- net7.0-macos
- net7.0-tvos
- net7.0-windows
Possible breaking changes for upgraded services
More things are optimised for mobile devices when developing Web applications, with better integration between Blazor and MAUI.
Assembly trimming: With .NET 7, this is done by default for console applications. We might be able to test whether this would break services to be upgraded by setting the following attribute in the project file:
<IsTrimmable>true</IsTrimmable>
Authentication method: This has something to do with .NET 7 selecting a given authentication method as the default. Might need to use the following to set a specific method:
builder.Services.AddAuthentication().AddOAuth("MyDefaultScheme");
Runtime server: Kestrel default address and port have been removed, and these must be set specifically in launchSettings.json. Previously it would serve the application at http://localhost:5000 and https://localhost:5001, which happens to be the case for some of our APIs.
Rate limiting for MVC and API controllers: The [EnableRateLimitingAttribute(“PolicyName”)] attribute can be set for a given controller, with the rate limiting policies specified in builder.Services.AddRateLimiter().
Container Support
Apparently all that’s needed to get a container up and running is something like the following few commands:
dotnet new mvc -n my-container-app
cd my-container-app
dotnet add package Microsoft.NET.Build.Containers
dotnet publish --os linux --arch x64 -c Release -p:PublishProfile=DefaultContainer
docker run -it --rm -p 5010:80 my-container-app:1.0.0
The base image to use in the Dockerfile is mcr.microsoft.com/dotnet/sdk:7.0.