Search code examples
asp.netsignalrazure-web-rolessignalr-hub

SignalR IHubContext in WebRole Task


I am testing a SignalR application that will run in an Azure Web Role. I have a background processing thread, created in the OnStart of the WebRole.

When I try to use IHubContext to send a group message, it fails with no errors - the message is never received in the hub client. The same code created from inside Global.asax will function correctly.

Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports Microsoft.WindowsAzure
Imports Microsoft.WindowsAzure.Diagnostics
Imports Microsoft.WindowsAzure.ServiceRuntime
Imports Microsoft.AspNet.SignalR
Imports System.Threading.Tasks

Public Class WebRole
    Inherits RoleEntryPoint

    Public Overrides Function OnStart() As Boolean

        Task.Factory.StartNew(Sub() AsyncTaskTest())

        Return MyBase.OnStart()
    End Function

    Private Sub AsyncTaskTest()

        Do
            Threading.Thread.Sleep(10000)

            Dim Context As IHubContext = GlobalHost.ConnectionManager.GetHubContext(Of Testub)()
            Context.Clients.Group("testgroup").Message("This is a delayed message from the WebRole thread.")
        Loop

    End Sub

End Class

Should it be possible to use IHubContext in this way? Does this count as a separate assembly, and would require use of a HubConnection? If I can't use IHubContext I will revert to using this inside Global.asax and keeping the web application alive.

Thanks, Daniel


Solution

  • I think, this has to do with the webrole OnStart method and the web applications being two different processes.