Build cross-platform with .Net Core

Saturday, 9 June 2018 by James Ellis-Jones

One of the key benefits of .Net Core is its ability to work on both Windows and Linux/Mac. This article aims to help you do that most effectively and take advantage of the flexibility this provides.

Developing for Cross Platform Use

When you start developing .Net Core apps and code that needs to run cross platform, you have to deal with the more complex set of standards in the .Net world that have arisen since Core came on the scene. Essentially:

Another complexity that is a feature of .Net Core 2.0 and above is that it is possible for a .Net Core application to target (some) .Net Framework binaries and Nuget packages. But if you do this, those binaries/packages won’t run on Linux/Mac, so don’t do it if you want your application to be cross platform.

Self-Contained or Framework Dependent

You can build .Net Core apps in two ways. If you start with a Visual Studio template for .Net Core by default you will be building a framework dependent app. Such an application when published, can run on any platform, so long as the machine has the .Net Core runtime installed on it.

For self-contained apps, you specify one or more platforms in the .csproj file and the publish output contains all libraries and the dotnet runtime required. The self-contained app therefore has no dependencies and can be run on the specified platform only without any other installations.

The platforms are specified by adding a line in the .csproj file as below:

<PropertyGroup>
    <RuntimeIdentifiers>win10-x64;osx.10.11-x64</RuntimeIdentifiers>
</PropertyGroup>

The current list of available platforms can be found here: https://github.com/dotnet/corefx/blob/master/pkg/Microsoft.NETCore.Platforms/runtime.json.

You can specify multiple platforms, but then for any given publish action you have to choose which one you’re going to build, either via the -r switch on the dotnet publish command, or in Visual Studio by choosing the Target Runtime in the Settings dialog for the publish profile you’re using.

To learn more about these two options, go here: https://docs.microsoft.com/en-us/dotnet/core/deploying/

Cross-Platform Coding Practices

Generally the beauty of .Net Core is that if you build using it, your code will simply work on all the supported platforms. There are a couple of gotchas to beware of however where you can inadvertently make your code platform specific if you don’t follow the right rules.

You might also like to look at this series of blog posts which has some more specific issues: https://www.stevejgordon.co.uk/asp-net-core-gotchas-number-1

What Cross-Platform Enables

Obviously building a cross platform .Net Core application opens up the possibilities of interacting with the whole world of Linux, and there are many use cases you can imagine, the most obvious of all being to take advantage of cheaper Linux-based server infrastructure. Here are some of the commoner ones.

Run the Back End on Front End Developers' Macs

If you are a back end .Net developer used to working on Windows, these days you will find the vast majority of front end devs will be working on their Macs. With .Net Core it’s now easy for them to install your back end locally on their machines. I’ve found the following setup works very well in this context where the front end is on a JS framework and talks to the back end via an API

Use Docker better

While it’s possible to run Windows applications on Linux/Mac under Docker, it also requires installation of a VM such as VirtualBox. .Net Core applications can run directly on the Linux/Mac underlying OS so this isn’t needed for them, making .Net Core an ideal platform for creating containerised applications.

Use for Serverless

.Net Core is now supported on AWS Lambda, Azure Functions, and Azure Service Fabric. You can build ASP.Net Core applications too, if you’re careful to optimise them for rapid startup.