Running Self-contained ASP .NET core application on Ubuntu

dotnet publish --self-contained
create installer for .net core application
asp.net core self contained deployment
deploy asp.net core to ubuntu
run net core console app on linux
dotnet publish self contained macos
dotnet run
publish release dotnet core

First, I'm a Linux newbie so forgive me.

I've published an ASP .NET Core application as a self-contained app targeting Ubuntu. The publish seems to work fine. I've copied the files to a pretty vanilla Ubuntu machine. Now, how do i run my application? My understanding is that because it is a self-contained .NET Core application I DO NOT need to download and install .NET Core anything. My app should contain everything it needs.

All tutorials seem to say i should call $dotnet run. However the "dotnet" command line doesn't exist (is it supposed to be published into the self-contained folder??) So if i call it, i get "command not found". Of course i could download .NET Core, but doesn't that go against the whole self-contained concept? Here is a sample of the files i'm copying over.

Answer

Now, how do I run my application? My understanding is that because it is a self-contained .NET Core application I DO NOT need to download and install .NET Core anything. My app should contain everything it needs.

You are correct. Run the executable.

When you create a self-contained app, the publish output "contains the complete set of files (both your app files and all .NET Core files) needed to launch your app." That includes the executable.

Example Self-Contained Deployment

Here is the output of dotnet publish -c release -r ubuntu.14.04-x64 for a simple self-contained application. Copy the publish directory to Ubuntu and run the executable.

C:\MyApp\bin\release\netcoreapp1.0\ubuntu.14.04-x64\publish\

...

libsos.so
libsosplugin.so
libuv.so
Microsoft.CodeAnalysis.CSharp.dll
Microsoft.CodeAnalysis.dll
Microsoft.CodeAnalysis.VisualBasic.dll
Microsoft.CSharp.dll
Microsoft.VisualBasic.dll
Microsoft.Win32.Primitives.dll
Microsoft.Win32.Registry.dll
mscorlib.dll
mscorlib.ni.dll
MyApp                        <------- On Ubuntu, run this executable
MyApp.deps.json                       and you will see Hello World!
MyApp.dll
MyApp.pdb
MyApp.runtimeconfig.json
sosdocsunix.txt
System.AppContext.dll
System.Buffers.dll
System.Collections.Concurrent.dll
System.Collections.dll

...

C:\MyApp\project.json

{
  "buildOptions": {
    "debugType": "portable",
    "emitEntryPoint": true
  },
  "dependencies": {},
  "frameworks": {
    "netcoreapp1.0": {
      "dependencies": {
        "Microsoft.NETCore.App": "1.0.1"
      }
    }
  },
  "runtimes": {
    "ubuntu.14.04-x64" : {},
    "win10-x64" : {}
  }
}

C:\MyApp\Program.cs

public class Program
{
    public static void Main(string[] args)
    {
        System.Console.WriteLine("Hello World!");
    }
}
See Also

This document differentiates between framework-dependent and self-contained deployments.

Application publishing, https://docs.microsoft.com › › .NET › ASP.NET Core › Host and deploy Running a self-contained ASP .NET Core application on Ubuntu (4 answers) Closed 2 years ago . I am using Visual Studio 2015 and created a .NET Core console application.

Host ASP.NET Core on Linux with Nginx, NET Core web app running on Kestrel. The app can also be published as a self-contained deployment if you prefer not to maintain the .NET  The app can also be published as a self-contained deployment if you prefer not to maintain the.NET Core runtime on the server. Copy the ASP.NET Core app to the server using a tool that integrates into the organization's workflow (for example, SCP, SFTP). It's common to locate web apps under the var directory (for example, var/www/helloapp).

You may want to check out dotnet-packaging as well. It includes a dotnet deb command line utility which allows you to create a .deb file (i.e. a Ubuntu installer) which you can use to install your app on Ubuntu. It should make deployment easier for you.

To get started, you'll first need to add this section to your .csproj file:

<ItemGroup>
  <PackageReference Include="Packaging.Targets" Version="0.1.45" />
  <DotNetCliToolReference Include="dotnet-deb" Version="0.1.45" />
<ItemGroup>

Then, run dotnet restore and dotnet deb -c Release -r ubuntu.18.04-x64 -f netcoreapp2.0. This will create a .deb file you can use to deploy your application to Ubuntu.

Self-contained .NET Core Applications, NET Core and keeping it up to date. I also need to decide on my target platforms ahead of time. If I want it to run on Windows, Mac, and Linux  After installing .NET Core runtime and transferred the necessary files, I could see the web application running with the help of PM2. However, this process was rather tedious and manual, as I could see that in the subsequent deployments down the road, I would need to make sure I clear the directory before deploying new files to a avoid conflicts.

Publish to a Linux Production Environment, Copy over your app¶. Run dotnet publish from your dev environment to package your application into a self-contained directory that can run on your  ASP.NET Core is a new open-source and cross-platform framework for building modern cloud based internet connected applications, such as web apps, IoT apps and mobile backends. ASP.NET Core apps can run on .NET Core or on the full .NET Framework. – Introduction to ASP.NET Core

Deploying .NET Core 3 apps as self-contained, Self-contained (SCD) : you are packaging the required libraries and runtimes needed for your code to run, not expecting any shared runtime on  Introduction. There once was a time where C# application could only run on the Windows operating system. This is no longer that time. Since the release of .NET Core, the power of these applications (and their treasure trove of libraries & frameworks) can be put to use on Linux systems.

How to distribute a .NET Core application to users who don't have , Downsides of self-contained deployments are that an own package is needed for each Operating System platform (win, osx, linux, …) and for  Setting up .NET Core. I already explained how to install .NET Core in article How to setup .NET Core 2 on Debian or Ubuntu Linux distro the easy way but I will make some small adjustments to the installation to make it more usable for running services, so I will shortly explain here how to setup .NET Core support.

Comments
  • I have no Idea as i'm not a dotnet dev, but I think you might need to install the dotnet cli on Ubuntu first. Is this something you need? github.com/dotnet/cli
  • I would agree with your assessment except it seems like one of the main points of publishing the application in a "self-contained" fashion is not to need to download anything else. The dotnet cli MIGHT be an exception to that but I don't think so.
  • I use a lot of "self contained" docker containers, without the docker service they don't run ;)
  • I suppose up there the "MyApp" is actually "MyApp.exe" with file extension .exe?
  • @JP_medevice The *.exe extension is a Windows convention that does not apply to Linux. See also askubuntu.com/questions/156392/…
  • For ubuntu ignorants life myself: To run the executable you type .\ExecutableName on the terminal. @ShaunLuttin maybe you could include this in your answer.