Now that we are successfully writing messages to
our service bus queue with our web role.
Let's update our worker role, so that we can remove messages from that queue.
I'm going to go up to Signupworker, open workerrole.cs.
And then I need to add these server bus
references, just like we did with the web role.
So let's go ahead and make sure that we have that reference.
And we need to check off the 2.0 version,
you may have a different version on your machine.
Make sure that you have the 2.0 version, and then we're going to say using
Microsoft service bus and using Microsoft ServiceBus messaging.
Now, you'll notice, unlike the web roll, we
don't have to add using Microsoft Windows Azure.
Those references were already set up for us in the worker role template.
Let's go down and add two class level
variables just like we did for the web roll.
So we're going to go
ahead and say string, connectionString, CloudConfigurationManager.
Go ahead and get the settings.
And this was the Microsoft ServiceBus, ConnectionString parameter that
we set up earlier inside of our configuration files.
So remember, if we scroll down here, you'll see
for the worker role that's set up with our key.
We want to
make sure that we're using the cloud configuration manager get settings.
So that we can easily switch between local and
cloud environments, staging and production, as well as being secure.
Let's go ahead and create a string, call this the qname,
and our q that we called was signups, and we'll save that.
Now, currently, our worker role is just going through
every ten seconds and saying that it's processing sign ups.
Now it's time to actually have it do something, so let's get rid of sleep.
And we're going to use the queue client just like we did inside of the web roll.
We'll call that qc ,and
we'll say QueueClient.CreateFromConnectionString,
and we're going to
use the ConnectionString we've set up, as well as the qname.
And then we're also going to set up the BrokeredMessage.
So I'm going to
call that message, and we're going to to go ahead and Receive that.
And remember we used the BrokeredMessage class as
well to actually write the message to the queue.
So, if this message is not null, then
let's go ahead and try and do something here.
So we'll say try, and for now, let's just go ahead and write out a
line that says, a New Signup is processed.
And that's going to be part of the brokered message.
It's going to have a property of email, if you recall,
which is what we set inside of the web roll.
And then we're going to mark that as completed.
Now, once we mark that as completed, it
will actually remove that from the queue for us.
And then we'll catch any exceptions that we get.
So this will indicate problem and we're going to
abandon, so this is going to unlock the queue, okay?
That looks good.
Let's go ahead and Save. Now let's Run this.
Oops, we have one error.
there we go. We forgot to close our trace line.
Run this.
Now, we're going to want to open the Compute Emulator UI,
so we can see locally what's actually happening here.
And we're going to look at our signup workers.
So, one of these guys is actually going to pick that up
off the message queue, because remember, the email address already exists.
And most likely
its going to be the one that starts up first.
Let's go ahead and put something new on the queue, just so that we
can go ahead and see this in real time, so we'll say fbennet signup.
That should move to the queue, and then
one of these should pick it up immediately.
And here you go.
So it was VM3 new signup process, and you can see the email address.
Pretty exciting stuff.
We're actually writing to
the cloud through our local cloud service environment.
And we're putting messages on a queue from a web role.
And we're picking them up from a worker role
which actually runs across four different virtual machine incidences.
Next, we're going to go ahead and store that email
address that we get off the queue into table storage.