How to Create a Visual Studio Template for Your Microservices Architecture

Creating a microservice architecture for our .NET development projects might seem complicated at first, but there are a lot of tools out there developed to make our work easier. One of them is provided by Microsoft, through Visual Studio. This tool offers us the possibility to create a custom Visual Studio Template that will maintain a certain degree of consistency across our team/teams and in our projects.

 

The Importance of Visual Studio Templates in a Microservices Architecture

In any project, there comes a time when you must do repetitive things. This is especially true when you have a microservice architecture in place because it requires creating multiple small and independent projects that will play a microservice role for you.

At its core, a microservice architecture is more code-oriented than SOA services. Breaking your code into separate services and libraries doesn’t automatically make your app part of a microservice architecture, but for sure this is the first step to get there.

Some might say that it brings you overhead, or that the gains are not so valuable compared to the drawbacks. But I know for sure you need to change your mindset a little bit and step out of your monolithic comfort.

When you want to step into the microservices world and you have a big team, you’ll often encounter different types of approaches when writing code or naming or even in setting up a new project infrastructure.

This is not a bad thing at all. But I think having your team brainstorm a uniform approach in the project’s creation phase will create a common look and feel for all future microservices and save you a few time ticks at certain points.

For every project you’ll make using this type of infrastructure, you’ll want almost the same things – same packages, and maybe a certain configuration and folder structure.

This is where the Visual Studio template steps in. Creating a custom template based on your project needs will enable you and your team to use the same structure in all your projects.

If you would like to read more about the features of Visual Studio 2017, I invite you to read my article Visual Studio 2017 and ASP.NET Core MVC.

 

Creating a Visual Studio Template

To create a Visual Studio template, first, you’ll need to install the Extensibility Tool extension. This will allow you to create VSIX projects, Item templates, and Project templates.

Install the Extensibility Tool Extension

Then, create a new project that will be your template base. Install the NuGet packages you need and the folders and files that represent your infrastructure.

For example, if you use Dapper as a Micro ORM, you’ll want to log in File and ElasticSearch. You’ll also want to configure it and, because you are developing an API, you’ll want to document it as nicely and easily as possible.

Maybe you’ll need a hosting.json file to be able to map your API to a specific port so that you’ll have more control in a hosted environment. Let’s say your template base will look like the one in the image below.Visual Studio Template Base

Once you’re done, decide if this infrastructure suits your needs. If it does, you’re ready to export this as a template.

 

Exporting Your Template Base

Export Visual Studio Template

To do that, you must go to the Project menu in Visual Studio and click on Export Template.Choose Template Type in Visual Studio

Because you will want to be able to use your project as a future template for Visual Studio, select the “Project template” choice instead of the “Item template” one.

Then the Wizard will allow you to select a Template Name (beware, this name will appear in Visual Studio), a description, and if you want, a representative icon.

Select Template OptionsWhen you click finish, this export will be saved in C:UsersusernameDocumentsVisual Studio 2017My Exported Templates as a zip file.

Then the hard work begins because, right now, Visual Studio contains a bug that you’ll need to overpass.

Close Visual Studio and extract the archive you just exported. Next, edit the MyTemplate.vstemplate manifest to add the <CreateInPlace>true</CreateInPlace> node. CreateInPlace NodeZip the whole project and move to the next step (let’s hope this hack won’t be necessary soon).

 

Creating the VSIX Extension

Under the Extensibility node, you’ll find the VSIX project. Select it and create a new project.

VSIX Project

Import the zip file you just ‘fixed’ and include it in this solution.

Import the Zip File

Right click on the zip file, go to Properties and under Build Action, select Copy to Output Directory and then Copy always.

File Properties

If you think this is the final step, you are unfortunately wrong. Next, you need to create the general properties of the final VSIX file.

For this, double-click the source.extension.vsixmanifest file under Solution to edit the metadata and assets.

 

Editing Metadata of the VSIX File

To edit the metadata of your VSIX file, you must click on the Metadata tab and add the desired details in the available input sections.

VSIX Metadata

 

Editing the Assets of the VSIX File

The asset section represents the most important part of this process because here you’ll basically indicate the resources used for your installable extension.

Add new Asset:

Type: Microsoft.VisualStudio.ProjectTemplate

Source: File on filesystem

Path: your zip file (that will be prompted to you by the IDE)

Add New Asset - VSIX File

Click OK, build the project and you’re done.

As a build artifact, under bin/debug folder, you’ll see the final, installable .vsix file that can be distributed inside your organization or team.

Installable VSIX FileVSIX InstallerInstall it as you would install any extension from the Visual Studio marketplace and you’ll be able to create new projects based on your template.

Visual Studio New Project

This extension will install a new type of project in your Visual Studio, containing all the bits and pieces you added in your template.

When you use it to create a new project, you’ll see that everything you’ve set up is there: the folder structure, the controllers you added, the files, and the most important part the NuGet packages you need.Custom Visual Studio Template

We often forget that Visual Studio can be extended and that those extensions can prove to be more than useful for many of us. We all have the tendency to use only the extensions that are already there, created by others. How about creating cool extensions that others can use from us, even if we are talking only about our own team, colleagues or organization?

Topics:
Senior Software Developer

Irina

Senior software developer, Microsoft Certified Trainer and MCSD for Web and Application Lifecycle Management. Above all, passionate about leadership and personal development, as well as creating learning contexts for her team.