Using the Azure File Service in your Cloud Services (Web and Worker Roles)

Last month at TechEd Microsoft announced the public preview of the Azure File Service (or Azure Files, or DFS-as-a-Service). This new feature allows us to access a Storage Account using the SMB protocol which you can access your Storage Account as if it were aDFS or a share.

The storage team recently blogged about some basic guidance to use the File Service in Virtual Machines and in Web/Worker Roles. In this article we'll be looking in detail at how you can use the File Service in your Web and Worker Roles.

Before we start

Keep in mind that this is still a preview feature, so in order to use the new File Service you'll need to sign up for the preview on the Preview features page:

Once the preview feature is activated go ahead and create a new Storage Account. After the account is created you'll see an additional endpoint showing up (*.file.core.windows.net):

Mounting Shares

The first thing you'll need to do after you account has been created is to create a share (CloudFileShare). A share is a top level entity in the File Service. You could for example have a reports share in which your application will be saving reports generated by users. In this share you'll be able to create directories, upload files, … But the most important part about this is that you can mount a share as a mapped drive, allowing you to access the data from multiple Web/Worker Role Instances and Virtual Machines. If you want to use this from Web Sites or on-premises (which cannot use the SMB feature at the moment), you do it through the REST API (or use the Storage SDK, PowerShell, …).

Let's start by creating the reports share (you will need to update the Storage SDK to version 4.0.0 or higher):

Now that our reports share exists we'll be able to mount it as a mapped drive and upload/download files, create directories, … In order to do this from code we'll use some P/Invoke magic to create or remove a mapped drive. You might be wondering why we're not using a startup task for this, but things will get clear when we start to talk about the context of a mapped drive.

The context is king

Here's the challenging part, a mapped drive always exists in the context of a specific user. And on a machine a user could have one or more contexts in which mapped drives are defined. An example why a user could have more than one context is when the user is running some tools in limited mode and other tools in elevated mode (eg: running cmd.exe normally and right-clicking cmd.exe and choosing "Run as administrator").

Because of this we'll need to take a closer look at how this will work when deploying Web Roles that use the File Service. Because when your Web Role is deployed your code runs in 2 processes: WaIISHost.exe (the code in your WebRole.cs, when the instance starts) and the w3wp.exe process (which runs your web application).

When exactly will our context be the same for 2 processes and when will it be different? By default, the context will be the same because for both your WebRole.cs and your web application the current user will be something like RD0003FF412670$ (a computer account).

So this means we'll be able to mount the mapped drive in the OnStart method of our WebRole.cs:

Can you imagine how powerful this is? You get the scalability, reliability, size, speed, … of Azure Storage while using the traditional System.IO API (FileStream, FileInfo, DirectoryInfo…). This is also a advantage for applications that should run both on-premises and in the cloud. Or consider legacy applications that you want to migrate to Cloud Services. Even if they (or a third party component) really depend on the local file system you'll be able to easily lift-and-shift them to Cloud Services thanks to these new features.

In the past the Cloud Drive (or XDrive) was also a solution for this problem, but the disadvantage was that a disk could only be mounted to 1 instances, while the File Service can be used to mount a mapped drive on multiple instances. This also applies to Data Disks in Virtual Machines, so even there it would be an advantage to use the File Service.

But let's get back to the context part. There will be times that your WebRole.cs and your web application could run in different contexts:

The executionContext of your role is set to elevated. In that case the code in your WebRole.cs will be running under the SYSTEM account, while your web application will remain under the computer account.

You change the identity of the application pool of your Web Application

If we run the same code in the OnStart of our WebRole.cs, the following will happen:

Since our WebRole.cs runs under the SYSTEM context, the mapped drive will only be available in this context. Our web application will not be able to see the mapped drive or access it. In that case, we'll also need to mount the mapped drive when the web application starts, if we want the share to be available in our web application (eg: being able to upload files, download files, list files, …).

In addition to mapping the drive in the OnStart of my WebRole.cs this means I'll need to setup the mapping when the application starts, in the Application_Start of my MvcApplication for example.

And that's it. If you want your Worker Role to also use the share you can add the same logic in the OnStart method of your Worker Role. Oh and this will also work for your applications running in Virtual Machines (not Web Sites).

Introducing RedDog.Storage

The code samples in this article all mention the P/Invoke signatures and the native calls to mpr.dll required to create mapped drives. To save time and reduce the friction of setting up the P/Invoke calls I created a repository on GitHub (https://github.com/sandrinodimattia/RedDog) that contains all the code you need to get started with the Azure File Service. The goal of this repository is to have somewhere I can put all my helpers, extension methods, tools, … This will make it easier for me to use my own tools in current and future Azure projects, but hopefully you'll also find it useful for what you're doing in Microsoft Azure.

PM> Install-Package RedDog.Storage

So the first thing I did was create a few helpers that make it easy to mount an Azure Files share as a mapped drive. These methods will make it easy to mount, unmount and list mapped drives without having to worry about any native P/Invoke calls. And remember that this will work for Web/Worker Roles but also for Virtual Machines.