Page 40 - MSDN Magazine, May 2019
P. 40
and the like. With a WinForms application in .NET Core, you don’t have to worry about any of this. Let’s now take a look at one of the biggest advantages of building a WinForms app using .NET Core.
Packaging the Application
In the past, deploying a new or updated WinForms application could cause problems related to the version of the .NET Framework installed on the host machines. With .NET Core, apps can be deployed self-contained and run from a single folder, with no dependencies to the version of .NET Framework installed on the machine. This means the user doesn’t need to install anything; they can simply run the application. It also enables you to update and deploy apps one at a time, because the packaged versions of .NET Core will not affect each other.
For the sample app in this article, you’ll want to package it up as self-contained. Keep in mind that self-contained applications will be larger because they include the .NET Core libraries with them. If you’re deploying to machines with the latest versions of .NET Core installed, you don’t need to make the app self-contained. Instead, you can reduce the size of the deployed app by leveraging the installed version of .NET Core. Self-contained options are for when you don’t want your applica- tion to be dependent on the environment in which it will be running.
To package the application locally, you need to make sure Devel- oper Mode is enabled in your settings. Visual Studio will prompt you and give you a link to the settings when you try to run the pack- aging project, but to enable it directly go to your Windows settings, press the Windows key and search for Settings. In the search box type “For developers settings” and select it. You’ll see an option to enable Developer Mode. Select and enable this option.
For the most part, the steps to create a self-contained package will seem familiar if you’ve previously packaged a WinForms applica- tion. First, start by creating a new Windows Application Packaging Project. Name the new project PullRequestHubPackaging. When prompted to select the target and minimum platform versions, use the defaults and click OK. Right-click on Applications and add a reference to the PullRequestHub project.
After the reference has been added, you need to set the PullRequestHub project as the Entry Point. Once that’s done, the next time you build you’ll very likely see the following error: “Project PullRequestHub must specify ‘RuntimeIdentifiers’ in the project file when ‘SelfContained’ is true.”
To fix this error, edit the PullRequestHub.csproj file. When you open this project file, you’ll notice yet another advantage of using .NET Core, because the project file is now using the new, lightweight format. In .NET Framework-based WinForms projects, the project file is much more verbose with explicit defaults and references, as well as NuGet references split out into a packages.config file. The new project file format brings package references into the project file, making it possible to manage all your dependencies in one place.
In this file, in the first PropertyGroup node, add the following line:
<RuntimeIdentifiers>win-x86</RuntimeIdentifiers>
A Runtime Identifier is used to identify target platforms where the application runs, and is used by .NET packages to represent platform-specific assets in NuGet packages. Once this is added, the build should succeed, and you can set the PullRequestHubPackaging project as the startup project in Visual Studio.
One thing to note in the PullRequestHubPackaging.wapproj file is the setting to indicate that the project is self-contained. The section of code in the file to pay attention to is the following:
<ItemGroup>
<ProjectReference Include=”..\PullRequestHub\PullRequestHub.csproj”>
<DesktopBridgeSelfContained>True</DesktopBridgeSelfContained> <DesktopBridgeIdentifier>$(DesktopBridgeRuntimeIdentifier) </DesktopBridgeIdentifier>
<Properties>SelfContained=%(DesktopBridgeSelfContained); RuntimeIdentifier=%(DesktopBridgeIdentifier)
</Properties> <SkipGetTargetFrameworkProperties>True </SkipGetTargetFrameworkProperties>
</ProjectReference> </ItemGroup>
Here you can see that the DesktopBridgeSelfContained option is set to true, which enables the WinForms application to be packaged with the .NET Core binaries. When you run the project, it dumps the files out to a folder named “win-x86” found in a path similar to this:
C:\Your-Path\PullRequestHub\PullRequestHub\bin\x86\Debug\netcoreapp3.0
Inside of the win-x86 folder you’ll notice many DLLs, which include everything that the self-contained app needs to run.
More likely, you’ll want to deploy the app as a side-loaded application or upload it to the Microsoft Store. Side-loading will make automatic updates possible using an appinstaller file. These updates are supported starting with Visual Studio 2017, Update 15.7. You can also create packages that support submission to the Microsoft Store for distribution. The Microsoft Store then handles all the code signing, distribution and updating of the app.
In addition to these options, there’s ongoing work to make it pos- sible for applications to be packaged up into a single executable, eliminating the need to populate an output directory with DLLs.
Additional Advantages
With .NET Core 3.0, you’re also able to leverage features of C# 8.0, including nullable reference types, default implementations on inter- faces, improvements to switch statements using patterns, and asyn- chronous streams. To enable C# 8.0, open the PullRequestHub.csproj file and add the following line to the first PropertyGroup:
<LangVersion>8.0</LangVersion>
Another advantage to using .NET Core and WinForms is that both projects are open source. This gives you access to the source code, and lets you file bugs, share feedback and become a contrib- utor. Check out the WinForms project at github.com/dotnet/winforms.
.NET Core 3.0 promises to breathe new life into the investments that enterprises and businesses have made into WinForms applica- tions, which continue to be productive, reliable, and easy to deploy and maintain. Developers can leverage new .NET Core-specific classes like HttpClientFactory, employ C# 8.0 features like nullable reference types, and package up self-contained applications. They also gain access to the .NET Core CLI and all the performance improvements that come with .NET Core. n
Eric FlEming is a senior software engineer with more than a decade of experience working with Microsoft tools and technologies. He blogs at ericflemingblog.com and co-hosts the Function Junction YouTube channel that explores Azure Functions. Follow him on Twitter: @efleming18.
Thanks to the following technical experts who reviewed this article: Olia Gavrysh (Microsoft), Simon Timms
34 msdn magazine
WinForms