Ezony
Ezony

Reputation: 151

Service bus + webjob in MVC. Sending string as Message

I'm trying to learn using Service Bus with Webjob. I'm using 3 projects, one Writer, Webjob, and Reader. I want to send message via service bus when passing on. I'm trying to send a message from Write-project, pass it to WebJob and convert text to uppercase, and then pass it on to Reader via queue again.

I got all these 3 projects in same solution (using visual studio).

This is how my WebJob's function.cs looks like:

namespace WebJob {
    public class Functions {

        public static void ProcessQueueMessage([ServiceBusTrigger("test")] BrokeredMessage message, TextWriter log)
        {
            var text = message.GetBody<string>().ToUppder();

            Console.WriteLine(text);
            log.WriteLine(text);
        }
    }
}

Connectionstring, and other stuff on azure are settled. But I have no clue how to send text from WriterController in Writer project to WebJob(convert message to uppercase) and pass it on to ReaderController and display it in Reader project.

I got no code for controllers yet as I've no clue how to send it on..

How do I send message from Writer to Reader in this way?

Upvotes: 1

Views: 1230

Answers (1)

Fei Han
Fei Han

Reputation: 27825

I'm using 3 projects, one Writer, Webjob, and Reader. I want to send message via service bus when passing on. I'm trying to send a message from Write-project, pass it to WebJob and convert text to uppercase, and then pass it on to Reader via queue again.

It seems that you’d like to create Service Bus queue messages from ‘Write’ project, and then a WebJob that could be triggered by Service Bus will convert message content to uppercase and insert the message to Azure storage queue, and then ‘Read’ project will display the message from Azure storage queue. If that is the case, you could refer to the following steps and code to design your project.

In your WriterController action method, you could create Service Bus queue messages.

var connectionString = "yourconnectionstring";
var queueName = "yourqueuename";

var client = QueueClient.CreateFromConnectionString(connectionString, queueName);

string mes = "your queue message";
var message = new BrokeredMessage(mes);

client.Send(message);

//redirect to another page to display message
//Thread.Sleep(2000);

//return Redirect("http://xxxx/{YourReaderControllerName}/ReadMessage");

In your WebJob function, you could convert the Service Bus queue message to uppercase and insert it as an Azure storage queue message while processing a Service Bus queue message.

public static void CreateQueueMessage([ServiceBusTrigger("servicebusqueuename")] string message, [Queue("azurestoragequeuename")] out string outputQueueMessage, TextWriter log)
{
    var text = message.ToUpper();

    outputQueueMessage = text;

    log.WriteLine(message);

}

In your ReaderController action method, you could get message from Azure storage queue.

CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
                        CloudConfigurationManager.GetSetting("StorageConnectionString"));

CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();
CloudQueue queue = queueClient.GetQueueReference("azurestoragequeuename ");
CloudQueueMessage retrievedMessage = queue.GetMessage();

string qmes = retrievedMessage.AsString;

//Process the message, and then delete the message
//queue.DeleteMessage(retrievedMessage);

edit:

Is it possible to send via another Service Bus queue2 from WebJob to WriterController instead? If so, would you mind modify it from Storage queue to Service Bus queue ?

You could refer to this code snippet to create a new message in another Service Bus queue named " queue2" when WebJob process a Service Bus queue message.

public static void CreateQueueMessage([ServiceBusTrigger("myqueue")] string message, [ServiceBus("queue2")] out string outputQueueMessage)
{
    outputQueueMessage = message.ToUpper();

    //Console.WriteLine(message);
}

Upvotes: 2

Related Questions