Porting Desktop Applications to .NET Core

Original author: Olia Gavrysh
  • Transfer
Since I started working with the community on porting desktop applications from the .NET Framework to the .NET Core, I noticed that there are “two camps”: representatives of one want a very simple and short list of instructions for porting their applications to .NET Core , while representatives of another prefer a more principled approach with a lot of initial information. Instead of writing a document like the “Swiss Army Knife”, we are going to publish two blog posts, one for each “camp”:

  • This post is a simple case . It focuses on simple instructions and small applications and considers the easiest way to port an application to .NET Core.
  • Later we will publish another post for more complex cases . It will focus on non-trivial applications, such as a WPF application with dependencies on WCF and third-party UI packages.

If you prefer to watch a video rather than read, then here is a video where I do everything described below.


Step 0 - Prerequisites


To transfer desktop applications to Core, you will need .NET Core 3 and Visual Studio 2019.

Step 1 - Launch Portability Analyzer


Before porting, you should check how compatible your application is with .NET Core. To do this, download and run the .NET Portability Analyzer .

  • On the first tab, Portability Summary, if all the values ​​in the .NET Core column are 100% (everything is highlighted in green), your code is fully compatible, and you can go to Step 2.
  • If you have values ​​less than 100%, first look at all the assemblies that are not part of your application. For them, you need to check whether their authors provide versions for .NET Core or .NET Standard.
  • Now look at the other part of the assemblies that comes from your code. If no assemblies are specified in the Portability Report, go to Step 2. Once you do this, open the Details tab, filter the table by clicking on the Assembly column and focus only on those that are related to your application. Browse through the list and code refactoring to stop using the API or replace using the API with .NET Core alternatives.

image

Step 2 - Migrating .csproj to SDK Style


In Solution Explorer, right-click your project (not a solution!). Do you see Edit Project File ? If so, then you are already using the SDK-style project file, so you should go to Step 3 . If not, do the following:

  • Check in Solution Explorer if the project contains packages.config file . If it is not, then no action is required, but if it is, right-click packages.config and select Migrate packages.config to PackageReference . Then click OK .
  • Open the project file by right-clicking on the project and selecting Unload Project . Then right-click the project and select Edit <your project name> .csproj .
  • Copy the contents of the project file somewhere, for example, to Notepad, so that you can later search it.
  • Delete everything from the project file opened in Visual Studio (I know this sounds aggressive, but we will only add the necessary content from the copy we just made in a few steps). Instead of just deleted text, paste the following code.

    For an application on WinForms:

    WinExenet472truefalse

    For a WPF application:

    WinExenet472truefalse
  • Find Notepad PackageReference . If you haven't found anything, move on. If you find a PackageReference , copy the entire ItemGroup containing the PackageReference into the project file opened in Visual Studio, directly below the lines inserted in the step above. Do this for each PackageReference element that you found. The copied block should look like this:

    3.11.0
  • Now do the same as above for ProjectReference . If you haven't found anything, move on. If you find any ProjectReference elements , they will look like this:

    {7bce0d50-17fe-4fda-b6b7-e7960aed8ac2}WindowsFormsApp1
  • You can delete lines with the Project and Name properties , since they are not needed in the new style of the project file. So for each ProjectReference that you found (if any), copy only the ItemGroup and ProjectReference .

  • Save it all. Close the .csproj file in Visual Studio. Right-click your project in Solution Explorer and select Reload Project . Rebuild and make sure there are no errors.

Great news, you just updated your project file to a new SDK style! The project is still targeted at the .NET Framework, but now you can reconfigure it to .NET Core.

Step 3 - Retarget on .NET Core


Open the project file by double-clicking it in Solution Explorer . Find the TargetFramework property and change the value to netcoreapp3.0 . Your project file should now look like this:

WinExenetcoreapp3.0
    ...
  
  ...

Build and run your project. Congratulations, you migrated it to .NET Core 3!

Error correction


If you encounter errors like

The type or namespace  could not be found

or

The name  does not exist in the current context

and if your Portability Report is displayed in green, you should know that they are easy to fix by simply adding the NuGet package with the appropriate library. If you are unable to find the NuGet package with the missing library, then try contacting Microsoft.Windows.Compatibility . This package adds ~ 21K .NET API from the .NET Framework.

Work with designers


Although the user interface of the application can be edited using code, developers usually prefer to use visual constructors. With .NET Core, we had to change the architecture of the work of designers with .NET Core projects:

  • WPF Designer is already in preview mode, and we are working on adding additional features to it.
  • WinForms Designer for .NET Core will be available later, so for now, you can use WinForms Designer for .NET Framework as a workaround.

Here's how to use the WinForms constructor for the .NET Framework:

  1. Copy the .csproj file (for example, MyProject.csproj ), give it a different name, for example, MyProject.NetFramework.csproj, and place it next to the existing project file.
  2. Make sure your project is closed in Visual Studio, open a new project MyProject.NetFramework.csproj .
    In Solution Explorer, right-click your project and select Properties . On the Application tab (should open by default), set the Assembly name and Default namespace to the same values ​​as in the original project (remove “.NetFramework” from the names).
    Save this solution next to your existing solution.
  3. Open a new project file and change TargetFramework to net472 .
  4. Now that you need to use the WinForms constructor, load your project from MyProject.NetFramework.csproj and you can start working with the .NET Framework constructor. When you are done with it, close and open your project with the .NET Core project file.
  5. This is just a workaround until the WinForms constructor for .NET Core is ready.

Why switch to .NET Core?


Watch a video where Scott Hunter and I talk about all the latest updates on .NET Core 3. Migrating to .NET Core 3.0 .

Also popular now: