net-core-self-contained.NET Core framework provides one very useful feature - Self-contained application. You don't need to install any .net runtime on your computer, you can just copy your application to the target host and run it. Furthermore, you can run it on any platform (Windows, Linux, OSX)!

When you create a self-contained application, after publishing, you will see the whole .net runtime next to your application.

There are some advantages:

  • You do not need to have installed .NET Core on a target machine
  • You can run applications with different .NET Core versions at the same time

But also, disadvantages:

  • Bigger application size (for empty web API project around 50 MB)
  • Limited target runtimes list (see list here)

Let's create simple web API application and deploy it to the clean Ubuntu 14.04 machine!

Developer prerequisites

  • Installed .NET Core
  • Editor (Visual Studio Code, Visual Studio 2015, Rider, notepad, etc...)
  • sftp client (winscp)
  • ssh client (putty)

Application

Create empty application

dotnet new

Edit project.json:

{
  "version": "1.0.0-*",
  "buildOptions": {
    "emitEntryPoint": true
  },
  
  "dependencies": {
    "Microsoft.NETCore.App": "1.0.0",
    "Microsoft.AspNetCore.Mvc": "1.0.0",
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
    "Microsoft.Extensions.Logging": "1.0.0",
    "Microsoft.Extensions.Logging.Console": "1.0.0",
    "Microsoft.Extensions.Logging.Debug": "1.0.0"
  },
  
  "frameworks": {
    "netcoreapp1.0": {
      "imports": "dnxcore50"
    }
  },
  
  "runtimeOptions": {
    "configProperties": {
      "System.GC.Server": true
    }
  },
  
  "runtimes": {
      "win10-x64": {},
      "ubuntu.14.04-x64": {}
  }
}

An important part is runtimes section. You can find supported runtimes here: RID catalog

Also, you should not include  "type": "platform" option in the following section

"Microsoft.NETCore.App": "1.0.0",

Now, edit Program.cs

using Microsoft.AspNetCore.Hosting;

namespace WebServiceApplication
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var host = new WebHostBuilder()
                .UseKestrel()
                .UseUrls("http://+:5000")
                .UseStartup<Startup>()
                .Build();

            host.Run();
        }
    }
}

In this file, we have configured our hosting environment, which is Kestrel (cross-platform web server) and specified which IP and port it will use.

Create Startup.cs

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace WebServiceApplication
{
    public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            loggerFactory.AddConsole();
            loggerFactory.AddDebug();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller}/{action}/{id?}");
            });
        }
    }
}

Here we enabled MVC and Logging in our application.

Now the logic, LuckController.cs

using Microsoft.AspNetCore.Mvc;

namespace WebServiceApplication
{
    public class LuckController : Controller
    {
        public IActionResult Try()
	{
		return Ok(42);
	}
    }
}

Now let's restore all required packages. In command line:

dotnet restore

And run application

dotnet run

If everything is ok you can go to http://localhost:5000/Luck/Try  and you will see 42.

Publish and run application on a remote machine

To create self-contained application run following command:

dotnet publish --configuration Release --runtime ubuntu.14.04-x64

where --runtime value is one of the runtimes elements from project.json

You will see folder where you can find self-contained application

Archive and copy folder to the target machine.

Extract archive, navigate to the extracted folder and make file executable:

chmod +x test

Run application:

./test

Your first self-contained ASP.NET Core application is ready!

Here is more information about different .NET Core application types