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
{
if (RoleEnvironment.IsAvailable && RoleEnvironment.CurrentRoleInstance != null)
{
return Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.CurrentRoleInstance;
}
return null;
}
}
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)
{
int instanceIndex = 0; string instanceId = CurrentRoleInstance.Id;
if (!int.TryParse(instanceId.Substring(instanceId.LastIndexOf(".") + 1), out instanceIndex))
{
int.TryParse(instanceId.Substring(instanceId.LastIndexOf("_") + 1), out instanceIndex);
}
_subscriptionName = String.Format("{0}_{1}", CurrentRoleInstance.Role.Name, instanceIndex);
}
}
}
And it works. Hope this helps.
source
share