How to Create a Visual Studio Template for Your Microservices Architecture
30 July 201830 July 2018 | Software & Technology
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.
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.
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
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.
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.Zip 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.
Import the zip file you just ‘fixed’ and include it in this solution.
Right click on the zip file, go to Properties and under Build Action, select Copy to Output Directory and then Copy always.
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.
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:
Source: File on filesystem
Path: your zip file (that will be prompted to you by the IDE)
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.
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.
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: NET
IrinaSoftware architect, 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.