Publishing Your First .NET Core Application to Azure

Today’s header image was created by Little Visuals, the original source for the image is available here
Previously I’ve covered setting up an Azure account (although this was specifically related to creating a VM so that you can run Visual Studio 2017 in the cloud).
it’s a bit of prerequisite for this post, so I’d recommend giving it a read
Today we’re going to publish a WebApi application to an Azure account using our existing subscription, and get our application out there in the wild for people to use.
Exciting, eh?
If you want to jump straight to the juicy goodness of publishing to Azure form Visual Studio, then click this link which should take you directly to that section. But first a little on Azure and cloud based VMs.
Azure Hosted Application Basics
The first thing that you need to know is that all Azure applications run on virtual machines.
Just like virtually all web applications
How powerful your Azure MV is depends on how much you’re willing to pay. Azure is a paid service, with a free tier – for hosted applications anyway.
That being said paying for performance with Azure shouldn’t break the bank, which was the topic of a talk given by Troy Hunt at Microsoft Ignite Australia.
On the back of that, I’d like to point out something that Rob Conery mentions in his excellent book “The Imposter’s Handbook”:
Most applications generate little to moderate amounts of data, depending on what needs to be tracked… Developers often over estimate how much data their application will generate.
The Impostor’s Handbook by Rob Conery (section 11.4 – “Big Data”)
As a side note: if you’re self taught or feel that there are gaps in you’re programming knowledge, I would recommend getting a copy of The Imposter’s Handbook. It’s incredibly useful, and I swear that you’ll learn something new in each chapter.
Another useful quote is this, which is what Ken Seguin, in The Little Redis Book, has this to say about server memory:
I do feel that some developers have lost touch with how little space data can take. The Complete Works of William Shakespeare takes roughly 5.5MB of storage
The Little Redis Book by Ken Seguin, page 8
Rob Conery advocates for selecting just enough power and storage space for a VM as is required and upgrading when necessary, especially when you need to shard the database.
Sharding is the process of splitting the load of a database across multiple VM instances
Hosting With Azure
In this post, I’m going to focus on using the free tier of Azure’s application hosting. Mainly because I need to host my demo application somewhere, and I might as well do it for free.
For now at least.
It doesn’t have to be ridiculously performant, and will have a very small memory and processor footprint.
But first, lets take a look at the default tiers when you add a new App Service to an Azure subscription.
an App Service is what our WebApi application will run on

But just under those default tiers are two of the cheaper ones. For our demo application, these will be perfect.

We’re not going to use the Azure portal to set up an app service plan, though. We’ll use Visual Studio 2017 to do it for us and publish time using the built in Azure tools.
Application
The first thing we’re going to need is an application to host on Azure.
I’m going to use my dwCheckApi application
The source for which can be found here in case you wanted to use is as you follow along
but you can use anything that you want. The default WebApi project would work perfectly well, it wouldn’t do anything fantastic but it would work really well.
It’s completely up to you.
Caveat
If you’re going to use dwCheckApi you need to know three things:
- It has an MIT license
You can read about the MIT license at tl;dr legal or at choose a license
- It’s incomplete (at the time of writing this blog post)
- You’ll need to make a change to the code to get it to work
The change that you’ll have to make is a trivial one, but if you don’t make the change then the application will be unusable.
In the Configure method (of the startup.cs file) you’ll find this:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) | |
{ | |
loggerFactory.AddConsole(Configuration.GetSection("Logging")); | |
loggerFactory.AddDebug(); | |
app.UseMvc(); | |
// Commented out because each time dotnet run is issued, | |
// this is ignore. Is dotnet run building in release mode? | |
//if (env.IsDevelopment()) | |
//{ | |
// seed the database using an extension method | |
using (var serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope()) | |
{ | |
var context = serviceScope.ServiceProvider.GetService<DwContext>(); | |
//context.Database.Migrate(); | |
context.EnsureSeedData(); | |
} | |
//} | |
} |
The commented out line (at line 16) will need to be uncommented.
I had commented it out, because when I built the application as part of the [WebApiTutorial] series, we were building the database using the Entity Framework Core CLI commands. We wont have access to the CLI on our Azure hosted app service, so we’ll have to build the database in code; which is what the commented out line does.
Once you’ve uncommented the particular line, the Configure method should look like this:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) | |
{ | |
loggerFactory.AddConsole(Configuration.GetSection("Logging")); | |
loggerFactory.AddDebug(); | |
app.UseMvc(); | |
// Commented out because each time dotnet run is issued, | |
// this is ignore. Is dotnet run building in release mode? | |
//if (env.IsDevelopment()) | |
//{ | |
// seed the database using an extension method | |
using (var serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope()) | |
{ | |
var context = serviceScope.ServiceProvider.GetService<DwContext>(); | |
context.Database.Migrate(); | |
context.EnsureSeedData(); | |
} | |
//} | |
} |
Publishing to Azure
Once you have your project ready to go (whether it’s my dwCheckApi or a project of your own), right click on the project (not the solution) in the Solution Explorer and click publish.

You’ll then be presented with the publishing wizard:
Here’s why we didn’t create the app service in the Azure portal earlier: we’re going to set one up now.
If we wanted to publish to our own server, or to use FTP, we could chose one of the other options and Visual Studio will publish locally as it used to.
We want to publish to an Azure app service, so make sure that’s selected, and that the Create New radio button is selected then click Publish.
You will have to sign into Azure at this point, and will be taken to the Create app service wizard once you have signed in.
I’ve chosen names for the app service and service plan which make sense to me, and you should pick one that makes sense for your application.
You’ll need to select your resource group, or create a new one here too
Before we can create our app service, we need to add a new service plan. Click the “New” button next to the Service Plan drop down.
Give your service plan a useful name, leave the location as it is, but
and here’s the important thing
in the “size” drop down, select “Free”
everyone loves free stuff, right?
Obviously, if the free tier doesn’t give you the performance that you want (or if you want a custom domain name and SSL), then you’ll need to pick a different service plan. But the other service plans have a cost associated with them.

Clicking ok on the New Service Plan wizard will pass you back to the Create App Service wizard, automatically choosing your new service plan. Clicking on the Create button on the Create App Service Wizard will create the app service and take you back to Visual Studio.
Creating the app might take a few moments, though.
Now Visual Studio will build, package and publish your application.
We can see all sorts of metadata about our app service and the url to access it. Not just on the build, package and process progress
try saying that quickly three times
Once the publish process has completed, you should be able to access your new WebApi application by heading to the Site URL (shown on the publish screen in Visual Studio).
It might take a few seconds for your app to come online, but once it does you’ll be able to access it just like any other web resource.

Stats
Everyone loves stats, right?
Once your app service (and the application that you’ve pushed up) is online, head back to the Azure portal in your browser and take a look at your dashboard.

If you click through to the App Service
that’s the first item in the list, in my screen shot
you’ll see all sorts of start about it. These stats can really help you to figure out whether the hosting tier that your application is on is the right one for you.

Conclusion
Publishing .NET Core applications to Azure is extremely easy, and only takes a few clicks.
I’m starting to feel a little like I’m pushing Azure on you all at this point, but I’m just trying to point out how easy it is.
Once you’ve got your app online, you can see a whole bunch of stats about it, too.
Give it a try with one of your .NET Core apps and leave a comment letting me know how it went.