Tuesday, November 01, 2011 #

Using Windows Azure table storage

The Azure Table service, provides a semi-structured storage in the form of tables  that contain a collection of entities, these entities have primary keys and set of properties. A property is a name, typed-value pair.

Every entity in table storage has two key properties, Partition Key and the Row key which together uniquely identify each entity in the table.

The Table Service API is compliant with REST API provided by WCF Data Services. In order to use the WCF Data Services Client Library to access data in table storage, you need to create a context class that derives from TableServiceContext, which itself derives from DataServiceContext in WCF Data Services.

So to get started lets create a new project with Visual Studio with a web role.

Next we need to create a new project for the schema classes. To do this click on File –> Add –> New Project.

  • Choose Class Library and give it a name I named mine LogSheet_Data
  • Delete the default class file generated by the class library template.
  • Add a reference to System.Data.Services.Client.
  • Add another reference to Microsoft.WindowsAzure.StorageClient 
  • Now we are ready to define its schema. to do this add a new class to your project, I am going to name mine LogSheetEntry
    • Add the namespace declaration to import the types from the two assemblies

using Microsoft.WindowsAzure.StorageClient;

    • Update the declaration of the LogSheetEntry class to make it public and derive from the the TableServiceEntity class

public class LogSheetEntry
    : Microsoft.WindowsAzure.StorageClient.TableServiceEntity
    {
    }

  • TableServiceEntity is a class found in the Storage Client API. This class defines the PartititionKey, RowKey and TimeStamp system properties required by every entity stored in a Windows Azure table.
    Together, the PartitionKey and RowKey define the DataServiceKey that uniquely identifies every entity within a table.
    • Add a default constructor then we need to initialize its PartitionKey and Row Key.

 

public LogSheetEntry()
{           
    PartitionKey = DateTime.UtcNow.ToString("MMddyyyy");
 
    RowKey = string.Format("{0:10}_{1}", DateTime.MaxValue.Ticks - DateTime.Now.Ticks, Guid.NewGuid());
}

    • To complete add the properties

public string Facility { get; set; }
public string AccountNum { get; set; }

    • Save
  • Next, you need to create the context class required to access the LogSheet table using WCF Data Services. To do this, in Solution Explorer, right-click the LogSheet_Data project, point to Add and select Class. In the Add New Item dialog, set the Name to LogSheetDataContext.cs   and click Add.
  • In the new class file, update the declaration of the new class to make it public and inherit the TableServiceContext class.

public class LogSheetDataContext
  : Microsoft.WindowsAzure.StorageClient.TableServiceContext

  • Now add a default constructor to initialize the base class with storage account information

public GuestBookDataContext(string baseAddress, Microsoft.WindowsAzure.StorageCredentials credentials)
    : base(baseAddress, credentials)
{ }

  • Add a property to the LogSheetDataContext class to expose the LogSheetEntry

public IQueryable<LogSheetEntry> LogSheetEntry
{
  get
  {
    return this.CreateQuery<LogSheetEntry>("LogSheetEntry");
  }
}

  • Finally, you need to implement an object that can be bound to data controls in ASP.NET. In Solution Explorer, right-click LogSheet_Data, point to Add, and select Class. In the Add New Item dialog, set the name to LogSheetDataSource.cs
  • In the new class file, add the following namespace declarations to import the types contained in the Microsoft.WindowsAzure and Microsoft.WindowsAzure.StorageClient namespaces.

using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;

  • Make the class public and define member fields for the data context and storage account information

public  class LogSheetDataSource
  {
      private static CloudStorageAccount storageAccount;
      private LogSheetDataContext context;
  }

  • Now , add a static constructor That will create the tables from the LogSheetDataContext class

   static LogSheetDataSource()
{
  storageAccount = CloudStorageAccount.FromConfigurationSetting("DataConnectionString");
  CloudTableClient.CreateTablesFromModel(
      typeof(LogSheetDataContext),
      storageAccount.TableEndpoint.AbsoluteUri,
      storageAccount.Credentials);
}

  • add a default constructor to initialize the data context class used to access table storage.

public LogSheetDataSource()
{
    this.context = new LogSheetDataContext(storageAccount.TableEndpoint.AbsoluteUri, storageAccount.Credentials);
  this.context.RetryPolicy = RetryPolicies.Retry(3, TimeSpan.FromSeconds(1));
}

  • Next we will need a method that will return the contents of LogSheetEntry table

public IEnumerable<LogSheetEntry> GetLogSheetEntries()
{
    var results = from g in this.context.LogSheetEntry
                  where g.PartitionKey == DateTime.UtcNow.ToString("MMddyyyy")
                  select g;
    return results;
}

  • We also are gong to need a method that will insert new entries

public void AddLogSheetEntry(LogSheetEntry newItem)
{
    this.context.AddObject("LogSheetEntry", newItem);
    this.context.SaveChanges();
}

  • You guessed it we will need a method for an update, for simplicity, we are going to change the the facility only at this time.

public void UpdateImageThumbnail(string partitionKey, string rowKey, string Facility)
{
    var results = from g in this.context.LogSheetEntry
                  where g.PartitionKey == partitionKey && g.RowKey == rowKey
                  select g;
    var entry = results.FirstOrDefault<LogSheetEntry>();
    entry.Facility = Facility;
    this.context.UpdateObject(entry);
    this.context.SaveChanges();
}

  • Next steps is Creating a Web Role to Display the Time Sheet Entry and process User input, which is going to be in my next blog entry.

 

Posted On Tuesday, November 01, 2011 2:40 PM | Feedback (7)

Creating a Windows Azure VM Role

 

I wanted to have a VM that I could access remotely and thought why not take advantage of the VM role. So I just documented my steps, for those of you might be interested, but more importantly for myself, if I have to do this again.

the server image that is used for the VM role in windows Azure consists of a base VHD and may or may not have differencing VHD. to create the base VHD you can use Hyper-V Manager.

Creating the Base VM image

  1. In Hyper-V manager connect to your server , right-click the server name
  2. Point to New, and then select Virtual Machine.
  3. Click Next at the welcome screen to start the New Virtual Machine Wizard.
  4. Give your VM a name and press Next.
  5. Assign Memory size, I am going to go with 4096, you should use minimum of 2048 to have a halfway decent performance.
  6. For your network select an external connection that has been configured, if you do not have any connections in the drop down please refer to http://technet.microsoft.com/en-us/library/ee247420(WS.10).aspx
  7. Now, in the Connect Virtual Disk step, select the option labeled Create a virtual hard disk. Set the Name of the disk to baseimage.vhd, change the location to a suitable folder in your Hyper-V server, set the disk Size to 30GB, and then click Next to continue. the size must fit the quota allocated for the chosen VM size for your role. In this case, setting the size to 30GB allows you to deploy the VM in a small Role.
    1. If you already have a VHD file with a clean installation of Windows Server 2008 R2, you may use that instead. To do this, select the option labeled Use an existing virtual hard disk and browse to the location of the VHD file. Note that the image file must contain a single partition with the OS installation and must not include a recovery partition.
  8. Select the Operating system, you can do this using a dvd, .iso image or other options, or selecting another image that already has the operating System.
  9. Press Next , review the settings and press Finish
  10. In the toolbar of the Virtual Machine Connection window, click the Start icon.
  11. after you connect you proceed with the installation of your operating system.

  12. The only special requirement for a valid VM Role image is to allocate the entire virtual hard disk file to a single partition where you install the operating system. To avoid creating a recovery partition during the installation, follow these steps:

    • Choose the Custom (advanced) installation type to select the partition where you will install Windows.
    • Press Shift + F10 to open a command prompt during GUI-mode setup.
    • At the command prompt, enter the following commands:

      diskpart
      select disk 0
      create partition primary
      exit
      Close the command prompt window.

    • Install Windows in the newly created partition.

Preparing the base VM Image for Deployment

Now we need to install all the components required for deployment to windows Azure.

  1. In Server manager click on add Roles and select webserver (IIS). press next and take all the default options and click on Install.
  2. Add Features , and select .Net Framework 3.5.1
  3. Open Windows update Control Panel from Start –> All Programs –> windows Update
    • Click on Change Settings link and then select Never Check For Updates (not recommended)
    • Click on Check for updates and install all available updates

Install windows Azure Integration component

    1. In the Virtual Machine Connection window, in the Media menu, point to DVD Drive and then select Insert Disk. In the Open dialog, browse to the location of the ISO file for the VM Role Integration Components, wavmroleic.iso, and then click Open. You can find this ISO in Windows Azure SDK folder ({drive}\Program Files\Windows Azure SDK\{version}\iso
    2. After you connect AutoPlay dialog will appear and then click Open Folder to view files using windows Explorer. If AutoPlay is disabled, use windows explorer and navigate to the newly created DVD Drive.
      • launch WaIntegrationComponents-x64.msi.
      • Press Next
      • Enter Administrator Password and press Next
      • Click on Install.
    3. When prompted to install device software, click Install to proceed.
    4. When prompted to restart the System, click Yes to continue.
    5. Wait for the system to restart and log in to the guest machine once again.
    6. Now, inside the VM, open the Start menu, type %windir%\system32\sysprep\sysprep.exe and then press Enter to launch the System Preparation Tool.
      • Set the System Cleanup Action to “Enter System Out-of-Box Experience (OOBE)”
      • Check the option labeled Generalize
      • Set the Shutdown Options to Shutdown, and then press OK. (Sysprep.exe) prepares the image by cleaning up various user and machine settings and log files, as well as removing any hardware-dependent information.
    7. Wait for system completely shut down.

Uploading the VM Disk image to Windows Azure

  1. Open a Windows Azure SDK Command Prompt as an administrator from Start –> All Programs –> Windows Azure SDK v1.x.
    • Before you can continue you want to make sure you have

    • Your subscription ID

    • Thumbprint of the certificate you have uploaded to learn how you can create a certificate and upload it to Azure manged certificates see(http://msdn.microsoft.com/en-us/library/gg432987.aspx)

    • Path to VHD File created earlier using Hyper-V Manager

    • Hosted Service location(choose, “East Asia”, “North Central US”, “North Europe”, “South Central US”, “Southeast Asia”, “West Europe”)

    • In Windows Azure Command Prompt execute the following command

csupload Add-VMImage -Connection "SubscriptionId={YourSubscriptionID};
 CertificateThumbprint={YourThumbPrint}-Description "Base image Windows Server 2008 R2" –LiteralPath 
"{PathToVHDFIle" -Name baseimage.vhd -Location {HostedServiceLocation}

Press Enter to start execution.

  1. In the Windows Azure VHD Verification Tool dialog, click OK to allow the VHD to be mounted. If the AutoPlay dialog appears, close it.

    5cf79665-981e-4bee-b3b2-73d6619be5b9

  2. The tool will prepare and create a new blob to hold the image file and then begins to upload the compressed image to your windows Azure account.

  3. In Hosted Services,Storage Accounts & CDN tab of Azure’s Management Portal you should see Pending under your VM Images.

  4. Depending on your Internet connection it might take hours for the image to get uploaded, which at that point the status will be changed to committed.

  5. Start Visual Studio

    1. Create a New Windows Azure Project (Language does not matter, but make sure Create directory for solution, is checked. Click OK.

    2. New Windows Azure Project click OK. No need to add any roles.

    3. Right click on Roles –> Add –> New Virtual Machine Role.

      1. If you have used Visual Studio previously to deploy service packages to Windows Azure, you may already have created the required credentials. For instructions on how to do this, see Appendix A - Configuring your Windows Azure Management Portal Credentials in Visual Studio.

      2. Once you configure the credentials, choose them in the drop down list labeled Select or create your Windows Azure account credentials. After you do this, Visual Studio accesses your subscription and retrieves a list of available virtual machine images.

      3. Expand the drop down list labeled Select VHD and choose the image named baseimage.vhd, which contains the installation of Window Server 2008 R2 Enterprise Edition that you uploaded earlier.
    4. Click on the Endpoints tab, then click Add Endpoint

        • Name = HttpIn

        • Type = Input

        • Protocol = htttp

        • Public Port = 80

        • Private Port = 80

    5. Configure the Remote Desktop connections for your Role, by right-clicking on your cloud service project, in Solution Explorer and select Package.

      • In Package Windows Azure Application dialog box, click Configure Remote Desktop connections.

      • Check the option labeled Enabled connections for all roles.

      • Expand the drop down list labeled Create or select a certificate to encrypt the user credentials and select Create.

      • In the Create Certificate dialog, enter a name to identify the certificate,  and then click OK.

      • In the Remote Desktop Configuration dialog, select the certificate from the drop down list, enter the name a  user and password,

        you may change the expiration Date.

      • Click on View, in the Details tab click on Copy to File, follow the wizard to export the certificate to a file, make sure that you check Export the private key. Save the file, the file will be uploaded to management portal later and press OK.

      • Click on Package, be patient this will take a few minutes.

Creating the Hosted Service and Deploying the package

    1. In Management Portal –select Hosted Services under the Hosted Services, Storage Accounts & CDN.
    2. Click on New Hosted Service.
      • Enter a service Name
      • Enter URL prefix for your service
      • Select a region or affinity group
      • select Do not deploy.
    3. Expand the node for your hosted service to display and select the certificates and click on Add Certificate.
      • Browse to the location of the certificate file and provide the password.
    4. Select your service then click on the New Production Deployment.
      • Provide a name, good name would be a version number like v1.0
      • Browse locally to the  package file (.cspkg) file created by Visual Studio
      • Browse locally to the configuration (.cscfg) file created by Visual Studio.
    5. You can monitor the status of your deployment.

Posted On Tuesday, November 01, 2011 8:59 AM | Feedback (12)