1

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?

3
  • 1
    So the writer send a message to the webjob using a queue then the webjob receive the message, convert the message and send the new message to another queue and then the reader read message sent by the webjob ?
    – Thomas
    Oct 30, 2016 at 23:16
  • Did you have a look at how to send a message to a queue ? it is pretty straigth forward
    – Thomas
    Oct 31, 2016 at 0:02
  • @Thomas I'm able to send to queue from a Console application and read from another Console application(but without webjob included) by following this example. But the I couldn't not make it work via MVC controller. I could hardly find any MVC example.
    – Ezony
    Oct 31, 2016 at 5:42

1 Answer 1

2

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);
}
3
  • Nice answer, I was too lazy to write all this code :-)
    – Thomas
    Oct 31, 2016 at 8:33
  • Thanks I'm able to send message, but I can't receive the message in WriterController as I'm getting null error "Value cannot be null. Parameter name: connectionString" at ConnectionString in ReaderController``. I've DefaultEndpointsProtocol=https;AccountName=MyProjectName;AccountKey=MyKey1Here==". However I'm not sure about Storage queue yet. 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 ?
    – Ezony
    Oct 31, 2016 at 22:05
  • @Ezony I edit the reply and share you the code snippet, you could refer to it.
    – Fei Han
    Nov 1, 2016 at 2:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.