With the release of .NET 8, many Blazor developers will want to take advantage of the new features and improvements it brings. Updating a Blazor Server app to .NET 8 is fairly straightforward, but there are a few steps to follow to ensure a smooth transition.
In this blog post, we’ll walk through the full process of updating an existing Blazor Server app to target .NET 8 instead of the previous version. By the end, you’ll have your app running on the latest .NET release and able to leverage everything it has to offer.
Check Project and Package References
Before making any code changes, it’s a good idea to inspect your project and package references to see how things may need to be updated. Open your Blazor Server app’s *.csproj file and take note of:
The TargetFramework value – This should currently be something like net6.0. Make sure it is updated to net8.0 once you upgrade.
Any package references with version constraints pinned to previous .NET versions. These will need relaxing to allow upgrading to newer package versions compatible with .NET 8.
You’ll also want to check the version numbers of any NuGet packages your project directly references. Some may have breaking changes or require upgrading due to dependency changes introduced with .NET 8.
Taking inventory now makes updating references and dependencies a smoother process later on. It’s also a good opportunity to clean up any unused or unnecessary packages before upgrading.
Update to .NET 8 SDK
With project context checked, it’s time to install the .NET 8 SDK if not already present. Open a command prompt and run:
dotnet tool install -g dotnet-sdk-8.0.xxx
Replace xxx with the specific .NET 8 SDK version you want, like 8.0.100-preview.7.22354.2. Once installed, update your project file to use .NET 8:
<TargetFramework>net8.0</TargetFramework>
This tells the SDK which version of .NET to target for building and running your app.
Update Packages
Now that your project is configured for .NET 8, you can update any NuGet packages to their latest compatible versions. A good first step is to restore packages:
dotnet restore
This will install or update any packages and dependencies to work with .NET 8. You may also need to manually update specific packages listed in your *.csproj if they have major version changes.
Core Blazor packages like Microsoft.AspNetCore.Components and Microsoft.AspNetCore.Components.Web should update automatically. But check the release notes of any third party packages for special upgrade instructions.
Compile and Test
Now you’re ready to try building and running your app on .NET 8. From the command line:
dotnet build
dotnet run
This compiles and starts the dev server to run your Blazor Server app targeted at .NET 8. Take some time to test all areas of the app – there may be runtime errors or compiler warnings to address from the upgrade.
Some things to check include Razor components rendering properly, API calls working as expected, and any specialized features or plugins from third-party packages. Thorough testing at this point helps catch issues early.
Fix Incompatibilities
While the core .NET update process aims to be non-breaking, you may encounter runtime exceptions or compilation errors that need fixing:
- API changes in updated NuGet packages
- Removed features no longer supported in .NET 8
- Code using obsolete APIs or patterns
Take note of error messages and review the .NET release notes to understand what may be causing issues. Common fixes include:
- Updating code to use replacement APIs if methods were deprecated
- Removing unused code no longer necessary
- Upgrading third party packages that depend on updated .NET APIs
Addressing these compiler and runtime errors one by one cleans up any remaining incompatibilities from the upgrade.
Also Read: What is the difference Between .NET 7 and .NET 8?
Update Runtime Host
Blazor Server apps rely on the ASP.NET Core host to run the server-side app. You’ll need to ensure your project references are configured to use the latest version compatible with .NET 8.
This typically involves updating packages like:
- Microsoft.AspNetCore.App
- Microsoft.AspNetCore.Hosting
- Microsoft.AspNetCore.Server.Kestrel
Once package references are aligned for .NET 8 runtime hosting, rebuilding and running your project should work without issues.
Finish and Deploy
After resolving all compilation errors and testing thoroughly on your local machine, your Blazor Server app is ready to be deployed targeting .NET 8 in production.
Some final checks:
- Commit updated project and package files to source control
- Make sure app runs as expected when published/deployed
- Monitor logs and telemetry for any runtime issues post-deployment
- Consider re-architecting parts of the app to take advantage of new .NET 8 APIs
And with that, your Blazor Server application is now up-to-date and running on the latest .NET 8 platform release! Be sure to continually update packages used in your project as new versions come out as well.
The post Updating Your Blazor Server App to .NET 8 appeared first on Datafloq.