Programming Windows Azure Table Storage

In my previous post, we went through the basics of Windows Azure Table Storage.

The sample application we are going to create is a small guestbook app which will allow your site visitors to enter some information.

Creating the Project

Open Visual Studio 2008 and create a Web Cloud Service project.
 


Updating Service Definition

The first step to do is to update the service definition and service configuration files to include the storage account. Edit your service definition file found in the Guestbook project and enter these settings



AccountName

This specifies your Windows Azure account name


AccountSharedKey

The shared key used to authenticate requests made against your Windows Azure account

TableStorageEndpoint

The base URI of the table storage service

Updating Service Configuration

After setting the properties in our service definition, we need to set the values of those properties in our service configuration file found in the Guestbook project


 
AccountName

devstoreaccount1

AccountSharedKey
 
Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==

TableStorageEndpoint

http://127.0.0.1:10002/

All the values reflect your local development storage and nothing in the cloud. The development storage supports only the above fixed account and is same for everyone.

We should be able to test our azure application locally in the local development storage before deploying it to the cloud.  Testing your application locally will also help you to debug your applications.

Creating  the Table Schema

Defining the table schema is as easy as creating classes. The idea here is to create a class, where the class name corresponds to the table name and the properties correspond to the table properties. The local development storage environment uses ADO.NET to create tables in the local environment, whereas this is not needed when we host our application in the cloud.

Creating these classes are made simple by using the StorageClient library provided in the Windows Azure SDK


 
You can add the StorageClient project to our Guestbook solution or just reference the library.

Below is our class which represents the Guestbook table:


 
As you can see in the above screenshot, our class GuestDataModel inherits TableStorageEntity. This TableStorageEntity class is available in the StorageClient library and defines some of the properties such as partition key and row key that is required for each entity in the table.

The GuestDataModel consists of three properties:

1) Message
2) Name
3) Url

And our simple constructor where we initialize the partition key and row key:


 
As the local storage uses ADO.NET we are going  to create class GuestDataServiceContext which inherits from  TableStorageDataServiceContext from the StorageClient library. The TableStorageDataServiceContext represents the runtime context of our ADO.NET service.


 
Now having the data model and the data context in place, its very easy to query our storage and perform the usual insert, delete operations

We create a GuestDataSource which is going to help interact with the data context and insert, delete and get all the GuestDataModel entities
 


As you can see, it's pretty straight forward. Below are insert and delete methods:
 


Creating our Tables

We saw how to insert, delete and get all our entities from the table using a simple data source, but where are we actually creating the table?



We are going to create the table on the first request to the website which can be done by adding the appropriate code into the global application class.
 
Executing this guestbook sample

Executing this guestbook sample is just as same as executing a normal asp.net application! Hit F5 and Visual Studio 2008 will take care of everything - from starting the local development storage, creating the tables and debugging the application.

Below is a screenshot of our guestbook app:


 
Deploying Guestbook to the Cloud

To deploy this sample in the cloud, you should have a Windows Azure storage account. For more information on deploying a service on Windows Azure, visit here.

I have already deployed this application to the cloud. So, if you are interested to try it out, please visit http://guestbook.cloudapp.net

You can download the Guestbook sample below:
 

*Originally posted at Geekzone VS 2008 blog

Getting Started with Windows Azure

I always wanted to write a small post which would help others getting started with Windows Azure. So here is my post which I wrote for Geekzone VS 2008 blog

The Azure Services Platform is designed to help developers quickly and easily create, deploy, manage, and distribute web applications and services. Windows Azure is a cloud services operating system that serves as the development, service hosting, and service management environment for the Azure Services Platform.

Windows Azure provides developers with on-demand compute and storage to host and manage web applications on the internet through Microsoft data centers.

How do I get started?

Below are the tools you need to install in your development machine to get started with Windows Azure

•    Visual Studio 2008
•    Windows Azure SDK
•    Windows Azure Tools for Microsoft Visual Studio

The Windows Azure SDK has all the necessary binaries, libraries and documentation that you need to build Windows Azure applications.

Windows Azure Tools for Visual Studio 2008 extends Visual Studio to build, debug, package Windows Azure applications.

The local Cloud depot

In order to publish your applications to the cloud, you need to have a valid Windows Azure account. You can apply for your account here, but in the meantime, you can make use of the local development fabric from the Windows Azure SDK.

The development fabric simulates Windows Azure cloud (fabric) in your local development machine and allows to run and test your Windows Azure applications.

Windows Azure Roles

Windows Azure roles are discrete scalable components built with managed code. There are two roles available in Windows Azure

  • Web Role
    • A web role is an application that listens and responds for web requests via HTTP or HTTPS endpoint
  • Worker Role
    • A worker role is an application which runs as a background processing application and does not expose any endpoints

Every Windows Azure application we build is always associated either with a web role or worker role or both.

Read more here

Windows Azure – New CTP, More fun!

So, Windows Azure SDK was updated in the MIX09 conference and here is what now we developers can take advantage of:

FastCGI allows developers to deploy and run web applications written with 3rd party programming languages such as PHP.  This provides developers using non-Microsoft languages the ability to take advantage of scalability on Windows Azure. 

NET Full Trust  provides developers with a level of flexibility in Windows Azure that removes limitations on .NET Libraries which require full trust (including .NET Services) .NET Full Trust, via spawning process and p/invoke, also allows developers to leverage existing investments in native code or legacy components that they will now be able to invoke on Windows Azure.

Geo-location provides developers with the ability to specify a location for their applications and data to build responsive services with lower network latency as well as the capability to meet location-based regulatory and legal requirements.

That's really cool! Its really nice to see the support for FastCGI applications! :)

What are you still waiting for, grab the SDK bits here!

Windows Azure Table Storage

Windows Azure provides simple data storage services like blobs, tables and queues to store your data in the cloud. They are called as Storage Account

azure-storage

Using Table Storage is one of the simplest way to start exploring the Windows Azure Storage. The tables are accessed using a Uri which is:

http://{application-name}.tables.core.windows.net

Replace the {application-name} with your cloud project name

How is data stored in a table?

Below is a representation of how a table looks like in the ‘cloud’

image

  • Each account has a Table
  • The Table has an Entity
  • An Entity contains Columns
  • Entity can be considered to be the row and Columns as values
  • An Entity always contains these properties:
    • PartitionKey
    • RowKey
    • Timestamp
  • The PartitionKey and RowKey identify a row or an Entity

Programming Windows Azure Tables

Not so fast again :)

You need to have a Storage Account if you want to use the ‘cloud’ table storage. But the Windows Azure SDK comes with a Development Storage tool which simulates the Windows Azure Storage in your local machine! You can very well use that to see how things are worked out in the ‘cloud’. If you are using the local development storage, the tables Uri would be:

http://127.0.0.1:10002

If you need ‘cloud’ storage, you have to register for the ‘Azure Services Invitations” program at Microsoft Connect and wait for your invitation code :)

In my next post, lets see how we can program Windows Azure Table Storage

Windows Azure Tables : Going ‘cloud’ from local storage

image

If you want to take your applications to the ‘cloud’ from the local storage, that is, from http://127.0.0.1:10002 to http://{application-name}.table.core.windows.net/ , all you need to do is edit your ServiceConfiguration.csfg file of your Web Role with the proper values!

<?xml version="1.0"?>
<ServiceConfiguration serviceName="RdChat" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration">
  <Role name="WebRole">
    <Instances count="1"/>
    <ConfigurationSettings>
      <Setting name="AccountName" value="<account-name>"/>
      <Setting name="AccountSharedKey" value="<primary-key>"/>
      <Setting name="UsePathStyleUris" value="false" />
      <Setting name="TableStorageEndpoint" value="http://table.core.windows.net"/>
    </ConfigurationSettings>
  </Role>
</ServiceConfiguration>

 

Hit F5, Tables will be created for you in the ‘cloud’ :)

The things to keep in mind here are:

  1. The <account-name> must be in lowercase
  2. UsePathStyleUris should be set to false when you point to the ‘cloud’

Using Cardspace authentication with .Net Services

If you want to use CardSpace authentication in your .Net services, then all you need to do is don’t add any authentication type to your binding ;)

Confused? Well, .Net Services by default uses CardSpace Authentication. So, all you need to supply is just your solution name and automatically you will be prompted for CardSpace authentication!

Here is my Service code

static void Main(string[] args){    Console.WriteLine("Enter your solution name : ");    string solutionName = Console.ReadLine();                    string serviceUri = String.Format("sb://{0}/services/{1}/HelloWorldService/",                                 ServiceBusEnvironment.DefaultRelayHostName,                                 solutionName);    ServiceHost serviceHost = new ServiceHost(typeof(HelloWorldService), new Uri(serviceUri));    serviceHost.Open();    Console.WriteLine("Service started - Press ENTER to stop");    Console.Read();    serviceHost.Close();}

 

And my config

<system.serviceModel>     <services>          <service name="Chaks.Samples.MyService.HelloWorldService">      <endpoint name="HelloWorldRelayEndpoint"                contract="Chaks.Samples.MyService.IHelloWorldService"                binding="netTcpRelayBinding"/>    </service>  </services></system.serviceModel>

 

But, if you do need to explicitly tell the Service Bus to use CardSpace Authentication, then you need to add an endpoint behavior which uses CardSpace authentication type to your config

<system.serviceModel>     <services>          <service name="Chaks.Samples.MyService.HelloWorldService">      <endpoint name="HelloWorldRelayEndpoint"                contract="Chaks.Samples.MyService.IHelloWorldService"                behaviorConfiguration="CardspaceCredentialsBehavior"                binding="netTcpRelayBinding"/>    </service>  </services>  <behaviors>    <endpointBehaviors>      <behavior name="CardspaceCredentialsBehavior">        <transportClientEndpointBehavior credentialType="CardSpace"/>      </behavior>    </endpointBehaviors>  </behaviors></system.serviceModel>

 

Programming Microsoft .Net Services

If you are not familiar with Microsoft .Net Services, do read my earlier post

So, are you ready to start programming .Net Services ? Not so fast, there are still certain things that you need to know before you start coding ;)

Microsoft .Net Services Account

First you need a Microsoft .Net Services account to actually host and create services that interact with the Service Bus. You can go to Microsoft Connect and apply for the ‘Azure Services Invitations’. Once you register, you will be put in the application pool and if you are lucky, you might get a token immediately for Microsoft .Net Services.

Once you have the token, proceed here to enter the invitation code. Activate the .Net Services and create a solution. Your solution name/password will be used for authentication when you host your services with Microsoft .Net Services. You can also associate Windows Cardspace or a X.509 Certificate for your solution.

azure-services-credentials

Finally, download the .Net Services SDK here

Connection Modes

Service Bus supports three types of connection modes:

Relayed

image

1) Uses relay between server and client.

2) This is the default connection mode

Direct

image

1) Direct connection between server and client, though a relayed connection is used for initialization

2) If Direct connection is not possible, then relay will be used

Hybrid

image

1) Combination of Relay and Direct

2) Establishes a relayed connection, but uses direct connection whenever possible

Service Bus Bindings

The Service Bus is entirely built on Windows Communication Foundation (WCF) . If you are not familiar with WCF, then you could start here. To build a simple ‘Hello World’ service, all you need to do is to create a WCF Service and WCF Client with a ‘specific’ service bus binding. The bindings currently available are:

  • WebHttpRelayBinding
  • BasicHttpRelayBinding
  • BasicHttpRelayContextBinding
  • WSHttpRelayBinding
  • WS2007HttpRelayBinding
  • WS2007FederationHttpBinding
  • NetTcpRelayBinding
  • NetOnewayRelayBinding
  • NetEventRelayBinding

‘Hello World’

Basic steps to write a ‘Hello World’ sample are:

1) Create a WCF Service (Service Contract)

2) Create a Service Bus binding

3) Host in the Service Bus (by authenticating yourself)

4) Write a basic client

5) The client also needs to authenticate with the Service Bus to interact with your service

Building the ‘Hello World’ Service

1) Create your Service Contract

[ServiceContract]public interface IHelloWorldService{    [OperationContract]    string HelloWorld();}

 

2) Implement your Service

[ServiceBehavior(Name = "HelloWorldService",     Namespace = "http://Chaks.Samples.MyService/HelloWorld/")]public class HelloWorldService : IHelloWorldService{    #region IMyService Members    public string HelloWorld()    {        return "[Service returns] Hello World";    }    #endregion}

3) Edit your app.config to hold the WCF endpoints

<?xml version="1.0" encoding="utf-8" ?><configuration>  <system.serviceModel>    <services>      <service name="Chaks.Samples.MyService.HelloWorldService">        <endpoint name="HelloWorldRelayEndpoint"                  contract="Chaks.Samples.MyService.IHelloWorldService"                  binding="netTcpRelayBinding"/>      </service>    </services>  </system.serviceModel></configuration>

The binding we are using here netTcpRelayBinding and the connection mode is the default, which is Relay.

4) You can now host your service. To do so,

a. Enter your solution name and password

Console.WriteLine("Enter your solution name : ");string solutionName = Console.ReadLine();Console.WriteLine("Enter your solution password : ");string solutionPassword = ReadPassword();

b. Create the Service Bus URI

string serviceUri = String.Format("sb://{0}/services/{1}/HelloWorldService/",                             ServiceBusEnvironment.DefaultRelayHostName,                             solutionName);

c. Specify the Transport Credentials

TransportClientEndpointBehavior endpointBehavior = new TransportClientEndpointBehavior();endpointBehavior.CredentialType = TransportClientCredentialType.UserNamePassword;endpointBehavior.Credentials.UserName.UserName = solutionName;endpointBehavior.Credentials.UserName.Password = solutionPassword;

d. Create your Service Host

ServiceHost serviceHost = new ServiceHost(typeof(HelloWorldService), new Uri(serviceUri));

e. Apply the credentials to the endpoints

foreach (ServiceEndpoint endpoint in serviceHost.Description.Endpoints){    endpoint.Behaviors.Add(endpointBehavior);}

f. Open the Service

serviceHost.Open();

If you have entered the proper credentials, your service will now be hosted at the Service Bus - in the Cloud!

Building the ‘Hello World’ Client

1) Enter your solution name and password

Console.WriteLine("Enter your solution name : ");string solutionName = Console.ReadLine();Console.WriteLine("Enter your solution password : ");string solutionPassword = ReadPassword();

2) Create the Service Bus URI

string serviceUri = String.Format("sb://{0}/services/{1}/HelloWorldService/",                             ServiceBusEnvironment.DefaultRelayHostName,                             solutionName);

3) Specify the Transport Credentials

TransportClientEndpointBehavior endpointBehavior = new TransportClientEndpointBehavior();endpointBehavior.CredentialType = TransportClientCredentialType.UserNamePassword;endpointBehavior.Credentials.UserName.UserName = solutionName;endpointBehavior.Credentials.UserName.Password = solutionPassword;

4) Create our HelloWorld channel

public interface IHelloWorldChannel : IHelloWorldService, IClientChannel { }

5) Create the ChannelFactory

ChannelFactory<IHelloWorldChannel> channelFactory =    new ChannelFactory<IHelloWorldChannel>("HelloWorldRelayEndpoint",                 new EndpointAddress(new Uri(serviceUri)));

6) Apply the credentials to the endpoint and create the service channel

channelFactory.Endpoint.Behaviors.Add(endpointBehavior);IHelloWorldChannel channel = channelFactory.CreateChannel();

7) Open the channel and invoke the HelloWorld() method

channel.Open();Console.WriteLine(channel.HelloWorld());channel.Close();

If everything worked fine, you should get a reply from the Service.

Below is the screenshot of the output from our ‘Hello World’ Service and Client

hello-world-output

You can download the sample below

 

Getting Started with Microsoft .Net Services

Microsoft .Net Services is a part of the recently announced Microsoft Azure Platform. What does Microsoft .Net Services provide ?

Microsoft .Net Services consists of three main components:

  1. Service Bus
  2. Access Control
  3. Workflow Services

They provide a hosting platform where you can develop connected, peer to peer applications that can speak to each other without considering the other complexities such as firewall rules and NAT etc,.

Consider the following diagram:

WebService-Common-Problem

Company A has a web service which is also consumed by Company B. Certainly with all the firewalls and ‘network zones’, this doesn’t look simple to develop. There are at least 3 ‘network zones’ for the web service to cross and get into the cloud and even after reaching Company B, there are at least 2 ‘network zones’ to cross to reach the systems. Depending on the company’s network architecture, these ‘network zones’ can be complex and developers need to write complex code to overcome the firewall and NAT rules. Company B also has to enable firewall rules for the incoming requests/responses from the web service from Company A and vice versa. Certainly this is a complex process!

Microsoft .Net Services addresses the above common problem with the help of Service Bus, which provides a communication infrastructure that developers can make use of when developing such complex applications/services. Service Bus hides the complexity of the firewall/router/NAT and enables to host the service in the cloud. The client can then connect to the service through the Service Bus. However, the messages exchanged in a Service Bus are always authenticated and trusted. The Access Control block provides a Security Token Service (STS) which provides Identity and Management Service. So, the above diagram would now change to,

WebService-servicebus

 

How are things accomplished using this Service Bus? Below is a typical message flow that happens in a Service Bus:

  1. Company A requests a Token by authenticating itself to the STS
  2. When authenticated, STS issues a token to Company A
  3. Company A  can now initiate a connection to it’s Service
  4. Company B requests a Token by authenticating itself to STS
  5. When authenticated, STS issues a token to Company B
  6. Company B can now exchange messages with Company A.

The messages are authenticated and checked for authorization respectively by the STS. This can be described in a diagram as follows:

servicebus-message-flow

In my next post, lets write our first ‘Hello World’ application using Microsoft .Net Services

Windows Azure SDK (October 2008 CTP) : Table Service not starting?

After downloading the Windows Azure SDK (October 2008 CTP), I wanted to create a simple application which can make use of Azure Tables. Using the Development Storage tool, Azure SDK simulates the Azure Tables in your local machine than in the cloud. But, when I started the Development Storage tool, the Table Service failed to start and I was not able to choose a proper Table Service Database. None of the SDK samples which uses the local Table Service worked.

The problem was – The Database (DevelopmentStorageDB) was created in my .\SQLEXPRESS (Azure SDK searches for a .\SQLEXPRESS instance by default) instance, but not the required Schemas!

How do I install the schemas?

Traverse to where you had extracted the SDK Samples and execute the rundevstore.cmd script

azure-rundevstore

This creates all the necessary Schemas and now if you open the Development Storage tool, you could see something like this

azure-development-storage

You are now ready to execute the SDK Samples which interact with the Azure Tables Services and also create your own applications to interact with the Azure Table Service :)

Intergen is the Microsoft Partner of the Year 2008!

Yesterday night was a big night for Intergen - Intergen won the Microsoft Partner of the Year 2008!

Hear it straight from Tony's words :

At the Microsoft Partner Awards ceremony in Auckland, in front of over 300 attendees, Intergen won the premier award of Partner of the Year for 2008. Microsoft said we won the award because of our commitment to delivering solutions to our clients across the entire Microsoft Application Stack. We were recognised as the only partner who can truly offer solutions that span the breadth from .NET development through MOSS, BI, CRM, NAV etc. Special mention was also made of the work we did in support of the Office Open XML standard.


Creative Commons License
Chaks' Corner Blog by Chakkaradeep Chandran is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.
Based on a work at www.chakkaradeep.com.
Permissions beyond the scope of this license may be available at http://www.chakkaradeep.com.