Microsoft Windows Azure Storage BlockBlob Intro

I just wanted to show a quick example of how to save a basic item in Microsoft Windows Azure Storage using BlockBlob storage.

Basically using the simplest process it takes about 8 steps to successfully save an item in Azure Storage.

  1. Get the CloudStorageAccount
  2. Get a CloudBlobContainer
  3. If container doesn’t exist create it
  4. Get a CloudBlockBlob from the CloudBlobContainer
  5. Create a MemoryStream for your byte array
  6. Set the permissions
  7. Set the content-type
  8. Upload the stream

In code this looks like this

morecode

and click here to download a simple little Azure BlockBlob storage manager that I created to help with the process.

Advertisements

Always use ChannelFactory when referencing WCF services (particularly for Azure)

Given the indeterminate nature of the IP addresses of Azure web roles (There are a number of exceptions to this but in general) you end up in a real pickle if you you use the Service Reference functionality built into Visual Studio that most of us regularly do.

(This thing)

Image

Truthfully, the way the proxy service references have always worked had its flaws despite its convenience.

The problem boils down to two things. First, if you’re correctly using WCF services you should be binding to DataContracts and Interfaces rather than any concrete implementations anyway. Secondly, you’re stuck with hard-coded values that live in your web.config, with what is inherently dynamic and often not known at compile time (ip addresses).

So how do you solve it?

Remarkably simply, it takes the form of the channel factory and provided you have both your datacontracts and your service interfaces referenced you can then do this.

Image

Download the example.

 

Implementing WCF Net.Tcp binding/activation in Azure Web Role

I’ve been spending a significant amount of time recently working with the Azure platform and all I can say is WOW! Congratulations to Microsoft for creating an awesome cloud computing platform. The power, scalability, robustness and small learning curve of Azure is a godsend.

Niceties aside, working out how to get WCF Net.Tcp bindings working in an Azure web role has been far from a trivial process so I figure its time to share the magic.

Step 1 (Configure the internal endpoint):

Image

Step 2 (Configure the WebRole to run in elevated execution context in the ServiceDefinition.csdef file):

Image

Step 3 (In the WebRole OnStart() method add code that installs NonHttp Activation and configures the site bindings to support net.tcp):

Note that this code requires a reference to Microsoft.Web.Administration which is located in C:\Windows\System32\inetsrv\

private void InstallWCFNonHTTPActivation()
{
    using (Process installer = new Process())
    {

        installer.StartInfo.UseShellExecute = false;
        installer.StartInfo.RedirectStandardOutput = true;
        installer.StartInfo.FileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "dism.exe");
        installer.StartInfo.Arguments = " /online /enable-feature /featurename:WCF-NonHTTP-Activation";
        installer.StartInfo.CreateNoWindow = true;
        installer.Start();
        //string output = installer.StandardOutput.ReadToEnd();
        installer.WaitForExit();
    }
}

private void ConfigureSite()
{
    try
    {
        InstallWCFNonHTTPActivation();

        ServerManager manager = new ServerManager();
        Site CurrentSite = manager.Sites.FirstOrDefault();

        if (CurrentSite != null)
        {
            Application app = CurrentSite.Applications.FirstOrDefault();

            if (app != null)
                app.EnabledProtocols = "http, net.tcp";

            CurrentSite.Bindings.Add("123:" + CurrentSite.Bindings.First().EndPoint.Address, "net.tcp");
        }

        manager.CommitChanges();
    }
    catch (Exception ex)
    {
        System.IO.File.WriteAllText(Path.Combine(Directory.GetCurrentDirectory(), "ConfigureSiteException.txt"), ex.Message);
    }

}

And there you have it, your Azure Web Role will now Install Non-Http Activation, configure bindings and automatically start without any intervention. Simple as that, only took me 3 days to work out! :-p