Click here to Skip to main content
15,867,686 members
Articles / Containers / Docker

Debug multiple .NET Core projects concurrently running on different docker containers in VS 17

Rate me:
Please Sign up or sign in to vote.
4.14/5 (5 votes)
11 Jun 2017CPOL4 min read 14.5K   133   2   2
Debugging multiple .NET core projects running on docker containers.

Introduction

In the microservices based application world we often need to debug multiple projects at the same time. And after docker's release the development of microservices based application has changed a lot. In Visual Studio 17 we can easily add the docker support for our application. In this article we'll see how we can debug two .NET Core project runnining on different docker containers.

Why debug concurrently

Out of microservices suppose, we have an application that has two project one is a ASP.NET MVC project and another one is a Web API project. Our MVC project gets data from our API project and API project serves the data when needed. In the development face of our application we'll often need to debug these two projects concurrently to observe the behaviour of our application.

Why use docker

Docker is a lightweight, open and secure platform for developers. It's a containerization tool that is often compared to virtual machine where vm consumes a lot of resources. But what benefits are docker providing for developers? This below one line is describing the major benefit of using docker-

Docker provides a consistent way to ship our code around different environment.

So, with docker we can develop and run our software on different environment by using minimal computing resources. Docker can- eliminate app conflict, ship software faster, make sure better usage of our resources and more.

What is docker container

Docker changing the world since it's debut and docker container is a very important role for this mission. Docker containers are basically the instances of docker images, we can simplify this as- docker containers are all about running softwares! We can have as many as docker container for any specific image.

Using the code

Before using the code we need to make sure some things, first of all check that you've docker installed and running perfectly and any version of vs 17 with .NET core component.

Creating the solution as Step 1:

In this article we'll create a test application called Employee attendance that will show a company's employees daily attendance data. To do so first of all we'll create a visual studio solution with two .NET core projects, one is ASP.NET MVC and another one is ASP.NET Web API like below-

Changing API Controller as Step 2:

Visual studio is awesome! it's already created a controller and some views for us in the MVC project and also a controller in API project. Let's make some changes in our API controller to get employee attendance data,

C#
        public IEnumerable<EmployeeAttendanceData> Get()
        {
            List<EmployeeAttendanceData> attendances = new List<EmployeeAttendanceData>
            {
                new EmployeeAttendanceData{ Name="Romel", LogInTime = new DateTime(2017, 6, 1, 12, 20, 0), Status="Late" },
                new EmployeeAttendanceData{ Name="Ashek", LogInTime = new DateTime(2017, 6, 2, 8, 20, 0), Status="In time" },
                new EmployeeAttendanceData{ Name="Hassan", LogInTime = new DateTime(2017, 6, 3, 9, 20, 0), Status="In time" },
                new EmployeeAttendanceData{ Name="Chapal", LogInTime = new DateTime(2017, 6, 4, 9, 15, 0), Status="In time" },
                new EmployeeAttendanceData{ Name="Maruf", LogInTime = new DateTime(2017, 6, 5, 11, 20, 0), Status="Late" }
            };

            return attendances;
        }

We've created a GET method in our API controller for providing attendance data. Note that we've also created a model class named EmployeeAttendanceData.

Getting attendance data as Step 3:

From our MVC controller we will call the API controller's method to get attendance data. So let's code that-

C#
public IActionResult Index()
{
    ValuesController apiController = new ValuesController();
    IEnumerable<EmployeeAttendanceData> attendances = apiController.Get();
    ViewBag.attendances = attendances;
    return View();
}

In the above action mmethod we're calling API controller's Get method and storing attendance data in the ViewBag, after we'll enumerate this collection in our view.

Add docker support as Step 4:

We can add individual docker support in our projects and also in our solution file. To add project wise docker support right click on the project and under Add  menu you'll see the option to add docker support-
 
dockerSupport
We've to add docker support for our each project. After adding docker support we'll see a Dockerfile has been added in our project, this file has entrypoint, workdir and some other information for the container.  And also notice that a new docker-compose project has been added in our solution, under the docker-compose there is a docker-compose.yml file to instansiate our project's docker container.
 

Running projects on container as Step 5:

Now we've docker support for our projects, so it's time to create our project's docker images and run them on container. Run the application with Docker to create the project's images and running them on container. After successfull creation of images and container open Windows PowerShell and run this command-

MC++
docker ps

you'll see your currently running docker containers like this-

RunningContainersNow if we open our browser and type this url in the address bar- http://localhost:32769/ we'll see our application is running on docker linux container.

Debugging concurrently as Step 6:

Now we'll set two break points- one in our MVC controller's action method and another one in our API controller's GET method. And now if we refresh our application from the browser window we'll see that our application breaks concurrently on that two break points. If we go to our solution's property window, we'll find that we have single project as the startup project-

solutionPropertyWindow

 So, we can debug multiple projects at the same time in Visual Studio 17 running on docker container.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Junior) Computer Ease Limited
Bangladesh Bangladesh
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
PraiseThanks Pin
kware21-Nov-17 21:29
kware21-Nov-17 21:29 
Question:) Pin
Member 1124779614-Jun-17 8:58
Member 1124779614-Jun-17 8:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.