When
it’s all said and done, working with worker roles is quite easy. The
core of the code for the worker role is the normal business code that
gets the work done. There isn’t anything special about this part of a
worker role. It’s the wrapper or handler around the business code that’s
interesting. There are also some key concepts you’ll want to pay
attention to, in order to build a reliable and manageable worker role.
In this section, we’ll show you
how to build a basic worker role service. You have to have some way to
communicate with the worker role, so we’ll first send messages to the
worker through a queue, showing you how to poll a queue. We’ll then upgrade the service so you can use inter-role communication to send messages to your service.
We’ll use the term service
fairly loosely when we’re talking about worker roles. We see worker
roles as providing a service to the rest of the application, hopefully
in a decoupled way. We don’t necessarily mean to imply the use of WS-*
and Web Service protocols, although that’s one way to communicate with
the role.
Let’s roll up our sleeves and
come up with a service that does something a little more than return a
string saying “Hello World.” In the next few sections, we’ll build a new
service from scratch.
15.1.1. No more Hello World
Because
Hello World doesn’t really cut it as an example this late in any book,
we’re going to build a service that reverses strings. This is an
important service in any business application, and the string-reversal
industry is highly competitive.
There will be two parts to
this service. The first part will be the code that actually does the
work of reversing strings—although it’s composed of some unique
intellectual property, it isn’t very important in our example. This is
the business end of the service. The other part of the service gets the
messages (requests for work) into the service. This second part can take
many shapes, and which design you use comes down to the architectural
model you want to support. Some workers never receive messages; they
just constantly poll a database, or filesystem, and process what they
find.
To build this
string-reversal service you need to open up Visual Studio 2010 and start
a new cloud project. For this project, add one worker role, and give it
the name Worker-Process String, as shown in figure 1.
At the business end
will be our proprietary and award-winning algorithm for reversing
strings. We intend to take the string-reversal industry by storm and
really upset some industry captains. The core method will be called ReverseString,
and it will take a string as its only parameter. You can find the
secret sauce in the following listing. Careful, don’t post it on a blog
or anything.
Listing 1. The magical string-reversal method
private string ReverseString(string originalString) { int lengthOfString = originalString.Length; char[]reversedBuffer = new char[lengthOfString];
for (int i = 0; i < lengthOfString; i++) { reversedBuffer [i] = originalString[lengthOfString - 1 - i]; }
return new string(reversedBuffer); }
|
The
code in the previous listing is fairly simple—it’s normal .NET code
that you could write on any platform that supports .NET (mobile,
desktop, on-premises servers, and so on), not just for the cloud. The
method declares a character array to be a buffer that’s the same length
as the original string (because our R&D department has discovered
that every reversed string is exactly as long as the original string).
It then loops over the string, taking characters off the end of the
original string and putting them at the front of the buffer, moving
along the string and the buffer in opposite directions. Finally, the
string in the buffer is returned to the caller.
For this example, we’ll put this business logic right in the WorkerRole.cs
class. Normally this code would be contained in its own class, and
would be referenced into the application. You can do that later if you
want, but we want to keep the example simple so you can focus on what’s
important.
We’ve chosen to put this
service in a worker in the cloud so that we can dynamically scale how
many servers we have running the service, based on usage and demand. We
don’t want to distract our fledgling company from writing the next
generation of string-reversal software with the details and costs of
running servers.
If you ran this project
right now, you wouldn’t see anything happen. The cloud simulator on
your desktop would start up, and the worker role would be instantiated,
but nothing would get done. By default, the worker service comes with an
infinite polling loop in the Run method. This Run method is what is called once the role instance is initialized and is ready to run. We like that they called it Run, but calling it DoIt would have been funnier.
Now that you have your code
in the worker role, how do you access it and use it? The next section
will focus on the two primary ways you can send messages to a worker
role instance in an active way.