How to get an ASP.NET MVC application to read from the Azure Service Bus service queue?

I have a working role, running on windows azure, which generates messages. I have an ASP.NET MVC application that has a SignalR hub. I would like to send messages from the working role to the SignalR hub, which will then push them to the connected clients in real time. My thoughts are to use the Azure Service Bus Queue from which the ASP.NET MVC application will be read. All this seems understandable enough for the concept, but I'm not sure how to connect the QueueClient service bus in an MVC application. I can find many patterns with ASP.NET MVC by queuing messages that will be picked up by the worker role service, but not vice versa.

Question : Is there a way to do this? Can someone point me towards some samples, etc.?

+3
source share
3 answers

In our project, we use this in MvcApplication> Application_Start:

new Thread(t => Container.Resolve<IMessageRouter>().StartRouting()).Start();

We also use DI (Windsor container), the router is registered as Transient. A router, starting with a web application in parallel flow and working all the time.

The router has a connection context for the SignalR hub and a theme listener (theme client wrapper):

public class MessageRouter : IMessageRouter
{
    private string _subscriptionName = "Subscription_0";

    private IHubConnectionContext ClientContext
    {
        get { return GlobalHost.ConnectionManager.GetHubContext<MessageHub>().Clients; }
    }

    private RoleInstance CurrentRoleInstance
    {
        get // try to get current Azure role instance
        {
            if (RoleEnvironment.IsAvailable && RoleEnvironment.CurrentRoleInstance != null)
            {
                return Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.CurrentRoleInstance;
            }

            return null; // return null if not Azure environment
        }
    }

    private string TopicName
    {
        get { return ConfigurationHelper.TopicName; }
    }

    private string TopicConnectionString
    {
        get { return ConfigurationHelper.TopicConnectionString; }
    }

    public ITopicListener TopicListener { get; set; }

    public void OnMessage(QueueMessage message)
    {
        ClientContext.Group(message.GetRecipientGroup()).updateProgress(message.GetBody<string>());
    }

    public void StartRouting()
    {
        TopicListener.Bind(TopicConnectionString, TopicName, _subscriptionName).StartListening(OnMessage);
    }

    public MessageRouter()
    {
        if (CurrentRoleInstance != null) // check Azure environment is exist
        {
            int instanceIndex = 0; string instanceId = CurrentRoleInstance.Id;

            if (!int.TryParse(instanceId.Substring(instanceId.LastIndexOf(".") + 1), out instanceIndex)) // on cloud
            {
                int.TryParse(instanceId.Substring(instanceId.LastIndexOf("_") + 1), out instanceIndex); // on compute emulator
            }

            _subscriptionName = String.Format("{0}_{1}", CurrentRoleInstance.Role.Name, instanceIndex);
        }
    }
}

And it works. Hope this helps.

+2
source

Read “How to receive messages from the queue” at http://www.windowsazure.com/en-us/documentation/articles/service-bus-dotnet-how-to-use-queues/

, , SignalR, SignalR - . : SignalR 2.0

+1
 //Fetch connection string from config  
        string connectionString = ConfigurationManager.AppSettings["connectionString"];
        // Create the connection 
        MessagingFactory factory = MessagingFactory.CreateFromConnectionString(connectionString);
        //Provide the queue name in the service bus
        MessageReceiver receiver = factory.CreateMessageReceiver("queue Name");
        BrokeredMessage retrievedMessage = receiver.Receive();
        if (retrievedMessage != null)
        {
            try
            {
                string output = retrievedMessage.GetBody<string>();
                //In the output string you will get the message from the queue.
                retrievedMessage.Complete();
            }
            catch (Exception ex)
            {
                retrievedMessage.Abandon();
            }
        }
-1
source

All Articles