
This article describes the recently released Windows Azure Management Libraries for managing .NET applications from the cloud infrastructure, services and storage elements hosted in
Windows Azure . A description of the capabilities of the libraries is given on examples of the code of a WPF application.
What are Windows Azure Management Libraries?
With the release of the Windows Azure Management Libraries, a wide part of the Windows Azure cloud infrastructure can be accessed and automated from .NET applications. These libraries run on top of the open REST API functions of the platform and their release is intended to facilitate the developer’s work with cloud infrastructure.
')
The library preview available today includes support for cloud services (Cloud Services), virtual machines and networks, web sites (Web Sites), storage accounts and infrastructure elements such as accessibility groups (Affinity Groups).
We spent a lot of time designing the natural .NET Framework API, which transparently represents the underlying open REST API. It was very important for us to present these services with the help of modern familiar to developers approaches used on the .NET platform:
- Portable Class Library (PCL) support for .NET Framework 4.5, Windows Phone 8, Windows Store and Silverlight;
- distributed as NuGet packages with a minimum of dependencies to facilitate versioning;
- support for asynchronous and asynchronous operations based on async / await platform elements (with an easy option to implement synchronous overloaded methods);
- common infrastructure for unified error handling, tracing, configuration, and management of the HTTP pipeline;
- designed for ease of testing and mocking;
- built on popular libraries like HttpClient and Json.NET.
These packages offer you rich possibilities and ease of management, automation, deployment and testing of WIndows Azure cloud infrastructure. With them, you can manage virtual machines, cloud services, virtual networks, websites and key infrastructure components of the platform.
Beginning of work
As with any other SDK, in order to find out how we work with it is best to learn a little bit of code. No code should be written to solve a problem that actually does not exist, so let's first formulate what problem the Windows Azure Management Libraries libraries solve:
I have a process that I run on Windows Azure as a Worker Role. Everything works great, but this process, in fact, needs to be run only a few times a week. It would be great to be able to start a new service, place my code in it and then start its execution. It would be very good if after the execution of the service, the code reported this back, so that we could delete it automatically. Sometimes, I forget to turn off this service manually, so I may have extra expenses when it remains on after executing the code. It would be great to be able to automate the creation of the infrastructure I need, its launch and subsequent self-destruction.
Prior to the release of the Windows Azure Management Libraries (abbreviated, though not officially, WAML), the solution to the described problem was not an easy task. True, there are already a number of excellent opensource libraries from the community that offer a .NET layer for managing Windows Azure services, however, none of them has the full implementation of the Windows Azure Management REST API functions or the full-fledged implementation of C # features. When creating your own layer for managing the power of your Windows Azure cloud, you will definitely have to write your own HTTP / XML code to exchange requests with the REST API. This is not the most interesting thing to do in life. Tedious and boring work, especially after you implement your second dozen of the hundreds of API methods.
Installing Management Libraries
I decided to implement the task in the form of a simple WPF application that will run on my desktop. Since I will have a cloud service with a working role running in the cloud, I have combined all three projects into a single solution, which you can see below:

You probably noticed that I was preparing to add some NuGet packages to my application. This is due to the fact that Windows Azure Management Libraries are distributed as separate NuGet packages. I’m going to select the
Microsoft.WindowsAzure.Management.Libraries package
, and it alone will install all the libraries available in Management Libraries. If I want to manage only one aspect of Windows Azure, then instead of adding all the libraries, I can install only one specific package, for example,
Microsoft.WindowsAzure.Management.WebSites , which offers functionality to manage only the Windows Azure Web Sites infrastructure.

After adding the link to the package, I have everything I need to configure client authentication between my WPF application and the Windows Azure REST API.
Client authentication
The first user authentication implementation we wrote for WAML and Windows Azure was based on the X509 certificate mechanism. Recently, Windows Azure for .NET SDK 2.2, Visual Studio, and PowerShell added built-in authentication based on user accounts. We are currently working on its support in WAML. The currently available release of WAML only supports certificate-based authentication; however, stay tuned, we are working to add account-based authentication support to the library.
I will not focus on discussing the use of certificate-based authentication. On the contrary, I intend to proceed as soon as possible to the discussion of functional things in our article. I need information about just two elements to connect to the Windows Azure REST API:
- Subscription ID (Subscription ID);
- Management Certificate (Management Certificate).
I got these values ​​from one of my publish settings files. Below you can see the XML content of such a file:

Using the key and subscription identifier later in my code, I can call the GetCredentials method, which returns an instance of the abstract
SubscriptionCloudCredentials class, which we use in the code of the Library Library to represent data about user access parameters. With this approach, if I want to add account-based authentication in the future, it will be much easier for me to replace the authentication mechanism. The code for the
CertificateAuthenticationHelper class from my example is shown below:
using Microsoft.WindowsAzure; using System; using System.Security.Cryptography.X509Certificates; namespace MyCloudServiceManager { internal class CertificateAuthenticationHelper { internal static SubscriptionCloudCredentials GetCredentials( string subscrtionId, string base64EncodedCert) { return new CertificateCloudCredentials(subscrtionId, new X509Certificate2(Convert.FromBase64String(base64EncodedCert))); } } }
Now, I'll write a controller class that will work on the interaction between a WPF application and Management Libraries libraries.
Control layer
In order to take into account all the possible parameters that will be used in my service, I created the class
ManagementControllerParameters. This class contains all the data that I need to create services and place in the cloud of my code.
namespace MyCloudServiceManager { internal class ManagementControllerParameters { internal string Region { get; set; } internal string StorageAccountName { get; set; } internal string CloudServiceName { get; set; } internal string PackageFilePath { get; set; } internal string ConfigurationFilePath { get; set; } public string SubscriptionId { get; set; } public string Base64EncodedCertificate { get; set; } } }
Then I created a class that contains convenient functionality for the interaction between the user interface and the Management Library layer. This class is created for cleaner code in the UX layer later on. Pay attention to the constructor code. It creates two clients. First, the
StorageManagementClient is designed to manage storage accounts. The second
ComputeManagementClient offers the opportunity to work with the computing power of Windows Azure - cloud services and virtual machines, their locations, and so on.
In order to divide the code between the files into different functional parts, I created the
ManagementCont -partial-class
, the code of which is available to you in a
public Gist , so that you can use it in your own projects.
using Microsoft.WindowsAzure; using Microsoft.WindowsAzure.Management.Compute; using Microsoft.WindowsAzure.Management.Storage; using System; namespace MyCloudServiceManager { internal partial class ManagementController : IDisposable { private StorageManagementClient _storageManagementClient; private ManagementControllerParameters _parameters; private ComputeManagementClient _computeManagementClient; internal ManagementController(ManagementControllerParameters parameters) { _parameters = parameters; var credential = CertificateAuthenticationHelper.GetCredentials( parameters.SubscriptionId, parameters.Base64EncodedCertificate); _storageManagementClient = CloudContext.Clients.CreateStorageManagementClient(credential); _computeManagementClient = CloudContext.Clients.CreateComputeManagementClient(credential); } public void Dispose() { if (_storageManagementClient != null) _storageManagementClient.Dispose(); if (_computeManagementClient != null) _computeManagementClient.Dispose(); } } }
Now, let's create some clients to manage the cloud and do some work.
Creating a new storage account
The first thing we need to implement the strategy of our service is the storage account. The service will use the .cspkg package file created in Visual Studio based on the cloud service project. It must be placed as a blob in the Windows Azure storage. Before I can do this, I need to create an account in which the package file can be downloaded to the repository. The code below is intended to create a storage account in the specified region:
using Microsoft.WindowsAzure.Management.Storage; using Microsoft.WindowsAzure.Management.Storage.Models; using System.Threading.Tasks; namespace MyCloudServiceManager { internal partial class ManagementController { internal async Task CreateStorageAccount() { await _storageManagementClient.StorageAccounts.CreateAsync( new StorageAccountCreateParameters { Location = _parameters.Region, ServiceName = _parameters.StorageAccountName }); } } }
After the storage account is created, I can use it. I need a connection string to connect my application (and my “soon-to-be-created” cloud service) to the repository. I wrote a method that is responsible for obtaining connection keys to the Windows Azure storage through the REST API. Then, I form a connection string and return it to the calling code.
using Microsoft.WindowsAzure.Management.Storage; using System.Globalization; using System.Threading.Tasks; namespace MyCloudServiceManager { internal partial class ManagementController { private async Task<string> GetStorageAccountConnectionString() { var keys = await _storageManagementClient.StorageAccounts.GetKeysAsync(_parameters.StorageAccountName); string connectionString = string.Format( CultureInfo.InvariantCulture, "DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}", _parameters.StorageAccountName, keys.SecondaryKey); return connectionString; } } }
Now, after creating a storage account, I can create a cloud service and publish my package to Windows Azure.
Creation and deployment of a new cloud service
The code needed for a cloud service is surprisingly simple. All we need is to specify the name of the service and the region in which it should be created.
using Microsoft.WindowsAzure.Management.Compute; using Microsoft.WindowsAzure.Management.Compute.Models; using System.Threading.Tasks; namespace MyCloudServiceManager { internal partial class ManagementController { internal async Task CreateCloudService() { await _computeManagementClient.HostedServices.CreateAsync(new HostedServiceCreateParameters { Location = _parameters.Region, ServiceName = _parameters.CloudServiceName }); } } }
Finally, all that remains for me to host a cloud service is to load the service package file created in Visual Studio into a blob, and then call the REST API. This call will consist of the blob address in which the package is located and the XML data obtained from the configuration file of the cloud project. This code uses the
Windows Azure Storage SDK library, which is available as a NuGet package.
using Microsoft.WindowsAzure.Management.Compute; using Microsoft.WindowsAzure.Management.Compute.Models; using Microsoft.WindowsAzure.Storage; using Microsoft.WindowsAzure.Storage.Blob; using System.IO; using System.Threading.Tasks; namespace MyCloudServiceManager { internal partial class ManagementController { internal async Task DeployCloudService() { var storageConnectionString = await GetStorageAccountConnectionString(); var account = CloudStorageAccount.Parse(storageConnectionString); var blobs = account.CreateCloudBlobClient(); var container = blobs.GetContainerReference("deployments"); await container.CreateIfNotExistsAsync(); await container.SetPermissionsAsync( new BlobContainerPermissions() { PublicAccess = BlobContainerPublicAccessType.Container }); var blob = container.GetBlockBlobReference( Path.GetFileName(_parameters.PackageFilePath)); await blob.UploadFromFileAsync(_parameters.PackageFilePath, FileMode.Open); await _computeManagementClient.Deployments.CreateAsync(_parameters.CloudServiceName, DeploymentSlot.Production, new DeploymentCreateParameters { Label = _parameters.CloudServiceName, Name = _parameters.CloudServiceName + "Prod", PackageUri = blob.Uri, Configuration = File.ReadAllText(_parameters.ConfigurationFilePath), StartDeployment = true }); } } }
All code needed to create an application in Windows Azure is created. Finally, it remains to write some code to destroy the application in the cloud.
Deleting items in the Windows Azure cloud
Removing items in Windows Azure using the Windows Azure Management Libraries libraries is as easy as creating them. The code below clears the created storage, then it simultaneously removes the placed code and the cloud service itself.
using Microsoft.WindowsAzure.Management.Storage; using Microsoft.WindowsAzure.Management.Compute; using Microsoft.WindowsAzure.Management.Compute.Models; namespace MyCloudServiceManager { internal partial class ManagementController { internal void Cleanup() { _computeManagementClient.Deployments.DeleteBySlot(_parameters.CloudServiceName, DeploymentSlot.Production); _computeManagementClient.HostedServices.Delete(_parameters.CloudServiceName); _storageManagementClient.StorageAccounts.Delete(_parameters.StorageAccountName); } } }
Given all the convenience of writing the previous code, the code for the user interface should be relatively simple.
User interface
The user interface for our application is relatively simplified. I just placed a couple of buttons on a WPF form. The first allows you to create the necessary elements in Windows Azure and to place the code. The second removes elements from the cloud. Below is the XAML code:
<Window x:Class="MyCloudServiceManager.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Deploy Service" Height="166" Width="308"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="112*"/> <ColumnDefinition Width="125*"/> </Grid.ColumnDefinitions> <Button x:Name="_createButton" Content="Create" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="73,80,0,0" Grid.Column="1" Click="_createButton_Click"/> <Button x:Name="_deleteButton" Content="Delete" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="73,105,0,0" Grid.Column="1" Click="_deleteButton_Click"/> </Grid> </Window>
The control code is also very simple. In the event handler for clicking on the
Create button, I create an instance of
ManagementController , passing it all the necessary parameters to create application components in Windows Azure. Then I call the controller methods to create items in the cloud.
I also handle the event of pressing the
Delete button
, deleting and clearing all the elements created earlier in the cloud.
using Microsoft.WindowsAzure.Management.Models; using System.IO; using System.Windows; namespace MyCloudServiceManager { public partial class MainWindow : Window { private ManagementController _controller; public MainWindow() { InitializeComponent(); } private async void _createButton_Click(object sender, RoutedEventArgs e) { if (_controller == null) { _controller = new ManagementController( new ManagementControllerParameters { Base64EncodedCertificate = "MIIKXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", SubscriptionId = "66c15fe5-XXXX-XXXX-XXXX-XXXXXXXXXX", CloudServiceName = "ManagementLibraryDemoSvc01", ConfigurationFilePath = "ServiceConfiguration.Cloud.cscfg", PackageFilePath = "MyCloudService.cspkg", Region = LocationNames.WestUS, StorageAccountName = Path.GetFileNameWithoutExtension(Path.GetRandomFileName()) }); } await _controller.CreateStorageAccount(); await _controller.CreateCloudService(); await _controller.DeployCloudService(); } private void _deleteButton_Click(object sender, RoutedEventArgs e) { _controller.Cleanup(); } } }
You can modify this code to use the capabilities of the Windows Storage SDK to monitor on the client side the message queue in the cloud storage. After the cloud service has completed its work, it will send a message to the queue in the cloud. The message will be received by the client, who will call the Cleanup method and remove all application components from the cloud.
Endless automation
Windows Azure Management Libraries offer a great layer of automation between your code and Windows Azure. You can use these libraries to automate the entire spectrum of processes for creating and deleting Windows Azure components. In the current version, we offer libraries for managing computing capacity and cloud storage, as well as Windows Azure Web Sites components. Over time, we will add more functions to the libraries. Our goal is to provide you with the ability to automate the execution of any task in Windows Azure.
We look forward to your feedback on working with libraries. Please try, experiment with Management Libraries and let us know to facilitate what tasks you use them. We are open to any of your ideas or questions. The library code is open, like the code for many other libraries for Windows Azure, you can find it in our
repository on GitHub .