Juan
Juan

Reputation: 23

Azure: How to call a WebRole method from a controller of my MVC implementation

I'm working on a MVC webapp using Azure with ASP.Net MVC 2 C#. I have a worker role from where I interact with a queue reading messages with images. I initialize the queue in my WebRole, and I want to call a method to enqueue elements from my controller. I don't know how to do this call.

Thanks!

Upvotes: 0

Views: 515

Answers (1)

David Makogon
David Makogon

Reputation: 71055

Adding to the queue is straightforward:

var queueClient = CloudStorageAccount.FromConfigurationSetting("mystorage").CreateCloudQueueClient();
var myQueue = queueClient.GetQueueReference("myqueue");
string myMessageContent = "Some formatted queue message"; // this could be bytes as well
var myQueueMessage = new CloudQueueMessage(myMessageContent);
myQueue.AddMessage(myQueueMessage);

One bit of advice: When creating the queue, do it in your role's OnStart(), not in the Run(). This way, it'll be created before your web app ever shows up in the Azure load balancer.

Upvotes: 2

Related Questions