How to enable HTTP/2 on an Azure App Service web app

Azure app service uses HTTP/1.1 by default. HTTP/2 brings a number of performance benefits and thankfully switching your app over is easy.

First, open up developer tools and hit your app to see your HTTP/1.1 baseline…

HTTP1

then in the Azure portal navigate to your App Service instance and visit the Configuration page. The HTTP version setting is available on the General settings tab as shown below…

Don’t forget to click Save after making the change. The protocol might not changeover immediately but after a couple of minutes you should see h2 in the Network tab in Chrome/Edge developer tools…

Note… to use HTTP/2 you need to use SSL. Azure has a wildcard certificate bound to *.azurewebsites.net, but if you’re using a custom domain you’ll need to get a cert.

How to have private constructors and private setters when using Entity Framework Core Database first

Having private constructors and private setters is one of the primary ways we can enforce encapsulation in our domain model.

Entity Framework Core can work fine with both private constructors and private setters. Developers using the code first approach can just set the access modifiers on their constructors and properties as they wish.

BUT…

To achieve something similar with the DB first workflow we need to customise the code that EF generates during the scaffolding process.

Thankfully this is easily done using the EF Core Power Tools Visual Studio extension. This extension uses the Scaffolding.Handlebars library to support the customization of code which is generated during the scaffolding process. You don’t need the extension to use the library but it’s a bit easier and EF Core Power Tools is a great extension anyway so I’ll use it in the post.

Customising Entity Framework scaffolded models

The steps are…

Install the extension via the Extensions -> Manage Extensions window.

Right click on the relevant project and select EF Core Power Tools -> Reverse Engineer and first select which tables you want to scaffold and then on the subsequent screen check the option for customizing the code…

Customise EF Code with EF Core Power Tools

After running the scaffolding you should see entities generated AND a CodeTemplates folder which contains all the handlebar template files for customizing entities and the DbContext…

Code templates folder

The two files you need to edit are highlighted above.

Your edits will look similar to below…

Private constructor

Extending Entity Framework generated models with partial classes

After changing the above files and scaffolding again all your entities should now have private constructors and private property setters. To add public constructors and public methods to your entities simply create partial classes for each entity.

Always use partial classes to extend your EF generated models. Never edit the generated models directly as you will lose your customisations each time you re-scaffold.

For example here’s a public constructor for creating an Address instance…

while here’s a public method which will allow us to update the properties of an address instance..

Now we can see that the only way to create an address is via the non default constructor. This means we can ensure that an invalid object is never created.

Create an address

Similarly when we want to update an address we cannot set properties individually as all the setters are private… we must go through the one centralised UpdateAddress method

Update an address

Summary

You’ll always get better DDD support with the Entity Framework code first workflow. For those of us who prefer the DB first approach combining the customisation of EF generated code and partial classes is a nice technique to move a little closer to a DDD based app.

How to store code snippets in Visual Studio for re-use later

Storing code snippets in the Visual Studio Toolbox is really useful when we are preparing for a demo or tutorial. We can add snippets beforehand and then during our demo we can just double click or drag them onto the file we are editing.

Save snippets into the Toolbox

First, open the toolbox with the CTRL+ALT+X shortcut or from the View -> Toolbox menu item.

To keep things organised, right click on the toolbox and click ‘Add Tab’ and enter an appropriate name depending on what items you want to store in the tab.

To add code to your tab simply select the code block and drag it across into your tab.

In the below example I’ve added a tab called ‘ENTITY FRAMEWORK DEMO’ and have added two code snippets…

Visual Studio Code Snippets

If we right click on a snippet we have the option to rename it to something more helpful…

Code snippets can be renamed

 

How to launch a web app in multiple browsers from Visual Studio at the same time

In Visual Studio to launch your web app in multiple browsers simply set multiple defaults in the ‘Browse With…’ screen as shown below. For this to work you’ll need to start without debugging (CTRL+F5).

This is helpful when you want to do a cross-browser check.

How to launch app in multiple browsers in Visual Studio

How to scaffold multiple ASP.NET CORE MVC controllers at once using a bat file

Previously I wrote about how we can customise the templates Visual Studio uses when it scaffolds a controller.

After changing a template we can re-scaffold our controllers one by one from the GUI provided by Visual Studio…

Scaffold a controller in Visual Studio

… BUT this can be very time consuming if we want to re-scaffold a lot of controllers each time we make a change to any of the controller or view templates.

A better way is to trigger the scaffolding process for multiple controllers at once by wrapping calls to the asp.net core scaffolding engine in a bat file.

In the example below (click for a larger view in a new window) I’m scaffolding three different controllers…

Scaffolding via the command lineI’ve included a call to install the aspnet-codegenerator global tool which we need to trigger scaffolding at the top of the file. You don’t have to include this, but I like to keep it there just to make things easier for the next developer.

The command that I’ve used to scaffold the CustomersController is…

dotnet aspnet-codegenerator controller – controllerName CustomersController – model Customer – dataContext EFContext – relativeFolderPath Controllers – force – useDefaultLayout – referenceScriptLibraries – layout "~/Views/Shared/_Layout.cshtml"

The flags I’ve set match the defaults which are set when we create a scaffolded controller through the GUI. Note the inclusion of the force flag which when included means existing files will be overwritten.

Since I’ve set the relativeFolderPath flag to ‘Controllers’ this means I need to save the .bat file in the root of my project.

If you’re copying and pasting from above, note WordPress has messed up the dashes. Each flag should have two dashes in front of it… see the image example above.

Running the scaffolding bat file from within Visual Studio

We can run the bat file by double clicking it in the file system but to run it from within Visual Studio we can install the Open Command Line extension. After installing the extension we can right click the .bat file and select ‘Execute File’…

Execute File

and we’ll see the CMD window pop up with output similar to below…controller-scaffold-batfile