Chapter 2. Working with solutions and projects
Introducing basic concepts
Project
A project contains all the source code files, data files, etc. that are compiled into a library or executable, and also includes compiler settings and other configuration files that might be used by a variety of services or components that your program works with.
Clicking on the menu File > New > New Project will open the New Project window where you can choose from the following project types:
- Console project:
- Console App
- Library project:
- Class Library
- ASP.NET Core Class Library
- Web project:
- ASP.NET Core Web API
- Test project:
- xUnit Test project
- Desktop project:
- Shared Project
Project File
Each C# project contains a project file identifiable from the file extension (.csproj). The project file is an XML file that contains all the information needed to build a project.
Solution
A solution is a container for projects that are related in some way. It contains build information, SnapDevelop window settings, and any miscellaneous files that are not directly related to a particular project. A solution is defined by a text file with the .sln extension that integrates projects, project items and solution items in the solution.
Solution Explorer
After you create a new project, you can use Solution Explorer to view and manage the project and solution and their related items.
A variety of menu commands, such as building/rebuilding a project, managing/restoring NuGet packages, adding a reference, renaming a file and adding Docker Support, are offered by right-clicking on various items in Solution Explorer.
The toolbar at the top of Solution Explorer currently provides 6 options:
- Refresh Status button, which refreshes the status of code management of all the nodes inside Solution Explorer.
- Sync with Active Document button, which allows you to locate the currently open file in the code editor inside Solution Explorer.
- Collapse All button, which collapses all nodes in Solution Explorer.
- Show All Files button, which shows all files including the hidden files in the selected project.
- Properties button, which allows you to set the various properties of the project/solution.
- Preview Selected Items button, which previews a selected file or component with one click in the code editor.
Creating solutions and projects
Creating a new project
To create a new project, take the following steps:
Select New > New Project... from the File menu.
The New Project page will pop up.
On the New Project page, select a project template from the available project templates options.
Specify a name for the project, select where you want the project to be created, and specify the solution name.
The fields required in each template are different. Please follow the guidance in the screen to specify all the necessary information, and then click Next to go to the next screen, until you see the Create button in the screen.
Click Create to generate the project. The project is then opened by SnapDevelop so that you can start to develop.
Setting a startup project
You can view your solutions and projects in the Solution Explorer tool window, in which you will see that a project is marked in bold. Such a project is called the startup project, which refers to the project that launches when you run the application.
The startup project can be changed to another by right-clicking on the node of the intended project and then selecting Set as Startup Project. For a library project, you must add an executable project to the solution and set the executable project as the startup project.
Adding items to a solution
As described earlier, a project contains all items required to build an application in SnapDevelop. When you create a new project, SnapDevelop creates a solution to accommodate the project. When the solution is created, you can also add existing and/or new projects/items to the solution. The newly added items do not necessarily belong to any individual project.
Sometimes, multiple projects in your solution share a file, such as the readme file for the solution, and some files belong at the solution level instead of the project level. In such cases, you can add the files to the solution itself. To do so, right-click on the solution node in Solution Explorer and then select Add > New Item, or Add > Existing Item.
Deleting a solution, project or item
You can use the SnapDevelop IDE or File Explorer to delete a solution, project or item. The difference is that deleting items within the SnapDevelop IDE only removes them from the current project or solution while deleting items in File Explorer removes them permanently. Before you delete items in File Explorer, however, it is recommended that you back up the items in case you need to use them later.
To delete an item within the SnapDevelop IDE, select the item and then select Delete from the right-click context menu.
To permanently remove an item,
- Right-click on the node of the solution/project/item you want to delete in Solution Explorer and then select Open Folder in File Explorer.
- Navigate up one level in File Explorer.
- Select the folder that contains the solution and then press the Delete key.
Project properties and solution properties
When you right-click on a project (or solution) in Solution Explorer and select Properties, the Properties Settings window will pop up. You can configure properties about the project (or solution) in this window. For a detailed description of each property, please refer to Configuring project properties.
Managing project references
Three available referencing methods
SnapDevelop currently supports three referencing methods, which are:
- Project-to-project Reference
- Shared Project Reference
- COM Reference
- File Reference
Adding or removing references using the reference manager
To add a reference, right-click on the project node and then select Add > Reference, or right-click on the Dependencies node in Solution Explorer and then select Add Project Reference.
The Reference Manager dialog box appears and lists the available references by group.
In the Reference Manager dialog box, there are four tabs:
- Projects, which lists all compatible projects within the current solution.
- Shared Projects, which allows you to write common code that can be referenced by various application projects.
- COM, which lists all COM components available for reference.
- Browse, which allows you to browse for a file in the file system. This tab contains the Recent node, which lists the components newly added to projects.
Note that there is a search bar in the Reference Manager dialog box that can be used to filter items under the tab that is in focus.
Troubleshooting reference-related exceptions
If your application cannot find a referenced component, an exception will be thrown. There are several scenarios in which an exception will be thrown.
- Network failure;
- The file being referenced has been deleted;
- The file being referenced has been renamed.
If any such exception occurs, you need to handle it in order to use the references properly.
Network failure
Fix the network connection problems until you have access to the required resources.
The file being referenced has been deleted
Delete the reference, or read it from another location if the reference exists elsewhere on your computer.
The file being referenced has been renamed
Delete the reference, and then add a reference to the renamed file.
Application settings
Managing application settings
Application settings allow you to save various application-related information, such as application URL, launch URL, and database connection strings.
The project system automatically stores application settings into two JSON files (launchSettings.json and appsettings.json) when you create an application in SnapDevelop.
launchSettings.json
In the file:
- applicationURL: The URL where you can launch any application on your local computer from the browser.
- launchBrowser: Whether to launch the browser when you run your application.
- launchURL: The URL to launch on your local computer.
- environmentVariables: Variables that ASP.NET Core reads at app startup and that ASP.NET Core uses to configure app behaviors.
appsettings.json
In the file:
- LogLevel: Indicates the minimum level to log for selected categories.
- AllowedHosts: The hosts headers that are allowed to access this site.
- ConnectionStrings: Strings that specify information about a data source and the way of accessing it.
Note: There are two configuration files that manage the Web app at runtime. The default application configuration is stored in the appsettings.json file. However, you can use the appsettings.Development.json file to override these settings.
Managing database connections
To create a database connection:
Select Connect to Database... from the Tools menu.
In the Choose Connection dialog, click New to configure the database connection. You can directly use the existing connection by selecting it from the list of Connections, or click Edit to modify the settings.
Select a data source from the Data Provider list.
Configure the basic and advanced connection settings for the selected data source.
Refer to Appendix D: Database Connection Settings for the settings to configure for each data source.
Click Test Connection to check the connection state.
Check the connection string in the Choose Connection dialog to make sure the properties are configured as expected. You may also copy the connection string to use it elsewhere.
Targeting a framework
SnapDevelop allows you to specify the .NET version that you want your project to target. If a .NET application is to run on another computer, it is necessary that the .NET version that the application targets should be compatible with the .NET version installed on the computer.
SnapDevelop supports long-term supported .NET versions as well as the latest .NET versions.
A SnapDevelop solution can contain projects that target different .NET versions.
Changing the target framework
When you create a .NET Core project, you have no option to select the target .NET Core version. The latest .NET Core version is selected by default. However, you can change the target .NET Core version if you want to.
To change the target framework:
- Select the project you want to change in Solution Explorer, and then select Properties from the right-click context menu.
- In the Properties window, select the Application tab.
- In the Target framework list, select the intended version.
Managing project and item templates
A variety of predefined project and item templates are automatically installed when you install SnapDevelop. These templates provide a basic set of pre-generated code and structure based on which developers just need to add some other code and resources to the project/item, thus reducing a lot of repetitive work during development.
SnapDevelop currently provides the following project templates:
- Console App
- Class Library
- ASP.NET Core Class Library
- ASP.NET Core Web API
- xUnit Test project
- Shared Project
and the following item templates:
- Class
- Enum
- Interface
- DataContext (SnapObjects)
- Application Configuration
- Application Manifest File (Windows only)
- Assembly Information File
- Resources File
- Text File
- JSON File
- XML File
When you create a new project/item, all project or item templates, including the default templates and custom ones, share the following items:
- The files to be created when the template is used. These files include source code files, embedded resources, project files, etc.
- A .template file, which contains the metadata needed to create a project or item from the template and to display the template in the New Project and New Item dialog boxes.
When these files are compressed into a .zip file and put in the correct folder:
Project templates appear in the New Project dialog box.
Item templates appear in the New Item dialog box.
Managing NuGet packages
The NuGet Package Manager in SnapDevelop allows you to install, uninstall, and update NuGet packages for your projects and solutions. It contains the following tabs, and each tab displays a list of related packages on the left side of the manager and details about the selected package on the right side.
- Browse, which displays packages to install. If a package is already installed, the Install button on the right side changes to Uninstall.
- Installed, which displays all installed and loaded packages. A green down arrow next to a package indicates that the package is already loaded. The red X icon to the right of a package or the Uninstall button on the right side of the manager can be used to uninstall the package. A blue up arrow to the right of a package can be used to update the package if a newer version of the installed package is available.
- Updates, which displays packages that have available updates from the currently selected package source.
- Consolidate, which displays packages that have been installed in multiple versions. You can consolidate the different versions to the one you specified.
The NuGet Package Manager remembers the following settings you configure for a single project or an entire solution, and restores your settings next time you open the NuGet Package Manager on the project or solution:
- Package source
- Include prerelease
- Show preview window
Managing packages for a single project
Finding and installing a package
In Solution Explorer, select either a project node or Dependencies node and then select Manage NuGet Packages... from the right-click context menu.
Select the Browse tab to display packages from the currently enabled package source. Search for the desired package using the search box on the upper left side of the package manager and select the package to view its detailed information.
Note
To include prerelease versions in the search, and to make prerelease versions available in the Version drop-down, select the Include prerelease option.
Select the desired version from the Version drop-down and select Install. SnapDevelop installs the package and its dependencies into the project. You may be asked to accept license terms. When installation is complete, the installed package appears under the Installed tab and in the Dependencies node in Solution Explorer, indicating that you can use the using directives to refer to them in your project.
Uninstalling a package
In Solution Explorer, select either the desired project node or Dependencies node and then select Manage NuGet Packages from the right-click context menu.
Select the Installed tab to display all installed packages.
Select the package to uninstall and select Uninstall.
Updating a package
In Solution Explorer, select either the desired project node or Dependencies node, and then select Manage NuGet Packages from the right-click context menu.
Select the Update tab to display packages that have available updates from the enabled package source. Select Include prerelease to include prerelease packages in the update list.
Select the package to update, select the desired version from the Version drop-down list, and select Update.
To update multiple packages to their latest versions, select them individually in the list or enable the Select all packages option and select the Update button above the list.
Managing packages for solution
If you want to manage NuGet packages for multiple projects simultaneously, it is recommended that you manage packages for the entire solution. Compared with the package manager for single projects, the package manager for solution has a Consolidate tab, in addition to the three tabs.
To manage packages for solution, you:
Select the solution and Manage NuGet Packages for Solution from the right-click context menu. Alternatively, select Tools > NuGet Package Manager > Manage NuGet Packages for Solution...
When you are managing packages for the entire solution, you are required to select the affected project(s).
Consolidating package versions
It is uncommon to use different versions of the same NuGet package across different projects in the same solution. When you manage NuGet packages for solution, you will see a Consolidate tab, which allows you to easily see where packages with distinct version numbers are used by different projects in the solution.
To consolidate package versions, you:
Select the project(s) to update in the project list.
Select the version to use in all those projects from the Version dropdown.
Select Install.
The Package Manager installs the selected package version into all selected projects. Then, the package disappears from the list under the Consolidate tab.
Specifying package sources
To change the source from which SnapDevelop obtains packages, select the desired one from the Package source dropdown list:
For information on how to manage package sources, refer to Package Sources.
Understanding the package manager options
When you select a package, you will see an expandable Options control below the version selector. For some project types (for example, .NET Core projects), only the Show preview window option is provided.
Showing preview window
If you enable the Show preview window option, a window shows the dependencies of a selected package before the package is installed.
Install and update options
Dependency behavior specifies how NuGet selects the version of dependency packages to install.
Option | Description |
---|---|
Ignore Dependencies | Does not install any dependencies. |
Lowest | Installs the dependency with the minimum version number that meets the requirements of the primary selected package. It is the default option. |
Highest Patch | Installs the version with the same major and minor version numbers but the highest patch number. If version 3.2.2 is specified, for instance, the highest version starting with 3.2 will be installed. |
Highest Minor | Installs the version with the same major version number but the highest minor number and patch number. If version 3.2.2 is specified, for instance, the highest version starting with 3 will be installed. |
Highest | Installs the highest available version of the package. |
File conflict action specifies how NuGet deals with packages existing in the project or local machine.
Option | Description |
---|---|
Prompt | Requires NuGet to ask whether to retain or overwrite existing packages. |
Ignore All | Requires NuGet to skip overwriting any existing packages. |
Overwrite All | Requires NuGet to overwrite any existing packages. |
Uninstall options
Option | Description |
---|---|
Remove dependencies | Removes any dependency packages not referenced elsewhere in the project. |
Force uninstall, even if there are dependencies on it | Uninstalls a package even if it is still being referenced in the project. This option is often used together with Remove dependencies to remove a package. However, applying this option may lead to broken references in the project. |