Cloud Concept Limited Logo

How-To Create a simple WebApi Docker Microservice

microservices
Neil Tomlinson
#microservices#WebApi#Docker#howto

ASP.NET 5 on Linux

Microsoft is pushing ahead with its commitment to make ASP.NET 5 a truly cross platform framework. I have been a .Net developer for a number of years and I am keen to embrace this new experience of building .Net web applications that can run on Linux. It is still early days though and I don’t want to clutter up my machine with pre release code and so I am going to use Docker.

Docker

With Docker I can use the machine image published by Microsoft as the base for my image. In future I could publish my image for other developers to consume so they don’t have to build their own.

Another reason I am using docker is that this service is potentially one small service(micro) in a group of other services. Using docker makes it really simple to have services written in different programming languages running side by side on my development machine.

Docker Compose

Docker compose makes it easy to define a group of docker instances that collaborate to provide our service. These would be deployed as a unit running near to each other.

Instances in the group might have one of the following roles.

The Code

In ASP.NET 5 we see that MVC 6 has finally unified MVC and WebAPI. So now our Api controller will look something like this:


  using Microsoft.AspNet.Mvc;
  using Chuck.Models;

  namespace Chuck.Controllers
  {
    [Route("[controller]")]
    public class FactController : Controller
    {    
      [HttpGet]
      public IActionResult Fact()
      {
        return new ObjectResult(new ChuckFact {
          Content="Chuck Norris can divide by zero."
        });
      }
    }
  }

Next create a startup class that looks like this.

  using Microsoft.AspNet.Builder;
  using Microsoft.Extensions.DependencyInjection;
  using Microsoft.Extensions.Logging;
  using Newtonsoft.Json.Serialization;

  namespace Chuck
  {
    public class Startup
    {
      public void ConfigureServices(IServiceCollection services)
      {
        services.AddMvc().AddJsonOptions(options => {
          options.SerializerSettings
            .ContractResolver = new CamelCasePropertyNamesContractResolver();
        });
      }

      public void Configure(IApplicationBuilder app)
      {
        app.UseMvc();
      }
    }
  }

Next to my StartUp.cs I create a file name ‘Dockerfile’ with the following contents.

 FROM microsoft/aspnet:latest

 COPY . /app
 WORKDIR /app

 RUN ["dnu", "restore"]

 ENTRYPOINT ["dnx","-p","project.json", "web"]

Next in the root of my project I create a docker-compose.yml to describe what is required for this service. This step is a bit contrived as currently I only have one service with no dependencies but we can extend this in future.

  api:
    build: ./src/api
    ports:
      - "8000:8000"

To run our service type

  docker compose up

This will build the image which includes restoring the required .net package dependencies this step may take a little time. Then finally something like the following console output should appear at which point your docker instance and its service are ready.

  Creating chuck_api_1
  Attaching to chuck_api_1
  api_1 | Hosting environment: Production
  api_1 | Now listening on: http://0.0.0.0:8000
  api_1 | Application started. Press Ctrl+C to shut down.

To test the service we can issue a get request like this:

  curl http://127.0.0.1:8000/fact

This should return a json response containing a Chuck Norris fact.

Finally

I hope you found this article useful. In a future post I will explore the local setup when we have a project with multiple service dependencies.

← Back to Blog