'IHostedService cannot access the session storage which already being defined in the page and passed to the service by parameter

I have 4 pages and a service, the purpose of each pages and a service:

  • First page: animals introduction > when user select animals then start the service
  • Second page: cities selection
  • Service (MyService): to get the data from the API and assign the result to the session storage which can be accessed later on from any pages and afterwards stop the service, otherwise loop through the service until it found the result or the service stopped manually
  • Third page: Summary of what user selected
  • Fourth page: to stop the service and to get the animals information from the session storage if found (foods liked or disliked by animals selected, animal's age, from where the animal coming from, etc), if not found then call the API directly from this page (but supposedly the data already being assigned to the session storage)

The reason on why I put as a service, is because I don't want user to wait and also user could select multiple animals from the first page and then pass the data to the service and the response from the service could take more than 1 second

Above scenarios already achieved, however when I wants to set the response from the API to the session storage, it didn't response to anything, then when comes to the fourth page, there is no data in session storage which when I manually query the DB, there is a response and also when I put the breakpoint at the line where it will assign to the session storage, it didn't get pass there and the last line after set to session storage never executed

Here is the code that I am using:

  • First Page

    @inject 
    Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage.ProtectedSessionStorage SessionStorage
    
    @inject MyService MyService
    
    @code {
      protected override void OnInitialized()
      {
         MyService.SessionStorage = SessionStorage;
      }
    
      // On user submit will execute below function
      private void SetBackgroundService()
      {
          if (MyService.IsStartService)
              return;
    
          MyService.IsStartService = true;
    
          if (!MyService.IsRunning)
              MyService.StartAsync(new System.Threading.CancellationToken());
      }
    }
    
  • MyService

      public class MyService : IHostedService, IDisposable
      {
          public bool IsRunning { get; set; }
    
          public bool IsStartService { get; set; }
    
          public Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage.ProtectedSessionStorage SessionStorage { get; set; }
    
          public Task StartAsync(CancellationToken cancellationToken)
          {
              Task.Run(async () =>
              {
                  if (!IsStartService)
                      return;
    
                  while (!cancellationToken.IsCancellationRequested)
                  {
                      if (!IsStartService)
                          return;
    
                      await Task.Delay(2000, cancellationToken);
    
                      await DoWorkAsync();
                  }
              }, cancellationToken);
    
              return Task.CompletedTask;
          }
    
          public Task StopAsync(CancellationToken cancellationToken)
          {
              IsStartService = IsRunning = false;
    
              return Task.CompletedTask;
          }
    
          private async Task DoWorkAsync()
          {
                  IsRunning = true;
    
                  var Animals = await <API Call>
    
                  if (Animals == null)
                      return;
    
                  await SessionStorage.SetAsync("Animals", JsonConvert.SerializeObject(Animals)); // this is where the debug stops
    
                  await StopAsync(new CancellationToken()); // this line never executed
          }
    
          public void Dispose()
          {
    
          }
      }
    
  • Fourth page

      @inject MyService MyService
    
      protected override void OnInitialized()
      {
              if (MyService.IsRunning)
                  MyService.StopAsync(new System.Threading.CancellationToken());
      }
    
  • Startup

      public void ConfigureServices(IServiceCollection services)
      {
          // hide other codes for simplicity
          services.AddHostedService<MyService>();
      }
    

Any idea what I am doing wrong?

Thank you very much.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source