Archive for June, 2011

This Blog will show you all the requirements and the software needed to begin developing on windows Azure and make a “Hello World” website hosted on Windows Azure Platform.  You must be running on windows 7 or windows server with IIS feature enabled.

First of All you need Windows Azure tools and SDK for any programming language and any environment.  Then you need to configure your IIS on your
machine to be able to run Windows Azure Emulator.

–        Open your Start menu and
write in the search bar: “Turn windows features on or off” or you can get it
from the Control Panel > Programs and features.

Windows azure has 3 types of storage: Windows Azure storage, Windows Azure App Fabrics Caching and SQL Azure. All the data hosted on the cloud is replicated automatically 3 times. Windows Azure has an automatic failover and load balancing, once one these replicates has a hardware failure or facing any problems, a second node is marked as primary and another replicate is created to maintain the high availability and stability (you will always have 3 replicates on Azure).

To go further in the Windows Azure storage security, we have to understand how the application on Azure is run. The cloud has a different point of view than the application hosted and running on a single on premises server. On Azure, storage is independent. It is hosted on separate hardware than the compute hardware where the application runs. The Azure storage always needs an SSL whether from the different nodes on Azure or from on premises servers, it doesn’t trust any kind of connection. So to get the most secure connection the top layer of the storage architecture layers: validates, authenticates and authorizes the connections and the users trying to connect to the storage and then navigate them to the other layers where the data are stored. To prevent the interaction between the data stored on the cloud, Windows Azure has made physical and logical separations.

For the app fabric caching and Windows Azure storage their owner is provided an access key which is named some time the secret key, with what he is granted the full control on his cloud storage. For the SQL Azure it implements the same concepts of SQL access control with a connection string that contains a username and password.

First Step to use the BLOB

“Pay as you go” è this is one of the advantages of Cloud Computing. Speaking of Windows Azure as Microsoft Platform for Cloud Computing, it offers developers different types of storage one of them is the BLOB. It stands for Binary Large Object.

It is used for uploading any type of file to the Cloud like a text file, a picture or even a movie. The blob’s size can be terabytes and can be divided in Blocks or BlobPages which I am going to discuss in later blogs. I just wanted to show you in this blog who to work with Azure using blob storage especially it only cost you 0.15$/month for each GB (prices may vary from continent to another) plus the number of transactions which is equal to 0.01/month for each 10K transactions made.

To Begin in the Technical part we have to understand the main things that must exist to start using the BLOB storage.

Let’s simply imagine it as something like your desktop. Your windows on your desktop may have several accounts where each one of them is only allowed to access only certain number of files. This is nearly like the windows Azure Storage you do have some files on it which we call the blobs. You must have the account where you have put the blob storage.

So what’s about the container? The containers are only something virtual. For example your hard disk is divided into a certain number of partitions on your desktop. Each partition has a name (C, D ….). On Azure it is also nearly the same the container are the virtual place where you put the blobs.

For each account you make, you can have any number of containers and for each container any number of Blobs which size can be Terabytes.

First you have to set the connection string with which you are going to access the account like the following:

In the following code you will see how to create the account, container and to upload the blob:

public void uploadfile()
{
//creating the account and the cloud blob client

//sometimes you can find them separated the account 

//is created in a command and the cloudblob in another one

var account = CloudStorageAccount.FromConfigurationSetting(“DataConnectionString”).CreateCloudBlobClient();

//getting a reference for the blob so you can access it

var blob = account.GetBlobReference(“BlobAdress”);
try
{
//uploading the data required in the blob
//there are several overloaded method where you
//can upload different types of files in it
blob.UploadFile(“”);
}
catch (StorageException ex)
{
if (ex.ErrorCode == StorageErrorCode.ContainerNotFound || ex.ErrorCode == StorageErrorCode.BlobNotFound)
{
//getting a container reference
//you can access the container from other applications
//with the same account and the contaienr’s URI
var container = account.GetContainerReference(“ContainerName”);
//creating the container if it didnt already exists
container.CreateIfNotExist();

var permissions = container.GetPermissions();
permissions.PublicAccess = BlobContainerPublicAccessType.Blob;
container.SetPermissions(permissions);
}
else
{
throw;
}
}
}


As I talked before about the relation between WP7 and the cloud in a previous blog, I will show you now how to begin coding to start using the different push notification using a WCF Service hosted on a cloud application.

The usual connection to the internet or to the cloud is made first by the mobile or in this case the WP7. In the following examples the cloud will initiate and send a notification to the mobile device id and only if the mobile is registered into the service or the channel.

The first kind of the push notification is the Toast. It is a simple notification sent to the WP7 it looks a bit like the SMS but it depends on the internet connection and the WP7 must be subscribed in the Notification channel. In other words, this application mainly has 2 parts: one hosted on the cloud and the second one is installed on the device. It is simply subscribe the device to the channel or the service. This type of notification doesn’t require you to be running the application. Here is some code:
[OperationContract]
void sendtoast(string message, string channeluri)
{
try
{
//create the HttpWebRequest to Microsoft Notification Server
//this require internet connection
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(channeluri);
request.Method = “POST”;
request.ContentType = “text/xml; charset=utf-8”;
request.Headers[“X-MessageID”] = Guid.NewGuid().ToString();
request.Headers.Add(“X-NotificationClass”, “2”);
request.Headers.Add(“X-WindowsPhone-Target”, “toast”);
//toast message templete
string notificationData = “<?xml version=\”1.0\” encoding=\”utf-8\”?>” +
“<wp:Notification xmlns:wp=\”WPNotification\”>” +
“<wp:Toast>” +
“<wp:Text1>WP7 TOAST</wp:Text1>” +
“<wp:Text2>” + message + “</wp:Text2>” +
“</wp:Toast>” +
“</wp:Notification>”;

byte[] contents = Encoding.Default.GetBytes(notificationData);
request.ContentLength = contents.Length;
//write to request stream
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(contents, 0, contents.Length);
}
}
catch
{
}
}

If you press on the notification sent, it navigates you to the application itself.
All the above examples are implemented in a WCF service hosted in a cloud application. The following is the code required to be in the mobile application in the app.xaml.cs

public App()

{


// Global handler for uncaught exceptions.

UnhandledException += Application_UnhandledException;


// Show graphics profiling information while debugging.


if (System.Diagnostics.Debugger.IsAttached)

{


// Display the current frame rate counters.


Application.Current.Host.Settings.EnableFrameRateCounter = false;


// Show the areas of the app that are being redrawn in each frame.


//Application.Current.Host.Settings.EnableRedrawRegions = true;


// Enable non-production analysis visualization mode,


// which shows areas of a page that are being GPU accelerated with a colored overlay.


//Application.Current.Host.Settings.EnableCacheVisualization = true;

}


// Standard Silverlight initialization

InitializeComponent();


// Phone-specific initialization

InitializePhoneApplication();


// Create the shell tile schedule instance

CreateShellTileSchedule();

}


// To store the instance for the application lifetime


private
ShellTileSchedule shellTileSchedule;


///
<summary>


/// Create the application shell tile schedule instance


///
</summary>


private
void CreateShellTileSchedule()

{

shellTileSchedule = new
ShellTileSchedule();

shellTileSchedule.Recurrence = UpdateRecurrence.Interval;

shellTileSchedule.Interval = UpdateInterval.EveryHour;

shellTileSchedule.StartTime = DateTime.Now;

shellTileSchedule.RemoteImageUri = new
Uri(@”http://cdn3.afterdawn.fi/news/small/windows-phone-7-series.png&#8221;);

shellTileSchedule.Start();

}


// Code to execute when the application is launching (eg, from Start)


// This code will not execute when the application is reactivated


private
void Application_Launching(object sender, LaunchingEventArgs e)

{

}


// Code to execute when the application is activated (brought to foreground)


// This code will not execute when the application is first launched


private
void Application_Activated(object sender, ActivatedEventArgs e)

{

}


// Code to execute when the application is deactivated (sent to background)


// This code will not execute when the application is closing


private
void Application_Deactivated(object sender, DeactivatedEventArgs e)

{

}


// Code to execute when the application is closing (eg, user hit Back)


// This code will not execute when the application is deactivated


private
void Application_Closing(object sender, ClosingEventArgs e)

{

}


// Code to execute if a navigation fails


private
void RootFrame_NavigationFailed(object sender, NavigationFailedEventArgs e)

{


if (System.Diagnostics.Debugger.IsAttached)

{


// A navigation has failed; break into the debugger

System.Diagnostics.Debugger.Break();

}

}


// Code to execute on Unhandled Exceptions


private
void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)

{


if (System.Diagnostics.Debugger.IsAttached)

{


// An unhandled exception has occurred; break into the debugger

System.Diagnostics.Debugger.Break();

}

}

#region Phone application initialization


// Avoid double-initialization


private
bool phoneApplicationInitialized = false;


// Do not add any additional code to this method


private
void InitializePhoneApplication()

{


if (phoneApplicationInitialized)


return;


// Create the frame but don’t set it as RootVisual yet; this allows the splash


// screen to remain active until the application is ready to render.

RootFrame = new
PhoneApplicationFrame();

RootFrame.Navigated += CompleteInitializePhoneApplication;


// Handle navigation failures

RootFrame.NavigationFailed += RootFrame_NavigationFailed;


// Ensure we don’t initialize again

phoneApplicationInitialized = true;

}


// Do not add any additional code to this method


private
void CompleteInitializePhoneApplication(object sender, NavigationEventArgs e)

{


// Set the root visual to allow the application to render


if (RootVisual != RootFrame)

RootVisual = RootFrame;


// Remove this handler since it is no longer needed

RootFrame.Navigated -= CompleteInitializePhoneApplication;

}

#endregion

}