Skip to content

Latest commit

 

History

History
75 lines (59 loc) · 5.31 KB

multi-service-netcore.md

File metadata and controls

75 lines (59 loc) · 5.31 KB
title services ms.date ms.topic description keywords
Running multiple dependent services: .NET Core & Visual Studio Code
azure-dev-spaces
11/21/2018
tutorial
This tutorial shows you how to use Azure Dev Spaces and Visual Studio Code to debug a multi-service .NET Core application on Azure Kubernetes Service
Docker, Kubernetes, Azure, AKS, Azure Kubernetes Service, containers, Helm, service mesh, service mesh routing, kubectl, k8s

Running multiple dependent services: .NET Core and Visual Studio Code with Azure Dev Spaces

In this tutorial, you'll learn how to develop multi-service applications using Azure Dev Spaces, along with some of the added benefits that Dev Spaces provides.

Call a service running in a separate container

In this section, you will create a second service, mywebapi, and have webfrontend call it. Each service will run in separate containers. You'll then debug across both containers.

Multiple containers

Download sample code for mywebapi

For the sake of time, let's download sample code from a GitHub repository. Go to https://github.com/Azure/dev-spaces and select Clone or Download to download the GitHub repository. The code for this section is in samples/dotnetcore/getting-started/mywebapi.

Run mywebapi

  1. Open the folder mywebapi in a separate VS Code window.
  2. Open the Command Palette (using the View | Command Palette menu), and use auto-complete to type and select this command: Azure Dev Spaces: Prepare configuration files for Azure Dev Spaces. This command is not to be confused with the azds prep command, which configures the project for deployment.
  3. Hit F5, and wait for the service to build and deploy. You'll know it's ready when the Application started. Press Ctrl+C to shut down. message appears in the debug console.
  4. The endpoint URL will look something like http://localhost:<portnumber>. Tip: The VS Code status bar will turn orange and display a clickable URL. It might seem like the container is running locally, but actually it is running in our dev space in Azure. The reason for the localhost address is because mywebapi has not defined any public endpoints and can only be accessed from within the Kubernetes instance. For your convenience, and to facilitate interacting with the private service from your local machine, Azure Dev Spaces creates a temporary SSH tunnel to the container running in Azure.
  5. When mywebapi is ready, open your browser to the localhost address. Append /api/values to the URL to invoke the default GET API for the ValuesController.
  6. If all the steps were successful, you should be able to see a response from the mywebapi service.

Make a request from webfrontend to mywebapi

Let's now write code in webfrontend that makes a request to mywebapi.

  1. Switch to the VS Code window for webfrontend.

  2. Replace the code for the About method in HomeController.cs:

    public async Task<IActionResult> About()
    {
        ViewData["Message"] = "Hello from webfrontend";
        
        using (var client = new System.Net.Http.HttpClient())
            {
                // Call *mywebapi*, and display its response in the page
                var request = new System.Net.Http.HttpRequestMessage();
                request.RequestUri = new Uri("http://mywebapi/api/values/1");
                if (this.Request.Headers.ContainsKey("azds-route-as"))
                {
                    // Propagate the dev space routing header
                    request.Headers.Add("azds-route-as", this.Request.Headers["azds-route-as"] as IEnumerable<string>);
                }
                var response = await client.SendAsync(request);
                ViewData["Message"] += " and " + await response.Content.ReadAsStringAsync();
            }
    
        return View();
    }

The preceding code example forwards the azds-route-as header from the incoming request to the outgoing request. You'll see later how this helps teams with collaborative development.

Debug across multiple services

  1. At this point, mywebapi should still be running with the debugger attached. If it is not, hit F5 in the mywebapi project.
  2. Set a breakpoint inside the Get(int id) method that handles api/values/{id} GET requests. This is around line 23 in the Controllers/ValuesController.cs file.
  3. In the webfrontend project, set a breakpoint just before it sends a GET request to mywebapi/api/values. This is around line 32 in the Controllers/HomeController.cs file that you modified in the previous section.
  4. Hit F5 in the webfrontend project.
  5. Invoke the web app, and step through code in both services.
  6. In the web app, the About page will display a message concatenated by the two services: "Hello from webfrontend and Hello from mywebapi."

Well done!

You now have a multi-container application where each container can be developed and deployed separately.

Next steps

[!div class="nextstepaction"] Learn about team development in Dev Spaces