Manage and Edit soluton files with visual studio code and .net CLI
If you are a C# Developer coming from the more traditional approach of developing .net applications making use of Visual Studio Integrated Development Environment (IDE) on Windows and looking to explore the capabilities of the .net core framework on Linux or Mac operating systems.
You maybe wondering which development environment to use and what the steps are to get a basic project up and running. You will no doubt want to explore the basic capabilities of VS code.
The Visual Strudio IDE, comes pre-packaged with the lot wizards and templates that automate a number of tasks behind the scenes when creating applications. Making it easier to create new projects and files wwith a click of mouse.
Transitioning to VS Code may seem difficult at first, due in part that most of this UI sugar has been stripped out and most steps might not be as intuituve as you may have come to appreciate. However, don’t despair once you get familiar with it, you’ll find that working in VS code actually does become a lot easier and once you lose your mouse dependency and become more familar with the Terminal Window and the host fo commands available.
Most of the features you have grown to love in Visual Studio are available and can be exercised by just issuing terminal commands and over time this starts to become second nature and you’ll find your development velocity will actually become a lot quicker once you overcome the initially steep learning curve.
Visual Studio Code combines the ease of use of a classic lightweight text editor with more powerful IDE-type features with very minimal configuration.
I will assume you already have .net core framework and Visual Studio Code installed and are ready to start developing your first project.
Let’s take a look at creating
Visual Studio Solution Files in .net Core, using the Command Line Interface (CLI).
Although I will be predominantly using the ubuntu operating system, it really doesn’t matter which operating system you use, the steps are the same irrespective of platform. In fact, you don’t even need to use Visual Studio Code to follow along, as long as you have access to .net CLI you will be able to accomplish this task.
What are Solution files
If you have worked with the .net framework for any length of time, you will no doubt be well acquainted with Solution Files, which contain the text-based information about your project .
The Visual Studio IDE uses the solution file to load the name-value parameters of all the projects that comprise a software solution.
A solution is a structure for organizing projects in Visual Studio. The solution maintains the state information for projects in .sln (text-based, shared) file
Each project file will contain additional information read by the environment to populate the hierarchy of the specific project.
The information contained in a solution file is not only used by the IDE, it is also used my
MsBuild, Cake Build or any other build tool to build or compile the solution.
A solution is a structure for organising projects in Visual Studio. The solution maintains the state information for projects in text based
Do we need Solution files in VS Code ?
Although not strictly necessary from a pure development perspective, we don’t necessarily need solution files when developing with VS Code, but solution files do come in handy during the Build and Release phases of projects, so it is generally good practice to start thinking about using them in the early stages of your project development.
The latest release of the .net core framework, has seen a number of changes incorporated in regards to Development Project Management. We’ve seen the demise of the
project.json and the re-introduction of
.csproj files. We have also see the introduction the of creating Solution Files in the .net CLI
What are Project Files
Project Files a.k.a
.csproj contains of all the source code files, icons, images, data files and anything else that will be compiled into an executable program or web site, or else is needed in order to perform the compilation.
A project also contains all the compiler settings and other configuration files that might be needed by various services or components that your program will communicate with.
A project is an
XML file (*.csproj ) that defines a virtual folder hierarchy along with paths to all the items it “contains” and all the build settings.
In Visual Studio, the project file is used by Solution Explorer to display the project contents and settings. When you compile your project, the
MSBuild engine consumes the project file to create the executable. You can also customise projects to product other kinds of output.
<Folder Include="wwwroot\" />
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.0" />
Creating a new solution file
It is now really easy to create solution file making use of
To create a new solution file in your current working directory :
dotnet new sln
This works great! However, typically you may want some additional control of the naming etc of your solution file. You can do this by making use of the command line arguments.
To get a full list of the available arguments simply use the help function
dotnet new sln --help
$ dotnet new sln --help
Template Instantiation Commands for .NET Core CLI.
Usage: dotnet new [arguments] [options]
template The template to instantiate.
-l|--list List templates containing the specified name.
-lang|--language Specifies the language of the template to create
-n|--name The name for the output being created. If no name is specified, the name of the current directory is used.
-o|--output Location to place the generated output.
-h|--help Displays help for this command.
-all|--show-all Shows all templates
In order to create a new solution file with the name of
SwcApi we simplay provide the name parameter
dotnet new sln --name SwcApi
Content generation time: 20.8484 ms
The template "Solution File" created successfully.
Your new solution file is now created and you can open and inspect the file in VS Code. Your Solution file is of no use until you start adding projects to it. so lets go ahead and some projects to it to start off.
Adding Projects to solution files
In my example I am going to add 2 new projects then I will add them to the Solution file. We’ll create an Web Api project and simple Class Library project.
dotnet new webapi --name api
dotnet new classlib --name Api.Database
We can now add these projects to our solution file using the
dotnet sln [path-to-solution.sln] add [path-to-project.csproj]
dotnet sln "swcApi.sln" add "api/api.csproj"
dotnet sln "swcApi.sln" add "Api.Database/Api.Database.csproj"
You’ll see a confirmation message. You have npow successfully added projects to your solution file.
Removing projects from solution files
Removing projects files using the CLI is also fairly trivial process. Making use of the
remove command i.e.
dotnet sln remove [path-to-project.csproj]
dotnet sln remove "someDodgy.csproj"
.NET CLI has the ability to list the projects in the solution file, instead of having to open it up and wade through the litany of GUIDs, which is especially handy when you working on a headless linux build server!
dotnet sln list
$ dotnet sln list
Making use of the .net core cli you can manage your solution files with ease, without having to edit the file by hand. It really is the worth the time and effort learning how to make use of the solution files, as it does drastically reduce the effort required in building your project later!
A unique background as business owner, marketing, software development and business development ensures that he can offer the optimum business consultancy services across a wide spectrum of business challenges.
Latest posts by Gary Woodfine (see all)
- Using IHost .net core console applications - August 15, 2018
- Book Review : Code Complete - August 6, 2018
- How to use Configuration API in .net core console application - August 6, 2018