- •Table of Contents
- •About the Author
- •Acknowledgments
- •Introduction
- •Version Support
- •Supported Versions
- •A Unified Platform
- •Roadmap
- •Supported Operating Systems
- •Command Line Interface
- •Desktop Development
- •Blazor
- •MAUI
- •Wrapping Up
- •.NET 6 Architecture
- •Runtimes
- •CoreCLR
- •Mono
- •WinRT
- •Managed Execution Process
- •Desktop Packs
- •Wrapping Up
- •Dotnet New
- •Dotnet Restore
- •NuGet.config
- •Dotnet Build
- •Dotnet Publish
- •Dotnet Run
- •Dotnet Test
- •Using the CLI in GitHub Actions
- •Other Commands
- •Wrapping Up
- •WinAPI
- •WinForms
- •STAThread
- •WinForms Startup
- •DPI Mode
- •Responding to Scale Events
- •Visual Styles
- •Text Rendering
- •The Message Loop
- •The Form Designer
- •WPF Startup
- •XAML Layout
- •Visual Tree
- •Data Binding
- •Windows App SDK
- •Building a Windows App SDK application
- •Using Windows APIs with Windows App SDK
- •Packaging
- •Migrating to .NET 6
- •Upgrade Assistant
- •Wrapping Up
- •Blazor WebAssembly
- •Creating a Blazor Wasm Project
- •Blazor Progressive Web Apps
- •Exploring the Blazor Client Project
- •Blazor in .NET 6
- •Blazor Component System
- •Creating Blazor Pages
- •Running a Blazor App
- •Blazor Server
- •SignalR
- •Blazor Desktop
- •Wrapping Up
- •Project Structure
- •Exploring MAUI
- •The Cross-Platform World
- •Application Lifecycle
- •MVVM
- •MVVM Toolkit
- •Wrapping Up
- •Model-View-Controller
- •Routing
- •Views
- •Controllers
- •Controller-Based APIs
- •Minimal APIs
- •Wrapping Up
- •Web Apps
- •Creating an App Service
- •Static Web Apps
- •Web App for Containers
- •Docker
- •Azure Functions
- •Deploying Azure Functions
- •Wrapping Up
- •Record Types
- •Monolith Architecture
- •Microservices
- •Container Orchestration
- •Kubernetes
- •Docker Compose
- •Dapr
- •Installing Dapr
- •Dapr State Management
- •Wrapping Up
- •Roslyn
- •Compiler API
- •Diagnostic API
- •Scripting API
- •Workspace API
- •Syntax Tree
- •Roslyn SDK
- •Source Generators
- •Writing a Source Generator
- •Debugging Source Generators
- •Wrapping Up
- •Garbage Collector
- •The Heap
- •The Stack
- •Garbage Collection
- •A Look at the Threadpool
- •Async in .NET 6
- •Await/Async
- •Cancellations
- •WaitAsync
- •Conclusion
- •Index
Chapter 4 Desktop Development
Migrating to .NET 6
The basics of migrating an existing desktop application to .NET 6 is straightforward, of course depending on the complexity of the application. Migration comes in six steps.
\1.\ Run the portability analyzer.
\2.\ Migrate to PackageReference.
\3.\ Update to .NET 4.8.
\4.\ Switch the desktop project to .NET 6.
\5.\ Switch any class libraries to .NET 6.
\6.\ Run the application and test thoroughly.
First step is usually running a Visual Studio extension called “Portability Analyzer.” The documentation can be found at https://docs.microsoft.com/en-us/dotnet/ standard/analyzers/portability-analyzer. This extension will scan your code base and generate a report that gives an overview of how portable your code is and what assemblies are compatible with which version of .NET. The report in Excel looks like in Figure 4-38.
Figure 4-38. Portability Analyzer
I’ve used a demo application that was built in WinForms with .NET 4.0; in the analyzer settings, I’ve checked .NET 5 as target framework. The analyzer tells me that my application is 100% compatible with .NET 5; since .NET 6 is the successor of .NET 5, it should work just fine. Do keep in mind that all this analyzer does is checking if the framework APIs you use and and the references you have are compatible with the
selected target framework. It does not compile, run, or test your application in any way.
119
Chapter 4 Desktop Development
The next step in the porting process is moving away from the old packages.config file to PackageReference. NuGet references used to be stored in an xml file called packages.config in older versions of .NET. After a while those referenced moved to a section in the csproj file called PackageReference. Migration to PackageReference is straightforward in Visual Studio; right-click references and select Migrate packages. config to PackageReference.
Figure 4-39. Migrating to PackageReference
Visual Studio will uninstall all NuGet packages from the project; remove the packages.config and reinstall the NuGet packages in PackageReference. After migration it will show an HTML report detailing any compatibility issues. Don’t forget to build your application after this step to make sure all packages are restored from the NuGet package feed. Make sure that your NuGet packages have the correct version installed. You might need to update some to have .NET 6 support.
After upgrading to PackageReference, the next step is to update your application to the latest version of the classic .NET Framework, .NET 4.8. Upgrading to .NET 4.8 first will bring you closer to “the new .NET” that started with .NET Core. This is done easily from the properties of each project, as shown in Figure 4-40.
120
Chapter 4 Desktop Development
Figure 4-40. Updating to .NET 4.8
If you don’t see the .NET Framework 4.8 option, you need to download and install the .NET Framework 4.8 SDK from this link https://dotnet.microsoft.com/en-us/ download/visual-studio-sdks . Make sure your application still compiles and runs after this step.
With that, we’re finally ready for the big migration! There’s quite an easy trick to move an application to .NET 6.
•\ |
Create a new .NET 6 application of the same type (WPF, WinForms, |
|
Class Library, etc.) as the project you’re trying to migrate. |
•\ |
Copy the csproj of the new project to the location of the old project. |
•\ |
Rename the new csproj to have the same name as the csproj of the |
|
old application. |
•\ |
Open in Visual Studio, recreate the references, and add the NuGet |
|
packages again. |
121
Chapter 4 Desktop Development
And that’s it! Because of the new project structure since .NET Core, it’s as easy as replacing the csproj with a .NET 6 version. There is one more step after migrating all projects within your solution: test every single button, textbox, and flow in your
application. No matter how insignificant the code seems, or whether or not it compiles, test everything! For example, Process.Start("https://www.apress.com") might seem like a very trivial piece of code that couldn’t break, but it does. Since .NET Core, this no longer works and crashes the application.
Upgrade Assistant
There is an alternative to doing the migration manually. Microsoft has created a command line tool called Upgrade Assistant. It basically does the same steps as described above but automatically. The upgrade assistant is open source and can be found at https://github.com/dotnet/upgrade-assistant. Upgrading a project using the upgrade assistant is as easy as calling upgrade assistant upgrade .\Pixelator. csproj. The upgrade assistant is a wizard that guides you through the process step by step, allowing you to skip whatever step you want or see more details.
Figure 4-41. Upgrade assistant
Just like with the manual process, test every single nook and cranny of your application after migrating to .NET 6!
122