My starting point for this was this Microsoft blog. (After installing .NET Core 3.0 and Visual Studio 2019.)
Portability Analyzer
The portability analyzer is a tool that reports how ready your Windows Forms or WPF application is for converting to .NET Core.
As it turns out there are two ways of getting this to run. First it can be built from this GitHub repository: https://github.com/microsoft/dotnet-apiport
There were some points to be aware of when building this tool:
- It needed to be build with Visual Studio 2017
- Visual Studio Extensions support needed to be installed via the Extension manager to build a couple of the projects
- The init.ps1 powershell script needed to be run first
The last step required Powershell to be run as admin. Also, as the script was unsigned, this command needed to be run first
set-ExecutionPolicy RemoteSigned
The build created a .NET and .NET Core binary. Running it by default generated an Excel sheet which output of this was pretty clean, everything was supported by at least
".NET Core + Platform Extensions" |
(The second way to install was using the Extension Manger in Visual Studio 2019 and load from the store. When this has been done, you can right click on the solution in Solution Explorer and select "Analyze assembly portability")
The analyzer did report problems with the unit test project. These types are no longer supported:
- Microsoft.VisualStudio.TestTools.UnitTesting.Assert
- Microsoft.VisualStudio.TestTools.UnitTesting.TestClassAttribute
- Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute
Change the project to SDK-Style
First step is to change from the old style .csproj format to the new SDK-style.Following the steps in the blog, the new .csproj file looked like this. Much cleaner!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<project sdk="Microsoft.NET.Sdk.WindowsDesktop"> | |
<propertygroup> | |
<outputtype>WinExe</outputtype> | |
<targetframework>net461</targetframework> | |
<usewpf>true</usewpf> | |
<generateassemblyinfo>false</generateassemblyinfo> | |
</propertygroup> | |
</project> |
There was also a problem with System.Windows.Forms. These needed to be added as references
After a successful build it was now time to change to .NET Core 3! This is really simple, just change this line:
<TargetFramework>netcoreapp3.0</TargetFramework>
The project was successfully updated in Visual Studio and a new attempt made to build. This failed with a problem with System.Drawing.Print types. To be fair, this was in the analyzer report.
The fix was to add a reference to System.Drawing.Common v4.6 using NuGet.
There was one outstanding problem with print preview:
var printPreviewDialog1 = new System.Windows.Forms.PrintPreviewDialog();
The pragmatic approach was to remove this code. Supporting print preview will be a future task.
This cleared the build issues but unfortunately when starting the app I got a runtime error caused by missing images/settings.ico.
The reason for this is that icons weren't automatically added as references in the new project. manually doing this fixed the problem.
And the test project I excluded earlier? The easy way for this was to create a new unit test project and copy the tests over, referencing NUnit.Framework this time.
Azure build pipeline
I've setup an Azure build pipeline for this project but unfortunately the first build failed with this error:
##[error]ClientApp\SuperMarketPlanner.csproj(0,0):
Error : C:\Program Files\dotnet\sdk\2.2.109\Sdks\Microsoft.NET.Sdk.WindowsDesktop\Sdk not found.
Check that a recent enough .NET Core SDK is installed and/or increase the version specified in global.json.
I updated the yml file to load the 3.0.100 Sdk, But then got a failure with the NuGetCommand:
Errors in D:\a\1\s\SuperMarketPannerUnitTests\SuperMarketPlannerUnitTests.csproj
NU1102: Unable to find package Microsoft.NETCore.App with version (>= 3.0.0)
- Found 76 version(s) in NuGetOrg [ Nearest version: 3.0.0-preview8-28405-07 ]
For now, I've got round this by explicitly defining the latest official version for the package reference in the unit test project
<PackageReference Include="Microsoft.NETCore.App" Version="2.2.7" />
Finally, I also changed the build and test tasks in the yaml to use the DotNetCoreCLI tasks. The pipline build is now green! The updated yaml is here:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trigger: | |
- master | |
pool: | |
vmImage: 'VS2017-Win2016' | |
variables: | |
solution: '**/*.sln' | |
buildPlatform: 'Any CPU' | |
buildConfiguration: 'Release' | |
steps: | |
- task: UseDotNet@2 | |
displayName: 'Install .NET Core SDK' | |
inputs: | |
version: 3.0.x | |
performMultiLevelLookup: true | |
- task: NuGetToolInstaller@0 | |
- task: NuGetCommand@2 | |
inputs: | |
restoreSolution: '$(solution)' | |
- task: DotNetCoreCLI@2 | |
inputs: | |
command: 'build' | |
- task: DotNetCoreCLI@2 | |
inputs: | |
command: test | |
projects: '**/*Tests/*.csproj' | |
arguments: '--configuration $(buildConfiguration)' |
No comments:
Post a Comment