Since I’ve been working with the community on porting desktop applications from .NET Framework to .NET Core, I’ve noticed that there are two camps of folks: some want a very simple and short list of instructions to get their apps ported to .NET Core while others prefer a more principled approach with more background information. Instead of writing up a “Swiss Army knife”-document, we are going to publish two blog posts, one for each camp:
If you prefer watching videos instead of reading, here is the video where I do everything that is described below.
To port your desktop apps to Core, you’ll need .NET Core 3 and Visual Studio 2019.
Before porting, you should check how compatible your application is with .NET Core. To do so, download and run .NET Portability Analyzer.
In Solution Explorer right-click on your project (not on the solution!). Do you see Edit Project File? If you do, you already use the SDK-style project file, so you should move to Step 3. If not, do the following.
packages.config
file. If you don’t, no action is needed, if you do, right-click on packages.config
and choose Migrate packages.config to PackageReference. Then click OK.<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net472</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
</PropertyGroup>
</Project>
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net472</TargetFramework>
<UseWPF>true</UseWPF>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
</PropertyGroup>
</Project>
PackageReference
. If you did not find anything, move on. If you found PackageReference
, copy the entire <ItemGroup>
that contains PackageReference
in your project file, opened in Visual Studio, right below the lines you’ve pasted in the step above. Do it for each occurrence of the PackageReference
you have found. The copied block should look like this.<ItemGroup>
<PackageReference Include="NUnit">
<Version>3.11.0</Version>
</PackageReference>
</ItemGroup>
ProjectReference
. If you did not find anything, move on. If you found any ProjectReference
items, they would look like this.<ItemGroup>
<ProjectReference Include="..\WindowsFormsApp1\WindowsFormsApp1.csproj">
<Project>{7bce0d50-17fe-4fda-b6b7-e7960aed8ac2}</Project>
<Name>WindowsFormsApp1</Name>
</ProjectReference>
</ItemGroup>
<Project>
and <Name>
properties, since they are not needed in the new project file style. So for each ProjectReference
that you have found (if any), copy only ItemGroup
and ProjectReference
like this.<ItemGroup>
<ProjectReference Include="..\WindowsFormsApp1\WindowsFormsApp1.csproj" />
</ItemGroup>
Great news, you just updated your project file to the new SDK-style! The project is still targeting .NET Framework, but now you’ll be able to retarget it to .NET Core.
Open your project file by double-clicking on your project in Solution Explorer. Find the property <TargetFramework>
and change the value to netcoreapp3.0
. Now your project file should look like this:
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>netcoreapp3.0</TargetFramework>
...
</PropertyGroup>
...
</Project>
Build and run your project. Congratulations, you ported to .NET Core 3!
If you get errors like
The type or namespace <some name> could not be found
or
The name <some name> does not exist in the current context
and your portability report was green, it should be easy to fix by adding a NuGet package with the corresponding library. If you cannot find the NuGet package with the library that is missing, try referencing Microsoft.Windows.Compatibility. This package adds ~21K .NET APIs from .NET Framework.
Even though it is possible to edit the user interface of your application via code, developers usually prefer using the visual designers. With .NET Core we had to rearchitect the way the designers work with .NET Core projects:
Here is how you can use the .NET Framework WinForms designer:
MyProject.csproj
), give it a different name, for example MyProject.NetFramework.csproj
and put it next to your existing project file.MyProject.NetFramework.csproj
.<TargetFramework>
to net472
.MyProject.NetFramework.csproj
project file and you’ll get the full experience of .NET Framework designer. When you are done with the designer, close and open your project with the .NET Core project file.Check out the video where Scott Hunter and I are talking about all the new things coming with .NET Core 3 Porting to .NET Core 3.0.
К сожалению, не доступен сервер mySQL