Running ASP.NET Core 2.0 Via Mono

In this article, you will learn how to run your ASP.NET Core 2.0 project via Mono . Most of time , we will use .NET Core CLI to run our ASP.NET Core web application , such as dotnet run and dotnet xxx.dll .

Introduction

In this article, you will learn how to run your ASP.NET Core 2.0 project via Mono.

Most of the time, we use .NET Core CLI to run our ASP.NET Core web application, such as dotnet run and dotnet xxx.dll .

But, Mono can run ASP.NET Core web application as well! I will show you how step by step.

Let's begin!

Step 1

Create a new ASP.NET Core Web application named MonoDemo.

ASP.NET Core

Step 2

ASP.NET Core

Change the default target from .NET Core to .NET Framework.

Note 

If you feel puzzled why we can choose .NET Framework here, you need to learn some more concepts, such as .NET Core, ASP.NET Core, and .NET Framework etc.

Step 3

In order to make this application able to run on OSX anLinux, we need to edit the MonoDemo.csproj.

ASP.NET Core

Here is the MonoDemo.csproj after editing.

  1. <Project Sdk="Microsoft.NET.Sdk.Web">  
  2.   
  3.   <PropertyGroup>  
  4.     <TargetFramework>net461</TargetFramework>  
  5.     <RuntimeIdentifiers>osx-x64;linux-x64</RuntimeIdentifiers>  
  6.   </PropertyGroup>  
  7.   
  8.   <ItemGroup>  
  9.     <PackageReference Include="Microsoft.AspNetCore" Version="2.0.0" />  
  10.     <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.0.0" />  
  11.     <PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.ViewCompilation" Version="2.0.0" PrivateAssets="All" />  
  12.     <PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.0.0" />  
  13.     <PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="2.0.0" />  
  14.   </ItemGroup>  
  15.   
  16.   <ItemGroup>  
  17.     <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.0" />  
  18.   </ItemGroup>  
  19. </Project>  
Step 4

Publish this web application using .NET Core CLI command. If your target is OSX, you can use the following command.

 

  1. dotnet publish -r osx-x64 -c Release -f net461  

 

If your target is Linux, you can use the following command.

  1. dotnet publish -r linux-x64 -c Release -f net461  

ASP.NET Core 

Here, I use OSX for an example. And, it's the same as Linux. The only difference between them is of libuv files. Each OS has its own type.

OSLibuv file after publishing
Windowslibuv.dll
Linuxlibuv.so
OSXlibuv.dylib

Step 5

Copy all the files in the Publish folder on your MAC or Linux computer. Before the following steps, you need to install Mono first. Please follow the below link to install if you have not installed yet .

http://www.mono-project.com/download/

Step 6

Let's run it via Mono.

  1. mono MonoDemo.exe  

ASP.NET Core

Unlucky, we find some errors from the terminal after we run the above command. We need to remove a DLL named System.Runtime.InteropServices.RuntimeInformation.dll from the Publish folder.

After removing, it runs well.

ASP.NET Core

Step 7

Open your browser and enter the listening URL http://localhost:5000. You will find that everything is good.

ASP.NET Core
We can use the lsof command to find out who is listening to the port 5000.

lsof -i :5000

ASP.NET Core

For comparison, I run a new ASP.NET Core web application via .NET Core CLI.

ASP.NET Core

At last, turn to Linux and run this application as well. Here, I use CentOS for example.

ASP.NET Core

As you can see, both OSX and Linux are the same and easy to run via Mono.

If you want to host your web application this way in Linux , follow the official document Set up a hosting environment for ASP.NET Core on Linux with Nginx, and deploy to it and edit the ExecStart in your service file.

The following is an example service file for our application:

  1. # Code removed for brevity.  
  2.   
  3. [Service]  
  4. WorkingDirectory=/var/www/monodemo  
  5. ExecStart=/usr/bin/mono /var/www/momodemo/MonoDemo.exe  
  6.  
  7. # Code removed for brevity.  
Summary

This article has introduced another way to run our ASP.NET Core web application via Mono. I hope this will help you.