Visual Studio .sln/.csproj Templates

To ease setting up a Visual Studio project, templates can be created.

There are multiple ways to do this:

  • VSIX Project Templates, by default shipped with Visual Studio.

  • The new .NET CLI (Command-Line-Interface) way, shipped with .NET Core. (Visual Studio GUI not available yet)

Because the first way, using the VSIX, has poor support for multi-project solutions and is a lot of work in comparison to the .NET CLI solution, the .NET CLI has been used.

This also has the consequence that the template will not be listed in Visual Studio, but only in the command line.

Step 1: Obtaining the templates.
Gather the templates in your favorite source control/version management system.

The templates can be found in the following repository:

https://github.com/wolfpackdcs/Templates

Currently there exist 2 templates:

  • Listener Template

  • Poller Template.

Note; In case you start the template projects/solutions to check them out, make sure that the following folders are deleted/gone before proceeding with the following steps:

  • packages (containing the NuGet packages)

  • all bin and obj folders.

The folders can sometime cause issues because of the renaming that happens when going from the template to an acutal project/solution. It's also possible to delete them after the project/solution has been created. First one works more often.

Note2: Because the following steps reference the source code of the template, the templates are dynamic, meaning that when someone changes the template and you get the new code for the template, you should not have to re-install the template.

Step 2: Installing the template using the .NET CLI
Open command prompt, in Administrator mode.

Run the following command to show a listing of the current templates:

List Templates Command
dotnet new -l

This will list all the templates currently installed, example:

As an example the Poller template will be installed. To install we need the following command, (which is dependent on where you put the repository):

Install Template Command
dotnet new -i [RootTemplateFolder]

The RootTemplateFolder is the folder containing the .template.config folder. This folder contains the configuration to make the templating work.

So in my case I have put the repository in E:\Code\Templates.

The root folder for the Poller Template in this case is : E:\Code\Templates\Orp.Core.Poller.Template.



So in my case, to install the template, the following command needs to be executed:

Install Template Command
dotnet new -i E:\Code\Templates\Orp.Core.Poller.Template

Giving as result the installed template like this:(in case this isn't shown after running the command, it's due to a setting of the CLI, use the command to list the templates as described above)

Step 3: From template to an application.
To create an application from the template the following command needs to be executed:

Create Application Command

The template short name is a column in the listing of the templates (see above picture). For the example this is: "wolfpack-poller-template".

The directory is the root directory in which the .sln and corresponding projects will be placed.

The name is the name of the project. (and prefix of any other projects, such as PREFIX.UnitTests etc)

Example: we want to create a Product Poller for Aspos. (SL/Valk Solution ERP), then the command will become:

Create Application Command

The result application:

Solution explorer:

Quite often there will be errors/warnings. Clean the solution and rebuild it. This should fix all references and dependencies. If not, there might be an issue with the template and that needs to be fixed .

For the Listener template, the procedure is the same, the paths are different.

Uninstalling a template:
In case you don't want the template after it has been installed, use the following command:

Uninstall Template Command:

With reference to the example this would become:

Uninstall Template Command

Fixed Issues In Templates:
Like always, there were issues creating these projects:

  • Slowcheetah JSON transforms only support JSON objects, not arrays. So it was not really suitable for TenantConfiguration files. As a solution TenantConfigurations were generalized to return T, instead of List<T>. (EncryptedConfigurationProvider<T> in code). The TenantConfiguration.json now contains an object, containing a list.

  • SlowCheetah sometimes does not have a build target. Gives some weird error, solution from here is used; https://github.com/Microsoft/slow-cheetah/issues/147

Sources:
Installing custom templates using dotnet new: https://docs.microsoft.com/nl-nl/dotnet/core/tools/custom-templates
Template samples from the dotnet/templating repository: https://github.com/dotnet/dotnet-template-samples



copyright wolfpack DCS b.v. 2020