3. Microservices server-side

3.1 About coding the server

3.2 There comes .NET Core

3.3 Introduction to ASP.NET Core

3.4 ASP.NET Core Web API

Project structure

Controllers, actions and routes

Complex return types

3.5 Exercise - Create a train scheduling service

{"departureTime":"2018-01-03T21:56:48.6711219+01:00","n\
umber":"5423","destination":"Paris"}

3.6 Exercise solution

  • Start Visual Studio.
  • Click on the File / New / Project… menu entry.
  • In the New Project dialog box, select the ASP.NET Core Web Application template making sure that you select Templates / Visual C# / .NET Core on the left-hand side. In the Name zone at the bottom, type “ScheduleService”. Click the OK button.
  • In the New ASP.NET Core Web Application dialog box, select the .NET Core and ASP.NET Core 2.0 at the top. Select the Web Application (Model-View-Controler) template in the middle. Make sure the Enable Docker Support checkbox is unchecked. Click the OK button.
  • Open the Solution Explorer clicking on the View / Solution Explorer menu entry.
  • In the Solution Explorer, right-click the Controllers directory, and select Add / Controller… from the context menu.
  • In the Add Scaffold dialog box, select the API Controller - Empty template. Click the Add button.
  • In the Add Empty API Controller dialog box, type NextDepartureController as the controller name. Click the Add button.
  • Locate the following code:
[Produces("application/json")]
[Route("api/NextDeparture")]
public class NextDepartureController : Controller
{
}
  • Replace it with the following code:
[Produces("application/json")]
[Route("api/NextDeparture")]
public class NextDepartureController : Controller
{
  public IActionResult GetNext()
  {
    return Ok(new {
      DepartureTime = DateTime.Now.AddMinutes(50),
      Number = "5423",
      Destination = "Paris"
    });
  }
}
  • From the Debug menu click Start Without Debugging
  • Point your browser to the following URL (port number varies):

http://localhost:51426/api/NextDeparture

  • From a PowerShell command-line, type the following and check the returned Content:
Invoke-WebRequest http://localhost:51426/api/NextDepart\
ure