title: How to detect a full page Refresh/Reload in Blazor
body: <p>I am trying to detect a page refresh in Blazor, So that before the refresh occures, I save some data to local storage, I tried Dispose, But the services that I get Data to save are not available at this stage.
  I also tried JavaScript's window.onbeforeunload and calling a C# method when this occures as follows:</p>
  
  <pre><code>public static class SessionSaver
  {
      [JSInvokable]
      public static Task SaveServicesToSession()
      {
          ViewDataService viewData = (ViewDataService)GetMeSomeServiceLocator.Instance.GetService(typeof(ViewDataService));           
          ProtectedBrowserStorage protectedBrowserStorage = (ProtectedBrowserStorage)GetMeSomeServiceLocator.Instance.GetService(typeof(ProtectedSessionStorage));
          protectedBrowserStorage.SetAsync("ViewData", viewData);
          protectedBrowserStorage.SetAsync("AuthModel", viewData.AuthModel);            
          return Task.FromResult("Success.");
      }
  }
  </code></pre>
  
  <p>Javascript:</p>
  
  <pre><code> window.onbeforeunload  = () =&gt; {
          DotNet.invokeMethodAsync('BlazorStore9', 'SaveServicesToSession')
              .then(message =&gt; { console.log(message); });
          }
  </code></pre>
  
  <p>Startup.cs: a nested class:</p>
  
  <pre><code> public static class GetMeSomeServiceLocator
      {
          public static IServiceProvider Instance { get; set; }
      }
  </code></pre>
  
  <p>and in the configure method:</p>
  
  <pre><code>GetMeSomeServiceLocator.Instance = app.ApplicationServices;
  </code></pre>
  
  <p>The JsInvokable is called when I reload the page but I get error:</p>
  
  <p>System.InvalidOperationException: 'Cannot resolve scoped service 'BlazorStore9.Services.ViewDataService' from root provider.'</p>
  
  <p>Is there anything can be done to solve this problem?</p>