Skip to content

Instantly share code, notes, and snippets.

@clemensv
Created May 10, 2012 19:34
Show Gist options
  • Save clemensv/2655319 to your computer and use it in GitHub Desktop.
Save clemensv/2655319 to your computer and use it in GitHub Desktop.
Message Correlation - Worker Role Side
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BackgroundWorker
{
using System.Threading;
using Microsoft.ServiceBus;
using Microsoft.ServiceBus.Messaging;
class MessageCorrelationWorker
{
static MessagingFactory messagingFactory;
static ManualResetEvent resetEvent;
public static void Run(WorkerRole role, ManualResetEvent manualResetEvent)
{
resetEvent = manualResetEvent;
messagingFactory = MessagingFactory.Create(
ServiceBusEnvironment.CreateServiceUri("sb", role.ServiceBusNamespace, String.Empty),
TokenProvider.CreateSharedSecretTokenProvider(role.ServiceBusAccount, role.ServiceBusKey));
var queueClient = messagingFactory.CreateQueueClient(WorkerRole.MessageCorrelationQueueName, ReceiveMode.ReceiveAndDelete);
queueClient.BeginReceive(MessageCorrelationReceiveLoop, queueClient);
}
static void MessageCorrelationReceiveLoop(IAsyncResult ar)
{
var queueClient = ar.AsyncState as QueueClient;
try
{
var request = queueClient.EndReceive(ar);
if (request != null)
{
var replyQueueClient = messagingFactory.CreateQueueClient(request.ReplyTo);
double result = 0d;
var operation = request.Properties["Operation"] as string;
if (operation == "add" || true)
{
result = (double)request.Properties["FirstArgument"] + (double)request.Properties["SecondArgument"];
}
var reply = new BrokeredMessage
{
CorrelationId = request.MessageId,
Properties = {
{ "JobId", request.Properties["JobId"]},
{ "Result", result }
}
};
replyQueueClient.Send(reply);
replyQueueClient.Close();
}
}
catch (Exception)
{
// log the exception
}
queueClient.BeginReceive(MessageCorrelationReceiveLoop, queueClient);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment