With .Net Core we can develop .Net application in Linux; we have already seen how we can get started by creating and running a single .Net project in Debian Stretch. Now let’s see how we can use the .Net Core CLI to create a .Net solution that contains different projects. That’s the setup that is used in real development, since it allows to keep the different components that form our applications: libraries, APIs, test projects, console applications, etc – neatly ordered and separated in their own projects.
How to create a .sln solution in Linux
We go to the folder where we want to keep our .Net solutions/projects and run this command:
dotnet new sln -o bAnalyzer
In the line before bAnalyzer is the name of the solution I want to create. indeed, after running the command a new bAnalyzer folder has appeared.
Next we are going to add the different projects that make up our solution: in my case I need a library project, and a console project that will use the class library.
So we move into directory bAnalyzer and run this two commands to create the aforementioned projects:
dotnet new classlib -o bAnalyzer.core dotnet new classlib -o bAnalyzer.console
Notice that after running those commands the bAnalyzer.core folder and the bAnalyzer.console folder have been created. Also if we navigate into this projects we will see the corresponding *.csproj files, and the other files related to the type of project we have created.
How to add projects to a solution file
Once we have created the solution and the different projects we need to add the projects to the solution:
dotnet sln ./bAnalyzer.sln add ./bAnalyzer.core/bAnalyzer.core.csproj ./bAnalyzer.console/bAnalyzer.console.csproj
Also I want to add to my console project a reference to the core project:
dotnet add ./bAnalyzer.console/bAnalyzer.console.csproj reference ./bAnalyzer.core/bAnalyzer.core.csproj
Now that the initial setup is finished I can use Visual Code to open the solution and start coding :-).
Further CLI commands
In our everyday there are a bunch of different actions we do on sour solutions besides creating them and adding: projects: we add NuGet packages to a project, we remove projects, remove references, run the solution, run tests, etc:
- dotnet new
- dotnet run
- dotnet test
- dotnet sln add
- dotnet sln remove
- dotnet add reference
- dotnet remove reference
- dotnet add package
- dotnet remove package