.NET Core - When to use "dotnet new sln"

asp.net Core.Net Core

asp.net Core Problem Overview


I'm a bit confused - the majority of .NET Core tutorials I have been reading have not mentioned "dotnet new sln" - they always just create the projects separately without any solution file to link them together.

  1. Is "dotnet new sln" a new command?

  2. When should I use this? What benefits do I gain from creating a .sln file instead of just having project files? Is it mainly for opening in Visual Studio? I use Visual Studio Code for Mac, so it may not be applicable.

I have googled "dotnet new sln" and the results are very minimal.

asp.net Core Solutions


Solution 1 - asp.net Core

> Is "dotnet new sln" a new command?

Yes. In version 1.0.1 of the dotnet command line interface, there is a dotnet new sln command. The command came with the change from project.json to csproj. If we run dotnet new --help, we will see "Solution File" as one of the templates.

> dotnet new --help

Templates                 Short Name      Language      Tags
----------------------------------------------------------------------
Console Application       console         [C#], F#      Common/Console
Class library             classlib        [C#], F#      Common/Library
Unit Test Project         mstest          [C#], F#      Test/MSTest
xUnit Test Project        xunit           [C#], F#      Test/xUnit
ASP.NET Core Empty        web             [C#]          Web/Empty
ASP.NET Core Web App      mvc             [C#], F#      Web/MVC
ASP.NET Core Web API      webapi          [C#]          Web/WebAPI
Solution File             sln                           Solution

> when should I use this?

Two times to use a solution file are:

  1. when we want to use Visual Studio, and/or
  2. when we want to manage several projects as a single unit.

> What benefits do I gain from creating a .sln file instead of just having project files? Is it mainly for opening in Visual Studio? I use Visual Studio Code for Mac, so it may not be applicable.

One of the benefits that do not require Visual Studio is the management of multiple projects as a single unit.

For instance, on a Mac with Visual Studio Code, we can use the dotnet CLI to create a new solution, create a few projects, add those projects to the solution, restore the solution, and build the solution.

dotnet new sln --name FooBar
dotnet new console --name Foo --output Foo
dotnet new console --name Bar --output Bar
dotnet sln add .\Foo\Foo.csproj
dotnet sln add .\Bar\Bar.csproj
dotnet restore
dotnet build FooBar.sln

The last command, which calls dotnet build, has the benefit of building all the projects that are in the solution. Without a solution, we would need to call dotnet build on each project.

There are no doubt other benefits which do not require the use of Visual Studio. I leave those to you to discover.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAndyNZView Question on Stackoverflow
Solution 1 - asp.net CoreShaun LuttinView Answer on Stackoverflow