How to port WinForm to. Net core 3.0

Time:2020-9-16

Refer to porting existing applications to. Net core 3.0

Environmental Science

First, download. Net core 3.0 and visual studio 2019

Edit csproj file


<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
 <PropertyGroup>
  <OutputType>WinExe</OutputType>
  <TargetFramework>netcoreapp3.0</TargetFramework>
  <UseWindowsForms>true</UseWindowsForms>
  <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
 </PropertyGroup>
</Project>

Recover nuget package

find package.config Right click the file and select upgrade to package reference, but I will pop up the box

I don’t know how to solve this problem. After searching for a circle, I didn’t find it. If you have any information, you can tell me in the comment area, so I will be honest


<?xml version="1.0" encoding="utf-8"?>
<packages>
 <package version="1.50.2" targetFramework="net45" />
 <package version="6.0.0" targetFramework="net45" />
 <package version="6.7.9" targetFramework="net45" />
 <package version="1.0.108.0" targetFramework="net45" />
 <package version="1.0.108.0" targetFramework="net45" />
 <package version="1.0.108.0" targetFramework="net45" />
 <package version="1.0.108.0" targetFramework="net45" />
</packages>

Change it to the following code and write it to the csproj file in the child node of the project


<ItemGroup>
  <PackageReference Include="Dapper" Version="1.50.2" />
  <PackageReference Include="EntityFramework" Version="6.0.0" />
  <PackageReference Include="MySql.Data" Version="6.7.9" />
  <PackageReference Include="System.Data.SQLite" Version="1.0.108.0" />
  <PackageReference Include="System.Data.SQLite.Core" Version="1.0.108.0" />
  <PackageReference Include="System.Data.SQLite.EF6" Version="1.0.108.0" />
  <PackageReference Include="System.Data.SQLite.Linq" Version="1.0.108.0" />
</ItemGroup>

Add references

At the same time, some DLLs are unique to the. Net framework, while. Net core is not referenced for various reasons. Refer to porting to. Net core. For example, the following items need to be re quoted:

  • System.Data 。 Although the foundation layer is already part of the. Net core, that is, the provider model and SQL client, there are no features such as architecture support and datatable / dataset.
  • System.DirectoryServices 。 . net core does not currently support communication with LDAP or active directory.
  • System.Drawing 。 Although it is strictly a client-side API, many developers use the drawing API on the server to provide thumbnail generation or watermarking. We currently do not support these APIs in. Net core.
  • System.Transactions 。 although ADO.NET Transactions are supported, but distributed transactions are not supported, including the concepts of environmental transactions and enlistment.
  • System.Xml.Xsl and System.Xml.Schema 。 . net core supports xmldocumentlinq xdocument, including XPath. However, XSD (XMLSchema) or XSLT (xsltransform) are not currently supported.
  • System.Net.Mail 。 Sending email from. Net core using these APIs is not currently supported.
  • System.IO.Ports 。 The. Net core currently does not include the ability to communicate with serial ports.
  • System.Workflow 。 Windows Workflow Foundation (WF) is currently not available on. Net core.
  • System.Xaml 。 When creating a uwp application, developers will use the winrt XAML API. As a result, the. Net core currently does not contain a managed XAML framework, which includes the ability to parse XAML documents and instantiate described object graphs.

In my project, I cited these:


<ItemGroup>
  <Reference Include="System.Data"/>
  <Reference Include="System.Xaml"/>
  <Reference Include="System.Transactions"/>
  <Reference Include="System.Drawing"/>
  <Reference Include="System.Data.DataSetExtensions" />
  <Reference Include="Microsoft.CSharp" />
  <Reference Include="System.Net.Http" />
 </ItemGroup>

Add other documents

The following documents are added to the project itself


<ItemGroup>
  <None Include="App_Data\data.db" />
  <None Include="packages.config" />
  <None Include="Properties\Settings.settings">
   <Generator>SettingsSingleFileGenerator</Generator>
   <LastGenOutput>Settings.Designer.cs</LastGenOutput>
  </None>
 </ItemGroup>
 <ItemGroup>
  <None Include="App.config">
   <SubType>Designer</SubType>
  </None>
 </ItemGroup>

Final csproj file


<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">

 <PropertyGroup>
  <OutputType>WinExe</OutputType>
  <TargetFramework>netcoreapp3.0</TargetFramework>
  <UseWindowsForms>true</UseWindowsForms>
  <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
 </PropertyGroup>
 <ItemGroup>
  <PackageReference Include="Dapper" Version="1.50.2" />
  <PackageReference Include="EntityFramework" Version="6.0.0" />
  <PackageReference Include="MySql.Data" Version="6.7.9" />
  <PackageReference Include="System.Data.SQLite" Version="1.0.108.0" />
  <PackageReference Include="System.Data.SQLite.Core" Version="1.0.108.0" />
  <PackageReference Include="System.Data.SQLite.EF6" Version="1.0.108.0" />
  <PackageReference Include="System.Data.SQLite.Linq" Version="1.0.108.0" />
 </ItemGroup>
 <ItemGroup>
  <Reference Include="System.Data"/>
  <Reference Include="System.Xaml"/>
  <Reference Include="System.Transactions"/>
  <Reference Include="System.Drawing"/>
  <Reference Include="System.Data.DataSetExtensions" />
  <Reference Include="Microsoft.CSharp" />
  <Reference Include="System.Net.Http" />
 </ItemGroup>
 <ItemGroup>
  <None Include="App_Data\data.db" />
  <None Include="packages.config" />
  <None Include="Properties\Settings.settings">
   <Generator>SettingsSingleFileGenerator</Generator>
   <LastGenOutput>Settings.Designer.cs</LastGenOutput>
  </None>
 </ItemGroup>
 <ItemGroup>
  <None Include="App.config">
   <SubType>Designer</SubType>
  </None>
 </ItemGroup>
</Project>

Problems encountered in the process of migration

. net core does not support access databases

My code contains code related to access database, and they all report the error of missing assembly reference, so I initially added the reference


<ItemGroup>
  <Reference Include="System.Data"/>
  <Reference Include="System.Data.OleDb"/>
 </ItemGroup>

However, it is still useless. After referring to. Net core data access, it is found that. Net core does not support access. Therefore, this code is packaged with conditional compilation code ා if.

lack hostfxr.dll file

After the compilation is successful, run the project and find that the window pops up. I checked the existence of the file in the file C: hostfxr.dll After reading the environment variable, dotnet is set manually_ Root variable, but always error, no way, finally through the copy hostfxr.dll The file can not run successfully until bin / debug / netcoreapp3.0 \

This article on how to port WinForm to. Net core 3.0 is introduced here. For more content about porting WinForm to. Net core 3.0, please search the previous articles of developeppaer or continue to browse the related articles below. I hope you can support developeppaer more in the future!