locked
SignalR messages from a scheduled job not working when site is deployed RRS feed

  • Question

  • User-1158769213 posted

    I am building a web site using ASP.NET Core 2.2, and SignalR.

    The prime use of SignalR is to send out notifications when donations are submitted. I have a scheduled job running Quartz.net that sends out a notification with the latest donations.

    In the scheduled job, I inject an instance of IServiceScopeFactory and use it to create an IHubContext<CampaignHub> as follows (irrelevant code omitted for clarity)...

    public class DonationsNotifierJob : IJob {
      private readonly IServiceScopeFactory _scopeFactory;
    
      public DonationsNotifierJob(IServiceScopeFactory scopeFactory) =>
        _scopeFactory = scopeFactory;
    
      public async Task Execute(IJobExecutionContext context) {
        using (IServiceScope scope = _scopeFactory.CreateScope()) {
          IHubContext<CampaignHub> hubContext = scope.ServiceProvider
            .GetRequiredService<IHubContext<CampaignHub>>();
          await hubContext.Clients.All
            .SendAsync("UpdatedCampaignData", new LiveCampaignData {
              // Data loaded here...
            });
        }
      }
    }
    

    The Razor for one of the pages that is supposed to pick up the notifications includes the following JavaScript...

      var connection = new signalR.HubConnectionBuilder()
        .withUrl("/campaignHub")
        .build();
      connection.start();
    
      connection.on("UpdatedCampaignData", function(d) {
        // Code omitted for clarity
      });
    

    This works fine when I'm running in Visual Studio (2017 Enterprise), but when I deploy the site to a test server, the page that contains the JavaScript above never receives notifications.

    I tried adding the usual "chat room" code you see in the samples, and that worked fine, so SignalR is working to some degree. Not only that, but one of the pages that sends out a SignalR notification directly (using the same hub method as shown above) works fine, so it seems to be a problem only in the scheduled job.

    Injection is working fine, as I don't get any null reference exceptions.

    I really have no idea how to start debugging this. It's the same code running in both cases, and I can see from the logging that the line in the scheduled job where the notification is sent is called, but I have no idea what happens to it then. I haven't found any way to tell if the notification isn't being sent, or if it is being sent but the target page doesn't pick it up.

    Anyone able to give me any idea how I find out what's going wrong? Sorry if there isn't enough info here, but I don't know what else to tell you.

    Thanks

    Sunday, June 23, 2019 10:19 PM

All replies

  • User61956409 posted

    Hi Yossu,

    The prime use of SignalR is to send out notifications when donations are submitted. I have a scheduled job running Quartz.net that sends out a notification with the latest donations.

    Please share the code snippet that you are using to add Quartz services and job in Startup.ConfigureServices(), so that we can reproduce and troubleshoot the issue better.

    With Regards,

    Fei Han

    Monday, June 24, 2019 7:20 AM
  • User-1158769213 posted

    Hello, thanks for the reply.

    Here is the code...

          services.AddSingleton<IJobFactory, JobFactory>();
    services.AddSingleton<ISchedulerFactory, StdSchedulerFactory>(); services.UseQuartz(typeof(DonationsNotifierJob));

    The JobFactory class looks like this...

      public class JobFactory : IJobFactory {
        private readonly IServiceProvider _serviceProvider;
    
        public JobFactory(IServiceProvider serviceProvider) =>
          _serviceProvider = serviceProvider;
    
        public IJob NewJob(TriggerFiredBundle bundle, IScheduler scheduler) =>
          _serviceProvider.GetRequiredService(bundle.JobDetail.JobType) as IJob;
    
        public void ReturnJob(IJob job) {
          if (job is IDisposable disposable) {
            disposable.Dispose();
          }
        }
      }

    Hope that helps. Thanks again.

    Monday, June 24, 2019 1:25 PM
  • User61956409 posted

    Hi Yossu,

    I did a test with the code snippet of DonationsNotifierJob that you provided to push LiveCampaignData (using testing data, like below) to connected clients on a schedule, which work well on both local and production server.

    public async Task Execute(IJobExecutionContext context)
    {
        using (IServiceScope scope = _scopeFactory.CreateScope())
        {
            IHubContext<CampaignHub> hubContext = scope.ServiceProvider
                .GetRequiredService<IHubContext<CampaignHub>>();
    
            await hubContext.Clients.All
                .SendAsync("UpdatedCampaignData", new LiveCampaignData
                {
                    //simple testing data
                    Mes = "hello world"
                });
        }
    }

    SignalR JavaScript Client

    connection.on("UpdatedCampaignData", function (message) {
        console.log(message);
    });

    Test Result

    With Regards,

    Fei Han

    Tuesday, June 25, 2019 6:57 AM
  • User-1158769213 posted

    So what do I do now then? Fine, it worked for you, but it doesn't work for me.

    Thanks

    Tuesday, June 25, 2019 12:53 PM
  • User61956409 posted

    Hi Yossu,

    As you mentioned, your ASP.NET Core SignalR application with scheduled job work fine on local, but it can not work after you deployed to a test server. If you are hosting it on IIS, you can post on IIS forum.

    https://forums.iis.net/

    With Regards,

    Fei Han

    Wednesday, June 26, 2019 2:16 AM