Home > Enterprise >  How to create, build and run a multi ASP.NET Core project in a single folder VsCode workspace?
How to create, build and run a multi ASP.NET Core project in a single folder VsCode workspace?

Time:12-12

How do I create, build and run multiple ASP.NET Core projects in a single folder in a VS Code workspace, for a repository that has a single startup project and one or more class libraries?

The documentation for multiple-root workspaces states that it is useful when dealing with multiple projects that are not stored in the same parent folder. Projects stored in the same repository though are all stored in the same parent folder, so how do I handle that situation in the simplest way, ideally without having to use multi-root workspaces, and ideally in a manner that is amenable to running in a dev container.

I've looked through the VS Code documentation and don't see anything about this at all, and Googling just turns up a few blogs from years ago that look quite out-of-date.

CodePudding user response:

It seems there is no documentation because 'it just works'.

Using VsCode you can open a folder in a dev container using an ASP Core 5 image. Manually create a folder at the workspace root and create an ASP Core project in that folder using dotnet new webapi. Run using dotnet run and after generating a dev certificate the service is accessible from outside the container.

Manually create a new subfolder at the workspace root and create a class library project in that folder using dotnet new classlib. Build it using dotnet build.

Reference the classlib from the service using dotnet add reference ../project2.csproj.

Two very nice bonuses.

  1. Running dotnet watch run on the service project will detect and build changes in both the service and the dependency.

  2. Generating a default debug configuration in VsCode will attach to the running service (once you locate the correct process), and will step into the dependency project too with no additional configuration required.

  • Related