title: Service not being injected within Hangfire job in Blazor Server App
body: <p>Disclaimer: I'm very new to the C#, ASP.NET Core and Dependency Injection world. I've created a simple Blazor Server App from the default template, which scaffolds a mock weather service and shows data fetched from it in a table. Now I want the table to be automatically updated every five seconds, for which I am using the <code>Hangfire.AspNetCore</code> and <code>Hangfire.MemoryStorage</code> packages. So I've slightly modified the <code>FetchData.razor</code> component to look like this:</p>
  <pre><code>@page &quot;/fetchdata&quot;
  
  @using WeatherTest.Data
  @using Hangfire
  
  @inject WeatherForecastService ForecastService
  
  &lt;h1&gt;Weather forecast&lt;/h1&gt;
  
  &lt;p&gt;This component demonstrates fetching data from a service.&lt;/p&gt;
  
  @if (forecasts == null)
  {
      &lt;p&gt;&lt;em&gt;Loading...&lt;/em&gt;&lt;/p&gt;
  }
  else
  {
      &lt;table class=&quot;table&quot;&gt;
          &lt;thead&gt;
              &lt;tr&gt;
                  &lt;th&gt;Date&lt;/th&gt;
                  &lt;th&gt;Temp. (C)&lt;/th&gt;
                  &lt;th&gt;Temp. (F)&lt;/th&gt;
                  &lt;th&gt;Summary&lt;/th&gt;
              &lt;/tr&gt;
          &lt;/thead&gt;
          &lt;tbody&gt;
              @foreach (var forecast in forecasts)
              {
                  &lt;tr&gt;
                      &lt;td&gt;@forecast.Date.ToShortDateString()&lt;/td&gt;
                      &lt;td&gt;@forecast.TemperatureC&lt;/td&gt;
                      &lt;td&gt;@forecast.TemperatureF&lt;/td&gt;
                      &lt;td&gt;@forecast.Summary&lt;/td&gt;
                  &lt;/tr&gt;
              }
          &lt;/tbody&gt;
      &lt;/table&gt;
  }
  
  @code {
      private WeatherForecast[] forecasts;
  
      public async Task UpdateForecasts()
      {
        forecasts = await ForecastService.GetForecastAsync(DateTime.Now);
        BackgroundJob.Schedule(() =&gt; UpdateForecasts(), TimeSpan.FromSeconds(5));
      }
  
      protected override async Task OnInitializedAsync()
      {
        await UpdateForecasts();
      }
  }
  </code></pre>
  <p>This is the weather service:</p>
  <pre><code>using System;
  using System.Linq;
  using System.Threading.Tasks;
  
  namespace WeatherTest.Data
  {
      public class WeatherForecastService
      {
          private static readonly string[] Summaries = new[]
          {
              &quot;Freezing&quot;, &quot;Bracing&quot;, &quot;Chilly&quot;, &quot;Cool&quot;, &quot;Mild&quot;, &quot;Warm&quot;, &quot;Balmy&quot;, &quot;Hot&quot;, &quot;Sweltering&quot;, &quot;Scorching&quot;
          };
  
          public Task&lt;WeatherForecast[]&gt; GetForecastAsync(DateTime startDate)
          {
              var rng = new Random();
              return Task.FromResult(Enumerable.Range(1, 5).Select(index =&gt; new WeatherForecast
              {
                  Date = startDate.AddDays(index),
                  TemperatureC = rng.Next(-20, 55),
                  Summary = Summaries[rng.Next(Summaries.Length)]
              }).ToArray());
          }
      }
  }
  </code></pre>
  <p>And the services configuration:</p>
  <pre><code>public void ConfigureServices(IServiceCollection services)
  {
      services.AddRazorPages();
      services.AddServerSideBlazor();
      services.AddSingleton&lt;WeatherForecastService&gt;();
      services.AddHangfire(c =&gt; c.UseMemoryStorage());
      services.AddHangfireServer();
  }
  </code></pre>
  <p>The problem is that <code>UpdateForecasts()</code> succeeds when called from <code>OnInitializedAsync()</code>, but fails when called by Hangfire throwing the following exception at <code>forecasts = await ForecastService.GetForecastAsync(DateTime.Now);</code>:</p>
  <blockquote>
  <p>System.NullReferenceException: 'Object reference not set to an
  instance of an object.'</p>
  <p>WeatherTest.Pages.FetchData.ForecastService.get returned null.</p>
  </blockquote>
  <p>It seems to me that, since the Hangfire worker runs in another thread, then the <code>WeatherForecastService</code> doesn't get injected. Am I right? Is it possible for a singleton to be used from multiple threads, or each thread should have its own service instance? At last, how should I solve this?</p>