title: Can I avoid using BuildServiceProvider in startup?
body: <p>Can I avoid using BuildServiceProvider in ExpireToken function below? In other words...</p>
  
  <p>Is there a way of avoiding BuildServiceProvider() in Startup.cs when using JWTBearer OnAuthenticationFailed event?</p>
  
  <p>(Using Web API aspnet Core 3.0)</p>
  
  <p><strong>Startup.cs</strong></p>
  
  <pre><code>public void ConfigureServices(IServiceCollection services)
      {                        
          //some code removed..
  
          //Authentication
          services.AddAuthentication(options =&gt;
          {
              options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
              options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
          }).AddJwtBearer(options =&gt;
          {
              options.Events = new JwtBearerEvents()
              {                    
                  OnAuthenticationFailed = context =&gt;
                  {
  
                      if (context.Exception.Message.Contains("The token is expired"))
                      {
                          Microsoft.Extensions.Primitives.StringValues token = string.Empty;
  
                          var logger = new LoggerManager();
                          logger.LogError("Token expired"); //confirming that the token expired so authentication failed
  
                          var header = context.Request.Headers;
  
                          if (!header.TryGetValue("Authorization", out token))
                              logger.LogError("no token found");
                          else
                              token = token.ToString().Substring("Bearer ".Length).Trim();
  
                          ExpireToken(services, token);
                      }
                      return Task.CompletedTask;
                  }
              };
           });
       }
  
  public void ExpireToken(IServiceCollection services, string tokenId)
      {
          var sp = services.BuildServiceProvider();
  
          var jwtManager = sp.GetService&lt;IJWTAuthenticationManager&gt;();
          jwtManager.ExpireToken(tokenId);
      }
  </code></pre>
  
  <p>jwtAuthentication Manager will publish this event so that subscribers are notified (c#:event + delegates)</p>
  
  <pre><code>public delegate void TokenExpiredEventHandler(object source, TokenExpiredEventArgs args);
  </code></pre>
  
  <p><strong>JWTAuthenticationManager.cs</strong></p>
  
  <pre><code>public class JWTAuthenticationManager : IJWTAuthenticationManager
  {
      private readonly string _tokenKey;      
      public event TokenExpiredEventHandler TokenExpired;
  
      //some code removed for brevity
      public void ExpireToken(string tokenId)
      {
          OnTokenExpired(tokenId); //notify all subscribers
      }
      protected virtual void OnTokenExpired(string token) {
          TokenExpired?.Invoke(this, new TokenExpiredEventArgs(token));
      }
  }
  
  public class TokenExpiredEventArgs : EventArgs
  {
      public string token;
  
      public TokenExpiredEventArgs(string tokenId)
      {
          token = tokenId;
      }
  }
  </code></pre>
  
  <p><strong>Program.cs</strong></p>
  
  <pre><code>public static void Main(string[] args)
      {
          var host = CreateWebHostBuilder(args).Build();
  
          var jwtMgr = host.Services.GetRequiredService&lt;IJWTAuthenticationManager&gt;();
          var cacheMgr = host.Services.GetRequiredService&lt;ICacheManager&gt;();
          jwtMgr.TokenExpired += cacheMgr.OnTokenExpired;
  
          host.Run();
      }
  </code></pre>