We now have a working Cloud service with a web roll that's writing to a service
bus message queue, and a worker roll that's
reading from that same queue and processing messages.
For the last piece, what we need to do is
update the worker roll to actually save to Azure storage.
And we're going to use table storage.
So let's go back to the portal.
And we're going to hit New, and under Data Service, we're going to create
a new storage account. We'll click Quick Create.
We're going to call this University Storage.
We're going to enable geo-replication, so remember, geo-replication, which is on
by default, will make sure that all of our data gets
written three times, and it also geo-replicated across data centers,
so the default fall back, which we saw before, was West.
For West US was East US. I'm going to leave that on.
And you can see univstorage
is all set here.
So let's go ahead and create storage account.
Everything is set up and online.
If I click on univstorage, you'll see our dashboard.
Geo replication is enabled.
And we have blog, table, and cue storage. Let's go back over to visual studio.
Click on our database explorer and underneath
our storage account let's go ahead and refresh.
We're going to dd manually enter credentials.
And for Univ Storage, I'm going to
get my access keys.
So we'll see here storage account name is Univ Storage, and our primary access key,
(SOUND)
we're going to paste that in. Make sure that HTTPS is checked off.
And you'll see here we now have
our storage account with blobs, queues, and tables.
We're actually going to use tables for this exercise.
So now that we have our storage account set up, let's go back into our
worker role settings over here under our cloud
service.e So double click on sign ups worker.
And we're going to go under Settings, and for
all configurations, just like we did
before, we're going to create a new setting.
I'm going to call this our table storage connection.
Now this is actually a connection string.
So because it's a connection string, this little ellipse here is going
to bring up a wizard where we can manually enter our credentials.
These are going to be the same credentials we just did inside the data explorer.
I'm going to paste my account key here,
and copy over my account name here. And then hit Okay.
Let's save that.
And this will set up a new connection
string so that we can use our table storage.
Let's go ahead back over to our workerrole.cs file now.
We're going to add a new class level variable.
We're going to call this string tableConnectionString.
And just like for our configurations before, we're going to go ahead and we're
going to use the cloud configuration manager.
So we're going to call that, get settings.
And then, if you recall, we called it Table Storage Connection.
This will set up a variable to our Table
Storage, so that we can read and write to it.
Let's go ahead and save. Let's right click on our worker roll here.
We're actually going to say Manage NuGet Packages.
This will make sure that you're
always bringing down the latest Azure storage NuGet Package.
So if I go in here and I hit update, you can see I
have a new Windows Azure storage, as
well as a new Windows Azure configuration manager.
And you'll see this was published fairly recently, on July 2, 2013.
Look at the number of downloads.
Almost 435,000, so you can see just the popularity of Azure here.
We're going to go ahead and
say, update all of those things.
And just like all the NuGet packages, it's super easy to use.
We just accept the agreement, It's going to pull
down all the latest DLLs and everything for us.
I'm going to hit close.
And now that we've updated all of our references
with the new get we need to come down
and after our message complete call, we're going to
add a new method we'll create called save to storage.
So this is basically to log to table
storage and I am going to call that method
Save To Storage basically we are going to put
the message properties or the email and we are going to send it off to that table
storage, so we need to cast this to a string and before I implement that method
We need to actually create a new class
for what we'll be using inside save to storage.
So what I want to do is I want to right click on the sign up worker.
I want to say
add, new class. I'm going to call this a person class.
Now at the top, we need to go ahead and add a reference
to Microsoft Windows Azure, and there's a storage client.
You'll see that over here.
Version 1.7. I'm going to say okay.
We're going to say using Microsoft Windows Azure storage client.
And we're going to make this person actually
inherit from something called a table service entity.
This will allow us to set up a class that can write directly to table storage.
And,
I'm going to call this, instead of person, I think I'm going to call
this the person entity, so that we better reflect the name of what we're inheriting.
I'm only going to have one property.
I'm going to make that a string and we'll call that email.
We'll do some getters and setters here.
What this will allow us to do is have a person
entity class that we can use to write directly to table storage.
Let's go back over to the worker role.
We need to uncomment the
Windows Azure storage, and we're going to be using the
same client that we were inside of our person entity.
So we're going to be using the Microsoft Windows Azure storage client.
And now we need to implement Save to storage.
So let's go ahead and put that down here at the bottom.
I'm going to make it private, nothing to return, save to storage.
I'm gona take on argument, for e-mail, and we need a table name.
If you recall our table name was actually signups, and
then we need to create a new Cloud storage account.
I'm going to call this, just account.
And we're going to parse that table connection string that we set up top.
Next, I need to create a client for the table storage.
So we're going to create a CloudTableClient.
I'm going to call this tablestorage.
And we're going to
call a method on the account cloud storage account variable we've created.
And we're going to use Create Cloud Table Client, no parameters.
Now that we've got our table storage cloud table client set up,
we need to go ahead and call in to the create table.
Now, there's two methods here.
You can create a table, or you can create if none exists.
I'm always a fan of checking to make sure
if it exists, and if it doesn't, create it.
So we'll go ahead and say,
CreateTableIfNotExist, pass it the table name.
Now we need to create that person entity, and then set its email.
So let's create something called a table service context.
I'm going to call this just context.
And this is going to come from table storage.
Get the data service context with no parameters.
So this will actually give us the context of that table.
And so now let's go ahead and create
a new person entity, we'll call it person, this is the class that we just set up.
And that person is going to have an e-mail, and
that e-mail is what we're passing into the newsletter.
And then what we need to do, is we actually need
to set what its partition key is So we're going to say signups.
Now for table storage, you can think of partition
key and row key as basically a compound primary key.
And this is going to allow us up to scale really,
really big when it comes to reads.
So we're going to partition the data across that partition key, and
then we're going to have uniqueness between the rows and the partition key.
So I need to also set the person's row key and we're going to make that email.
So the compound key here is look for sign ups plus that email.
And then the last piece that we need to do is actually save this out.
So we're going to save this new
person to the table and I'll say context
Add an object, that objects going to take two parameters.
So the first is going to be the name of the
table and then the second is going to be the object.
So we're going to send it it's person entity.
So this is the really cool part here, is that we are working with native objects
We're using a person entity .net object, but
because it inherited from that table entity, we could
actually just write an object to the table out directly.
Azure is going to handle everything else underneath the
cover, so let's go ahead and save those changes.
We're going to go ahead and run this, so let's put a debug point
right here when we create the table, just so we see everything happen.
And this is where it all comes together.
So we're going to have a cloud service
that's running in our local compute environment.
It's got five virtual machines.
One for the web role. Four for the worker role.
We're going to have an asp.net MVC4 site.
We're going to accept an email.
We're going to have a worker role that will come along and read
that email from a service bus queue, which our web role will actually place
the message on, and then that worker role will remove the message from the
queue, and it'll actually save it back up to the cloud in table storage.
So you can see the scenario happening in a real scenario
on a website where you may have numerous people at the
same time trying to sign up, and we want the availability
to scale out on a back end across all these different servers.
So I've got breakpoints here.
You can see table names, sign ups.
You can see emails coming across live, this person, entity.
We go ahead and we step over this.
You'll see this has actually been set
up with email property. So, it knows all about that.
Let's step over.
Our table name looks correct with signups, and our person is now
set up with that correct email that came across from the web role.
We're going to go ahead and continue running this,
and you can see we get our nice page.
So if we come back here, into our portal, and we
come up to, university storage, you can see everything is set up.
And then if we move over to our service
bus, we should not see anything on our queues.
Let's click on here and see queues and you can see zero messages.
And then let's make sure that that's actually been written to table
storage, so we're going to come over here, go into data explorer.
You can see university storage and you can see tables.
Go ahead and pull all that information.
Look at that, the sign ups table has been created.
And underneath here is
our partition key of sign ups, our row
key and then you can actually see email address.
Congratulations, you've now created a working Cloud service
that uses a service bus queue, has web
roles, and worker roles, and then can easily
scale up or down as your needs depend.