Having got the newly released “Visual Studio 2017” installed in my PC, I decided to try writing cross-platform .NET Core Apps. I started with the simplest of apps, to write a console app and see how it goes. To follow along you can download Visual Studio Community edition, which is free or the commercial versions. You can refer to this Microsoft doc titled “Building a C# Hello World Application with .NET Core with Visual Studio 2017” for additional information on what I am doing here.
static void Main(string args)
Console.WriteLine("Cross Platform App made using .NET Core");
The next step is IMPORTANT. You need to access the .csproj file by right-clicking on the project in solution explorer and select “Edit MyConsoleApp.csproj”. CSProj is a new file type in released .NET Core and VS 2017, in demos made for Visual Studio 2015 or beta releases of .NET Core you would’ve put these settings in Project.JSON file.
There are two types .NET Core Applications Deployment – Framework-dependent deployments (FDD) and Self-contained deployments (SCD). In this example, I am going with SCD so that I can deploy and run the app on machines which don’t have .NET Core installed. In the output package, we will create, the app will include the necessary .NET Core Runtime needed to run on the target platform, whether it is Windows or Mac OS X or Ubuntu Linux.
Create a tag under the section in your csproj file that defines the platforms your app targets, and specify the runtime identifier of each platform that you target. For a full list of .NET Core Runtime IDentifier (RID) is here.
You can try “running” the app once to ensure everything is working fine. Then Right-Click on the Project to select Publish.
You can try running the Windows 10 binaries from the folder (bin\Release\PublishOutput\Win10) we selected for that platform.
Copy the Mac OS binaries to a Mac OS and execute the application (MyConsoleApp) file from the folder. Note, the Mac machine doesn’t need to have .NET core installed as this package of about 53MB in size contains a self-contained version of .NET Core bundled. From terminal, you need to give Execute rights to the application. Repeat the same steps for Ubuntu OS, including the chmod command.
sudo chmod +x ./MyConsoleApp
That’s all. As you have seen developing a multi-platform console app with .NET Core & Visual Studio 2017 is super easy.