13 Dec 2018 - garfbradaz - blog
Setting this option does what you might expect – it tells Visual Studio to not open a web page in the default browser when you start the server. This is a lesser-known option that proves to be convenient when working with ASP.NET Web API projects, as ASP.NET Web API projects have no user interface. Visual Studio Code is a lightweight but powerful source code editor which runs on your desktop and is available for Windows, macOS and Linux. It comes with built-in support for JavaScript, TypeScript and Node.js and has a rich ecosystem of extensions for other languages (such as C, C#, Java, Python, PHP, Go) and runtimes (such as.NET and Unity). To learn to build web apps with the Django and Flask frameworks, see the following tutorials: Use Django in Visual Studio Code; Use Flask in Visual Studio Code; There is then much more to explore with Python in Visual Studio Code: Editing code - Learn about autocomplete, IntelliSense. Visual Studio Code is free and available on your favorite platform - Linux, macOS, and Windows. Windows media video player for mac. Download Visual Studio Code to experience a redefined code editor, optimized for building and debugging modern web and cloud applications.
Debugging ASP.NET Core in a Linux Docker with Visual Studio Code
So, you have started using Docker for your .NET Core applications - awesome news. You are using Linux Containers, and Visual Studio Code, and you then go to Debug your container….oh. (I'm using Windows).
Its not the most straight forward process I have found, if you are looking at getting this up and running. Firstly I didn't figure this out on my own, the reason I'm blogging about it is because I meshed together the knowledge I had, plus these very informative posts:
It was the above two posts that glued it together for me. Have a good read of those as well. Before we go any further, just to confirm these instructions are for Visual Studio Code and Linux Containers using Docker Compose, I'm not sure how this is set-up on Visual Studio. I'm presuming you have VSCode, Docker and .NET Core SDK installed. And you are using Linux Containers.
DockerFile
Firstly lets look at my multi-staged (Multiple FROM
) Dockerfile for building my project.
1st Stage Build
The first part of the Dockerfile build-env
stage, takes an ARG
of buildconfig
, which controls how the dotnet publish
is configured. And yes I know, I have a todo item to refactor the if/else to just pass the buildconfig
as a -c
Make photo size smaller mac. value.
2nd Stage Run
So the 1st stage handled the build, the 2nd handles the running of the Web API project. As you can see, there is a Debug
switch again, to install some dependencies when running in Debug
. Ignore the mongo installs, it is the vsdbg installation We are interested in.
curl -sSL https://aka.ms/getvsdbgsh | bash /dev/stdin -v latest -l
The debugger platform has been expanded between Visual Studio 2017 15.3 andprevious versions of Visual Studio. Visual Studio can now communicate usingthe debug adapter protocol which is used by Visual Studio, Visual Studio Codeand Visual Studio for Mac. This protocol is now used for debuggingcross-platform .NET Core applications.
Must have free mac apps. So vsdbg is installed into the container to allow remote debugging from your host machine using Visual Studio Code.
Launch Task Configuration for Visual Studio Code
So this was the bit I struggled with, to the point I logged a Github Issue and subsequently had a light bulb moment afterwards!
Both Jonathan and Richards work and investigations for their posts, again helped tenfold.
Important bits are the sourceFileMap
which map your projects code to the binaries in the Container. The /app in reference in mine, relates to the /app directory declared in the Dockerfile.
The other important snippet is the debuggerPath
option. This is the directory where vsdbg has been installed within your Container. You can set different directories within your curl
command in the Dockerfile if you so wish.
Lastly, note the Container name of webapi_debug
, I set this up in the relevant compose file.
Commander one pro 1 2 download free. The below will live in a launch.json
in your .vscode
directory. If configured correctly you should get a nice launch icon:
Composing
Because I'm using separate Docker containers for MongoDB and Identity Server I use docker-compose
to knit together my containers.
Note: I'm planning on doing a Identity Server post next on my experiences in getting that set-up.
The below is from my docker-compose.dev.yml
file.
Important bits are the sourceFileMap
which map your projects code to the binaries in the Container. The /app in reference in mine, relates to the /app directory declared in the Dockerfile.
The other important snippet is the debuggerPath
option. This is the directory where vsdbg has been installed within your Container. You can set different directories within your curl
command in the Dockerfile if you so wish.
Lastly, note the Container name of webapi_debug
, I set this up in the relevant compose file.
Commander one pro 1 2 download free. The below will live in a launch.json
in your .vscode
directory. If configured correctly you should get a nice launch icon:
Composing
Because I'm using separate Docker containers for MongoDB and Identity Server I use docker-compose
to knit together my containers.
Note: I'm planning on doing a Identity Server post next on my experiences in getting that set-up.
The below is from my docker-compose.dev.yml
file.
So the container with the interesting configurations is webapi
which is my ASP.NET Core Web API project. As you can see I set the container name to webapi_debug
is set. As I mentioned before, the launch task needs that to remote into that particular container.
You can also see I mount my Source Code directory from my host Windows machine using volumes
using a relative path to my source code directory (./api) to :/app, allowing us, when a breakpoint is hit, for the code to be shown. Update mac os 10 11. If you configure this bit wrong, you will get the following error when the breakpoint is thrown:
Visual Studio Api Call
The other interesting config points, are the ASPNETCORE_* environment variables for Development
and the buildconfig
argument of Debug
, which as we learnt previously, is passed into the Dockerfile
and used.
Running
So put this all together you can run your containers using the Compose:
docker-compose -f docker-compose.dev.yml up -d --build
Set a breakpoint in your code (Example I set in my Controller), and run your debug task in Visual Studio Code. This will ask you to pick your process for your dotnet
code. Then when the breakpoint is hit, you can inspect it as normal.
I find that it can hang and take half a minute sometimes for the debug session to initiate, so bear with it. https://eaklt.over-blog.com/2021/01/basic-topology-armstrong-pdf.html.
Web Api Visual Studio 2019
Anyway I hope this helps someone, and a massive thank you to Jonathan and Richard.
Lover of tech, running and Guinness