/// <summary> /// Process an individual request. /// </summary> /// <param name="httpContext">The HTTP context for the current request.</param> /// <returns>A task that represents the asynchronous operation.</returns> public virtual async Task Invoke(HttpContext httpContext) { if (httpContext == null) { throw new ArgumentNullException(nameof(httpContext)); } try { // Because CallContext is cloned at each async operation we cannot // lazily create the error object when an error is encountered, otherwise // it will not be available to code outside of the current async context. // We create it ahead of time so that any cloning just clones the reference // to the object that will hold any errors. _localDiagnostic.Value = new DiagnosticHolder(); await _next(httpContext); } catch (Exception exception) { try { if (ShouldDisplayErrorPage(exception)) { var contextType = _localDiagnostic.Value.ContextType; var context = (DbContext)httpContext.RequestServices.GetService(contextType); if (context == null) { _logger.LogError(Strings.FormatDatabaseErrorPageMiddleware_ContextNotRegistered(contextType.FullName)); } else { var relationalDatabaseCreator = context.GetService <IDatabaseCreator>() as IRelationalDatabaseCreator; if (relationalDatabaseCreator == null) { _logger.LogDebug(Strings.DatabaseErrorPage_NotRelationalDatabase); } else { var databaseExists = await relationalDatabaseCreator.ExistsAsync(); var migrationsAssembly = context.GetService <IMigrationsAssembly>(); var modelDiffer = context.GetService <IMigrationsModelDiffer>(); // HasDifferences will return true if there is no model snapshot, but if there is an existing database // and no model snapshot then we don't want to show the error page since they are most likely targeting // and existing database and have just misconfigured their model var pendingModelChanges = (!databaseExists || migrationsAssembly.ModelSnapshot != null) && modelDiffer.HasDifferences(migrationsAssembly.ModelSnapshot?.Model, context.Model); var pendingMigrations = (databaseExists ? await context.Database.GetPendingMigrationsAsync() : context.Database.GetMigrations()) .ToArray(); if (pendingModelChanges || pendingMigrations.Any()) { var page = new DatabaseErrorPage { Model = new DatabaseErrorPageModel( contextType, exception, databaseExists, pendingModelChanges, pendingMigrations, _options) }; await page.ExecuteAsync(httpContext); return; } } } } } catch (Exception e) { _logger.LogError( eventId: 0, exception: e, message: Strings.DatabaseErrorPageMiddleware_Exception); } throw; } }