Microservice on ASP.NET Core RC2 on Ubuntu

ASP.NET Core RC2

Overview

Have you heard that ASP.NET Core RC2 has been just released?. Is the revolution is coming? In my latest posts I have built simple microservice on Ubuntu using ASP.NET Core RC1. As we have something new, let’s try to migrate to ASP.NET Core RC2 on Ubuntu.

Code

You can find the code on my GitHub. All of these should be on master when we are done. Until this you can find them here: ASP.NET Core RC2.

Moving to ASP.NET Core RC2 on Ubuntu

Comments

Despite all love to Linux current instruction says that they have issues with apt-get.
When I was writing this post, documentation has changed. Package should be available on apt-get!

asp.net core rc2 on ubuntu

Step 1 – Uninstall old

Following the instruction I need to run the script that they have provided. However running this script fails:

 sudo ./dotnet-uninstall-debian-packages.sh Unable to find dotnet installation to remove. 

If I check what is installed on my machine… but slightly different then this script does:

 mogluszka@mogluszkaW540:~/Pulpit/blog$ dpkg -l dotnet +++-=============================================-===========================-===========================-=============================================================================================== ii dotnet 1.0.0.001675-1 amd64 .NET Core & command line tools 

So I have just removed it via apt-get.

$ sudo apt-get purge dotnet

And I have made my first (very) small contribution to the dotnet-cli!

Step 2 – Install

Following current (18.05.2016) instruction works fine.

Step 3/4 – Initialize and Run

You can do this step, but this creates only console app. Not ASP.NET app, so you can just try is everything ok.

$ dotnet run
Project hwapp (.NETCoreApp,Version=v1.0) will be compiled because expected outputs are missing
Compiling hwapp for .NETCoreApp,Version=v1.0

Compilation succeeded.
    0 Warning(s)
    0 Error(s)

Time elapsed 00:00:03.0516280
 

Hello World!

 

Migrating to ASP.NET Core RC2 on Ubuntu

Current ASP.NET RC1 State

Project.json will be a key in this as I believe. For Release Candidate I had this:

{
  "webroot": "wwwroot",
  "version": "1.0.0-*",
  "dependencies": {    
    "Microsoft.AspNet.Server.Kestrel": "1.0.0-*",
    "Microsoft.AspNet.Mvc": "6.0.0-rc1-final",   
    "Microsoft.AspNet.Cors": "6.0.0-rc1-final"
  },
  "commands": {
    "kestrel": "Microsoft.AspNet.Server.Kestrel"        
  },
  "frameworks": {
    "dnxcore50": { 
        "dependencies": {
        }
     }    
...
}

Moving to ASP.NET Core RC2

Let’s start changing this according to sample app…

  "dependencies": {    
    "Microsoft.NETCore.App": {
      "type": "platform",
      "version": "1.0.0-rc2-3002702"
    }
  },
  "frameworks": {
    "netcoreapp1.0": {
      "imports": "dnxcore50"
    }    
  },

And run this…

$ dotnet restore
$ dotnet run
Project userService (.NETCoreApp,Version=v1.0) will be compiled because expected outputs are missing
Compiling userService for .NETCoreApp,Version=v1.0
/home/mogluszka/Sources/shout/src/userService/project.json(29,22): warning DOTNET1015: The 'publishExclude' option is deprecated. Use 'publishOptions' instead.
/home/mogluszka/Sources/shout/src/userService/project.json(36,15): warning DOTNET1015: The 'exclude' option is deprecated. Use 'exclude' within 'compile' or 'embed' instead.

Compilation succeeded.
 2 Warning(s)
 0 Error(s)

Time elapsed 00:00:03.7673231 

Expect required library libhostpolicy.so to be present in [/home/mogluszka/Sources/shout/src/userService/bin/Debug/netcoreapp1.0]
 - This may be because of an invalid .NET Core FX configuration in the directory.
mogluszka@mogluszkaW540:~/Sources/shout/src/userService$ dotnet info
No executable found matching command "dotnet-info"
mogluszka@mogluszkaW540:~/Sources/shout/src/userService$ dotnet --info
.NET Command Line Tools (1.0.0-preview1-002702)

Ok, so few warnings and that’s all. Shouldn’t be the big deal.

(2 days later…)

Final solution – Works!

There is some sort of documentation about migration to ASP.NET Core RC2.
There are horrendous major changes in latest release:

  • namespaces have been renamed
  • methods have been changed
  • project.json has been changed

Shortly it’s a nightmare, but you can find extremely KISS solution here: GitHub User Service. If not there, take a look on master :), or below…

project.json

{
   "dependencies": {
    "Microsoft.NETCore.App": {
      "version": "1.0.0-rc2-3002702",
      "type": "platform"
    },
    "Microsoft.AspNetCore.Mvc": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.0-rc2-final",

    //solves:  error CS1061: 'ConfigurationBuilder' does not contain a definition for 'SetBasePath' and no extension method 'SetBasePath' 
    "Microsoft.Extensions.Configuration.Json": "1.0.0-rc2-final"
  },
  "frameworks": {
    "netcoreapp1.0": {
      "imports": [
        "dotnet5.6",
        "dnxcore50",
        "portable-net45+win8"
      ]
    }
  },
  "buildOptions": {
    "emitEntryPoint": true,
    "preserveCompilationContext": true
  },
  "runtimeOptions": {
    "gcServer": true
  },
  "publishOptions": {
    "include": [
      "wwwroot",
      "Views",
      "appsettings.json",
      "web.config"
    ]
  }
}

Program.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;

namespace Shout.UserService
{

public class Program
{
  public static void Main(string[] args)
  {
      var host = new WebHostBuilder()
          .UseKestrel()
          .UseContentRoot(Directory.GetCurrentDirectory())
          .UseIISIntegration()
          .UseStartup()
          .Build();

      host.Run();
  }
}
}

 

Startup.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

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

namespace Shout.UserService
{
    public class Startup
    {
        public Startup(IHostingEnvironment env)
        {
              var builder = new ConfigurationBuilder()
                .SetBasePath(env.ContentRootPath);
                
            Configuration = builder.Build();
        }
        
        public IConfigurationRoot Configuration { get; }

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();            
        }
    
    public void Configure(IApplicationBuilder app)
        {
            app.UseMvc(routes =>
             {
                 routes.MapRoute(
                     name: "default",
                     template: "{controller=Home}/{action=Index}/{id?}");
             });
            
        }
    }
}

Controller

using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;


namespace Shout.UserService.Controllers
{
    class User
    {
        public string UserName { get; set;}
    }
    
    [Route("api/[controller]")]
    public class UserController : Controller
    {
        // GET: api/user
        [HttpGet]
        public JsonResult Get()
        {            
            var user = new User() { UserName = "michalogluszka" };
            return Json(user);            
        }
    }
}

 

NuGet.config

NuGet Config files used:
/home/mogluszka/.nuget/NuGet/NuGet.Config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
 <packageSources> 
 <add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
 <add key="dotnet-core" value="https://dotnet.myget.org/F/dotnet-core/api/v3/index.json"/>
 <add key="aspnetrelease" value="https://www.myget.org/F/aspnetrc2/api/v3/index.json" />
 </packageSources>
</configuration>

4 thoughts on “Microservice on ASP.NET Core RC2 on Ubuntu

  • Pingback: dotnetomaniak.pl

  • June 1, 2016 at 7:48 pm
    Permalink

    You seem to have shown Program.cs for the second time instead of Startup.cs.

    Also, you don’t need NuGet.config if you’re only using RC2 final packages.

    Reply
    • June 1, 2016 at 7:59 pm
      Permalink

      Thanks for comment, I fixed Startup.cs.
      As far as NuGet.config, I haven’t been able to run it that time without it.
      Actually it was a key to success, but maybe something has changed already.

      Reply
  • June 6, 2016 at 1:32 pm
    Permalink

    thanks for sharing such a valuable content. i think Ubuntu as a most difficult operating system but i love to code in asp.net and PHP. this helps a lot of people who use ubuntu and do code for dotnet development

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *