Microservice with ASP.NET Core & MVC 6 on Linux

microservice template

Moving to Microservice with ASP.NET Core & MVC6

In last post REST Service I have set up extremely simple REST Service with MVC6. Now let’s try to create starting point for Microservice with ASP.NET Core. Did I mention that this is done on Ubuntu?

Restructuring project

I have decided to choose following naming convention and structure.

├── userService
│   ├── bin
│   │   └── Debug
│   │       └── dnxcore50
│   │           ├── userService.dll
│   │           └── userService.xml
│   ├── Controllers
│   │   └── UserController.cs
│   ├── project.json
│   ├── project.lock.json
│   └── Startup.cs
└── web
    └── index.html

As you can see I have removed all unnecessary files from previous post. These are only things that you need to run ASP.NET Core stuff on your machine.
UserController is our REST Service from last post. In web part I have left only static html file which will be calling this service.

JSON as Service Result

A bit surprising, but on .NET Core returning JSON works without much problems and any addtional libraries.

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

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

Let’s test it right now:

mogluszka@mogluszkaW540:~/Sources/shout/src/userService$ dnx kestrel
Hosting environment: Production
Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.

Cool that works.

Calling REST Service via jQuery

Should be pretty simple… I found it on StackOverflow (index.html)
A lot of html and very simple call. What we get?

XMLHttpRequest cannot load http://localhost:5000/api/user. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.


Shortly, browser won’t allow you to make a call from different domain. It’s of course more complicated and you can start reading in wikipedia: https://en.wikipedia.org/wiki/Cross-origin_resource_sharing
But the real question is, how to resolve it?
Whatever you will find in google won’t work
This may be simplification, but the problem is that .NET team changes their API more frequently then JavaScript teams.
Here is a solution that is compatible with rc1-update1


  "dependencies": {    
    "Microsoft.AspNet.Server.Kestrel": "1.0.0-*",
    "Microsoft.AspNet.Mvc": "6.0.0-rc1-final",   
    "Microsoft.AspNet.Cors": "6.0.0-rc1-final"

Now you need to configure your services. Proper API in RC1 is:

using Microsoft.AspNet.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNet.Cors;

namespace Shout.UserService
    public class Startup
        // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
            services.AddCors(options => options.AddPolicy("AllowAll", p => p.AllowAnyOrigin()

        public void Configure(IApplicationBuilder app)

And now… it’s alive!

3 thoughts on “Microservice with ASP.NET Core & MVC 6 on Linux

Leave a Reply

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