Aim

I have always created more complex solutions, which are more than 1 project, through the friendly and familiar UI of Visual Studio. Now we have had multiple version of dotnet core and we are about to receive .NET 5 by the end of the year I thought it would be fun to see if I can setup a more involved solution purely on the command line.

For fun I will also look to get it running with the new Project Tye which Microsoft demo’d at Build.

So let’s get started.

Creating the folder structure

To start with I want to create the familier src and tests folders in the root of the project directory. This is a common practice for most solutions now a days.

mkdir src
mkdir tests

Creating the main applications

Now we have the src and tests folders setup I want to be able to create a new webapp project for the UI, a new webapi project for the api and a common Core project.

cd src
dotnet new webapp -o Web
dotnet new webapi -o Api
dotnet new classlib -o Core

The Core project is for demo purposes and to discuss how to reference projects etc. futher on in the process. You don’t need to have a Core project, or a common project. A potential use would be a common POCO type project for data transfer objects between the web and api projects but that would be down to the type of application you are building. I will leave that up to you.

Creating the test projects

It is very important there are tests written and they require a tests project. For this demo I have decided on a tests project for each of the webapp and webapi applications. As my unit testing framework of choice is xunit this is the template I have chosen.

cd ../tests
dotnet new xunit -o Web.Tests
dotnet new xunit -o Api.Tests

Creating the solution

I would normally do my development in Visual Studio so I would want to create a sln file and reference all of the projects. We’ve now got 5 projects which need referencing. This can also all be done on the command line.

cd ..
dotnet new sln
dotnet sln add .\src\Api\
dotnet sln add .\src\Web\
dotnet sln add .\src\Core\
dotnet sln add .\tests\Api.Tests\
dotnet sln add .\tests\Web.Tests\

Creating the references

Now we have our projects created, our solution created and all of the projects referenced in that solution the next step is to add some references. At this point none of the projects know about any of the others. We can’t write any tests againsts the applications and the applications can’t use any of the code in the Core solution.

To do this we use the add argument with the source and the reference for the projects we want to reference.

dotnet add .\src\Web\Web.csproj reference .\src\Core\Core.csproj
dotnet add .\src\Api\Api.csproj reference .\src\Core\Core.csproj
dotnet add .\tests\Api.Tests\Api.Tests.csproj reference .\src\Api\Api.csproj
dotnet add .\tests\Web.Tests\Web.Tests.csproj reference .\src\Web\Web.csproj

Getting them all to run

Now we’ve got the solution created and multiple projects which we want to start at run time we need to either load it into Visual Studio and set multiple startup projects or look to use Microsoft’s new experiment “Project Tye”.

To use tye we first have to install it. This is a global tool which is installed on the command line.

dotnet tool install -g Microsoft.Tye --version "0.4.0-alpha.20371.1"

Once the tool is installed we can now use it.

I’ve navigated back to the root of my solution and run the following command at the same level as the sln file.

tye run

This will output something similar to the following:

C:\src\Playground\microservices> tye run
Loading Application Details...
Launching Tye Host...

[23:38:11 INF] Executing application from C:\src\Playground\microservices\microservices.sln
[23:38:11 INF] Dashboard running on http://127.0.0.1:8000
[23:38:12 INF] Building projects
[23:38:14 INF] Launching service api_acd89852-b: C:\src\Playground\microservices\src\Api\bin\Debug\netcoreapp3.1\Api.exe
[23:38:14 INF] Launching service web_71725a46-e: C:\src\Playground\microservices\src\Web\bin\Debug\netcoreapp3.1\Web.exe
[23:38:14 INF] api_acd89852-b running on process id 8332 bound to http://localhost:55220, https://localhost:55221
[23:38:14 INF] web_71725a46-e running on process id 7208 bound to http://localhost:55222, https://localhost:55223
[23:38:14 INF] Replica api_acd89852-b is moving to a ready state
[23:38:14 INF] Replica web_71725a46-e is moving to a ready state
[23:38:14 INF] Selected process 7208.
[23:38:14 INF] Selected process 8332.
[23:38:14 INF] Listening for event pipe events for web_71725a46-e on process id 7208
[23:38:14 INF] Listening for event pipe events for api_acd89852-b on process id 8332

Looking at the output above you can see it has started up both the web and api projects and they are now running on separate ports for both http and https. It has also created and started up a dashboard automatically which allows acces to the individual urls. In the dashboard there is a lot more information which can be explored.

Conclusion

In this post we have created a none trivial solution setup, albeit still relatively simple, with both web and api projects. We have added unit test projects for both and added references. All of this being done on the command line using the dotnet command.

We have then started up the new projects at the same time using Project Tye and allowed for browsing the applications as if they were run up separately. These projects are not linked at the moment but you can see them running, view their logs and counters through the generated dashboard.

Any questions/comments then please contact me on Twitter @WestDiscGolf

Further reading

Cli reference - https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-add-reference