3

We have an Azure Web Role that needs to monitor a Service Bus Queue for responses to earlier requests (which will then be transmitted to the client via SignalR).

At first we wanted to use a message pump (QueueClient.OnMessage) in the WebRole.OnStart(). However, we have grown to quite like the WebJobs SDK, especially how it frees the programmer of some lower level implementation and the dashboard too.

For various reasons we want to keep the Web Role rather than switch to a WebSite. So a question arises: how to use the WebJobs SDK in an Azure Web Role? In a little experiment we have adapted the Web Role's OnStart() in WebRole.cs, as follows:

public class WebRole : RoleEntryPoint
{
    public override bool OnStart()
    {
        JobHostConfiguration config = new JobHostConfiguration()
        {
            ServiceBusConnectionString = "...", 
            StorageConnectionString = "...",
            DashboardConnectionString = "..."
        };

        JobHost host = new JobHost(config);
        host.RunAndBlock();

        return base.OnStart();
    }

    public static void ProcessQueueMessage([ServiceBusTrigger("MyQueue")] string inputText)
    {
        Trace.WriteLine(inputText);
    }
}

This seems to work fine, but we're having difficulty to assess the impact it has on the Web Role. Could there be consequences? Perhaps in scaling the Web Role?

Thanks.

Victor Hurdugaci
  • 28,177
  • 5
  • 87
  • 103
Egotix
  • 31
  • 1

1 Answers1

3

The WebJobs SDK should work just fine in a WebRole.

I have one suggestion on your implementation: do not block the OnStart method. Instead of calling RunAndBlock, use Start/StartAsync. This will not block that method and will create a separate thread for the job host.

The could would look like (not sure if it compiles):

public class WebRole : RoleEntryPoint
{
   private JobHost host;

   public override bool OnStart()
   {
       JobHostConfiguration config = new JobHostConfiguration()
       {
           ServiceBusConnectionString = "...", 
           StorageConnectionString = "...",
           DashboardConnectionString = "..."
       };

       host = new JobHost(config);
       host.Start();

       return base.OnStart();
   }

   // Not sure if this is the signature of OnStop
   // or even if this method is called this way
   public override bool OnStop()
   {
       host.Stop();
       return base.OnStop();   
   }

   public static void ProcessQueueMessage([ServiceBusTrigger("MyQueue")] string inputText)
   {
       Trace.WriteLine(inputText);
   }

}

Victor Hurdugaci
  • 28,177
  • 5
  • 87
  • 103
  • Thanks, this works fine to monitor the queue. However, because we adapted the WebRole.cs, queue messages are now received in a different process than the web application (from where SignalR can notify the clients). Should we make the first process notify the second? How? Or just move the webjob to Global.asax or OWIN's Startup.cs? – Egotix Oct 28 '14 at 08:59
  • You can move the web job inside the web app. That shouldn't be an issue. The only concern is that by default IIS will stop the website (and the webjob) is there are no requests. You need some sort of always on mode to keep the webjob alive – Victor Hurdugaci Oct 28 '14 at 15:56
  • You can also self host SignalR in the worker role and invoke it from the WebRole – pranav rastogi Nov 02 '14 at 22:30