Windows Azure is one of the latest Microsoft web technologies that offers fast cloud based web hosting to .NET web solutions. 51Degrees.mobi Foundation now supports the Azure cloud, allowing fast, accurate browser detection with all the capabilities anyone could ever need. Here is a guide on how to deploy the Foundation on Windows Azure. To The Cloud!
What's the Difference?
Windows Azure is powered by ASP.NET, so it was possible to use 51Degrees.mobi Foundation to detect mobile browsers same as before. However, there were issues with tracking redirections over multiple instances and keeping the log file. Azure doesn't have file system concept so keeping that information in a common file location cannot work.
Instead, this update stores that information into a storage table. This enables all the instances to keep logs for debugging and to keep a record of previous device requests so 'firstRequestOnly=true' can work.
Setting up the Foundation
The code for Foundation can be found here. Anyone looking to deploy should also look at the release note. This release introduces a new conditional compiler symbol, 'AZURE'. Only AZURE builds will work on the cloud but will not work on regular ASP.NET. See the user guide and example website in the source code linked to above to see how to integrate Foundation with your web application.
To make use of the changes you must create a storage account and put the name into the AZURE_STORAGE_NAME string in the Constants class (this requires modifying the source). The foundation will then create two tables, one for previous devices, and another for logs. The names for the tables are taken from web.config where the file names are usually specified (‘redirect/devicesFile' and ‘log/logFile'). Any invalid characters, such as ‘/' are stripped out. If you don't need to use either of the tables then simply leave devicesFile/logFile out. If both tables have been omitted then a 'fiftyonedegrees' storage account isn't required. Otherwise an exception will be thrown.
Apart from that, the rest of the Azure configuration is the same as any other Azure configuration. The only caveat to that is that unlike some Azure deployments, the web.config must be deployed. Set the Build Type as 'Content' in the Properties of web.config to make sure the package is deployed with all the correct foundation settings.
Accessing the Data
The way to view the log in traditional deployments is to just open the text file. In the Azure environment though, the table must be read in code and then either shown in a browser or downloaded to a file. This code shows how to fill a string with the contents of the log table:
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;
using Microsoft.WindowsAzure.ServiceRuntime;
using FiftyOneDegrees;
//Access the storage account
var storageAccount = CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue("fiftyonedegrees"));
//Create the service context to access the table
var serviceContext = new TableServiceContext(
storageAccount.TableEndpoint.ToString(),
storageAccount.Credentials);
//Getting the table entries
foreach (var row in serviceContext.CreateQuery("log")) //"log" - the name of the table you wish to examine
{
OutputString += row.Message;
}
//Deleting all the table entries
foreach (var row in serviceContext.CreateQuery("log"))
{
serviceContext.DeleteObject(row);
}
serviceContext.SaveChanges(); //It is important to save changes to delete objects
Will this Cost Extra?
As always, the Foundation continues to be free and open source. However, the Foundation will also use a small amount of storage transactions to keep the tables up to date. The code has been designed to use as few queries as possible, so this shouldn't be a problem.
And with that you now have mobile detection on the Azure Cloud. If you run into problems make sure you have compiled the Foundation with both the 'AZURE' and 'VER4' compiler symbols, or you could leave a comment or go to our forums.