From b66b2df2641b4c76b515bf1dc55e93e47cbb1c46 Mon Sep 17 00:00:00 2001 From: Hao Kung Date: Thu, 19 Mar 2015 11:22:22 -0700 Subject: [PATCH] React to hosting --- .../DatabaseErrorPageMiddleware.cs | 55 ++++++++--------- .../MigrationsEndPointMiddleware.cs | 40 ++++++------ src/Microsoft.AspNet.Diagnostics/project.json | 3 +- .../DatabaseErrorPageMiddlewareTest.cs | 61 ++++++++----------- .../MigrationsEndPointMiddlewareTest.cs | 36 +++++------ 5 files changed, 85 insertions(+), 110 deletions(-) diff --git a/src/Microsoft.AspNet.Diagnostics.Entity/DatabaseErrorPageMiddleware.cs b/src/Microsoft.AspNet.Diagnostics.Entity/DatabaseErrorPageMiddleware.cs index a9756d5b41..04aa5dc7e2 100644 --- a/src/Microsoft.AspNet.Diagnostics.Entity/DatabaseErrorPageMiddleware.cs +++ b/src/Microsoft.AspNet.Diagnostics.Entity/DatabaseErrorPageMiddleware.cs @@ -1,21 +1,19 @@ // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +using System; +using System.Linq; +using System.Threading.Tasks; using JetBrains.Annotations; using Microsoft.AspNet.Builder; using Microsoft.AspNet.Diagnostics.Entity.Utilities; using Microsoft.AspNet.Diagnostics.Entity.Views; using Microsoft.AspNet.Http; -using Microsoft.AspNet.RequestContainer; using Microsoft.Data.Entity; using Microsoft.Data.Entity.Infrastructure; -using Microsoft.Data.Entity.Internal; using Microsoft.Data.Entity.Relational; using Microsoft.Data.Entity.Relational.Migrations; using Microsoft.Framework.Logging; -using System; -using System.Linq; -using System.Threading.Tasks; namespace Microsoft.AspNet.Diagnostics.Entity { @@ -65,37 +63,34 @@ namespace Microsoft.AspNet.Diagnostics.Entity { if (ShouldDisplayErrorPage(_loggerProvider.Logger.LastError, ex, _logger)) { - using (RequestServicesContainer.EnsureRequestServices(context, _serviceProvider)) + var dbContextType = _loggerProvider.Logger.LastError.ContextType; + var dbContext = (DbContext)context.RequestServices.GetService(dbContextType); + if (dbContext == null) { - var dbContextType = _loggerProvider.Logger.LastError.ContextType; - var dbContext = (DbContext)context.RequestServices.GetService(dbContextType); - if (dbContext == null) + _logger.LogError(Strings.FormatDatabaseErrorPageMiddleware_ContextNotRegistered(dbContextType.FullName)); + } + else + { + if (!(dbContext.Database is RelationalDatabase)) { - _logger.LogError(Strings.FormatDatabaseErrorPageMiddleware_ContextNotRegistered(dbContextType.FullName)); + _logger.LogVerbose(Strings.DatabaseErrorPage_NotRelationalDatabase); } else { - if (!(dbContext.Database is RelationalDatabase)) + var databaseExists = dbContext.Database.AsRelational().Exists(); + + var migrator = ((IAccessor)dbContext.Database).Service; + + var pendingMigrations = migrator.GetUnappliedMigrations().Select(m => m.Id); + + var pendingModelChanges = migrator.HasPendingModelChanges(); + + if ((!databaseExists && pendingMigrations.Any()) || pendingMigrations.Any() || pendingModelChanges) { - _logger.LogVerbose(Strings.DatabaseErrorPage_NotRelationalDatabase); - } - else - { - var databaseExists = dbContext.Database.AsRelational().Exists(); - - var migrator = ((IAccessor)dbContext.Database).Service; - - var pendingMigrations = migrator.GetUnappliedMigrations().Select(m => m.Id); - - var pendingModelChanges = migrator.HasPendingModelChanges(); - - if ((!databaseExists && pendingMigrations.Any()) || pendingMigrations.Any() || pendingModelChanges) - { - var page = new DatabaseErrorPage(); - page.Model = new DatabaseErrorPageModel(dbContextType, ex, databaseExists, pendingModelChanges, pendingMigrations, _options); - await page.ExecuteAsync(context).WithCurrentCulture(); - return; - } + var page = new DatabaseErrorPage(); + page.Model = new DatabaseErrorPageModel(dbContextType, ex, databaseExists, pendingModelChanges, pendingMigrations, _options); + await page.ExecuteAsync(context).WithCurrentCulture(); + return; } } } diff --git a/src/Microsoft.AspNet.Diagnostics.Entity/MigrationsEndPointMiddleware.cs b/src/Microsoft.AspNet.Diagnostics.Entity/MigrationsEndPointMiddleware.cs index 710aa8100d..13d65e1a30 100644 --- a/src/Microsoft.AspNet.Diagnostics.Entity/MigrationsEndPointMiddleware.cs +++ b/src/Microsoft.AspNet.Diagnostics.Entity/MigrationsEndPointMiddleware.cs @@ -2,17 +2,14 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; +using System.Net; using System.Threading.Tasks; using JetBrains.Annotations; using Microsoft.AspNet.Builder; using Microsoft.AspNet.Diagnostics.Entity.Utilities; using Microsoft.AspNet.Http; using Microsoft.Data.Entity; -using Microsoft.Data.Entity.Relational.Migrations.Infrastructure; -using Microsoft.Framework.DependencyInjection; -using System.Net; using Microsoft.Framework.Logging; -using Microsoft.AspNet.RequestContainer; namespace Microsoft.AspNet.Diagnostics.Entity { @@ -44,29 +41,26 @@ namespace Microsoft.AspNet.Diagnostics.Entity { _logger.LogVerbose(Strings.FormatMigrationsEndPointMiddleware_RequestPathMatched(context.Request.Path)); - using (RequestServicesContainer.EnsureRequestServices(context, _serviceProvider)) - { - var db = await GetDbContext(context, _logger).WithCurrentCulture(); - if (db != null) + var db = await GetDbContext(context, _logger).WithCurrentCulture(); + if (db != null) + { + try { - try - { - _logger.LogVerbose(Strings.FormatMigrationsEndPointMiddleware_ApplyingMigrations(db.GetType().FullName)); + _logger.LogVerbose(Strings.FormatMigrationsEndPointMiddleware_ApplyingMigrations(db.GetType().FullName)); - db.Database.AsRelational().ApplyMigrations(); + db.Database.AsRelational().ApplyMigrations(); - context.Response.StatusCode = (int)HttpStatusCode.NoContent; - context.Response.Headers.Add("Pragma", new[] { "no-cache" }); - context.Response.Headers.Add("Cache-Control", new[] { "no-cache" }); + context.Response.StatusCode = (int)HttpStatusCode.NoContent; + context.Response.Headers.Add("Pragma", new[] { "no-cache" }); + context.Response.Headers.Add("Cache-Control", new[] { "no-cache" }); - _logger.LogVerbose(Strings.FormatMigrationsEndPointMiddleware_Applied(db.GetType().FullName)); - } - catch (Exception ex) - { - var message = Strings.FormatMigrationsEndPointMiddleware_Exception(db.GetType().FullName); - _logger.LogError(message); - throw new InvalidOperationException(message, ex); - } + _logger.LogVerbose(Strings.FormatMigrationsEndPointMiddleware_Applied(db.GetType().FullName)); + } + catch (Exception ex) + { + var message = Strings.FormatMigrationsEndPointMiddleware_Exception(db.GetType().FullName); + _logger.LogError(message); + throw new InvalidOperationException(message, ex); } } } diff --git a/src/Microsoft.AspNet.Diagnostics/project.json b/src/Microsoft.AspNet.Diagnostics/project.json index ad516b1372..ccab38e8fd 100644 --- a/src/Microsoft.AspNet.Diagnostics/project.json +++ b/src/Microsoft.AspNet.Diagnostics/project.json @@ -3,8 +3,9 @@ "description": "ASP.NET 5 Middleware for error handling, error pages, and diagnostics information.", "dependencies": { "Microsoft.AspNet.Diagnostics.Interfaces": "1.0.0-*", - "Microsoft.AspNet.RequestContainer": "1.0.0-*", + "Microsoft.AspNet.Hosting": "1.0.0-*", "Microsoft.AspNet.WebUtilities": "1.0.0-*", + "Microsoft.Framework.OptionsModel": "1.0.0-*", "Microsoft.Framework.NotNullAttribute.Internal": { "type": "build", "version": "1.0.0-*" }, "Microsoft.Framework.WebEncoders.Core": "1.0.0-*" }, diff --git a/test/Microsoft.AspNet.Diagnostics.Entity.FunctionalTests/DatabaseErrorPageMiddlewareTest.cs b/test/Microsoft.AspNet.Diagnostics.Entity.FunctionalTests/DatabaseErrorPageMiddlewareTest.cs index cadc689c69..4959c4f430 100644 --- a/test/Microsoft.AspNet.Diagnostics.Entity.FunctionalTests/DatabaseErrorPageMiddlewareTest.cs +++ b/test/Microsoft.AspNet.Diagnostics.Entity.FunctionalTests/DatabaseErrorPageMiddlewareTest.cs @@ -231,21 +231,20 @@ namespace Microsoft.AspNet.Diagnostics.Entity.Tests { var server = TestServer.Create(app => { - app.UseServices(services => - { - services.AddEntityFramework().AddSqlServer(); - services.AddScoped(); - - var optionsBuilder = new DbContextOptionsBuilder(); - optionsBuilder.UseSqlServer(database.ConnectionString); - services.AddInstance(optionsBuilder.Options); - }); - var options = DatabaseErrorPageOptions.ShowAll; options.MigrationsEndPointPath = new PathString(migrationsEndpoint); app.UseDatabaseErrorPage(options); app.UseMiddleware(); + }, + services => + { + services.AddEntityFramework().AddSqlServer(); + services.AddScoped(); + + var optionsBuilder = new DbContextOptionsBuilder(); + optionsBuilder.UseSqlServer(database.ConnectionString); + services.AddInstance(optionsBuilder.Options); }); HttpResponseMessage response = await server.CreateClient().GetAsync("http://localhost/"); @@ -266,21 +265,16 @@ namespace Microsoft.AspNet.Diagnostics.Entity.Tests var server = TestServer.Create(app => { - app.UseServices(services => - { - services.AddEntityFramework() - .AddSqlServer(); - - var optionsBuilder = new DbContextOptionsBuilder(); - optionsBuilder.UseSqlServer(database.ConnectionString); - services.AddInstance(optionsBuilder.Options); - }); - app.UseDatabaseErrorPage(); - app.UseMiddleware(); - app.ApplicationServices.GetService().AddProvider(logProvider); + }, + services => + { + services.AddEntityFramework().AddSqlServer(); + var optionsBuilder = new DbContextOptionsBuilder(); + optionsBuilder.UseSqlServer(database.ConnectionString); + services.AddInstance(optionsBuilder.Options); }); var ex = await Assert.ThrowsAsync(async () => @@ -383,18 +377,6 @@ namespace Microsoft.AspNet.Diagnostics.Entity.Tests { return TestServer.Create(app => { - app.UseServices(services => - { - services.AddEntityFramework() - .AddSqlServer(); - - services.AddScoped(); - - var optionsBuilder = new DbContextOptionsBuilder(); - optionsBuilder.UseSqlServer(database.ConnectionString); - services.AddInstance(optionsBuilder.Options); - }); - app.UseDatabaseErrorPage(); app.UseMiddleware(); @@ -403,6 +385,17 @@ namespace Microsoft.AspNet.Diagnostics.Entity.Tests { app.ApplicationServices.GetService().AddProvider(logProvider); } + }, + services => + { + services.AddEntityFramework() + .AddSqlServer(); + + services.AddScoped(); + + var optionsBuilder = new DbContextOptionsBuilder(); + optionsBuilder.UseSqlServer(database.ConnectionString); + services.AddInstance(optionsBuilder.Options); }); } } diff --git a/test/Microsoft.AspNet.Diagnostics.Entity.FunctionalTests/MigrationsEndPointMiddlewareTest.cs b/test/Microsoft.AspNet.Diagnostics.Entity.FunctionalTests/MigrationsEndPointMiddlewareTest.cs index 863fb9ffa1..ebf08ad148 100644 --- a/test/Microsoft.AspNet.Diagnostics.Entity.FunctionalTests/MigrationsEndPointMiddlewareTest.cs +++ b/test/Microsoft.AspNet.Diagnostics.Entity.FunctionalTests/MigrationsEndPointMiddlewareTest.cs @@ -17,6 +17,7 @@ using Microsoft.Data.Entity.Relational.Migrations.History; using Microsoft.Framework.DependencyInjection; using Xunit; using Microsoft.AspNet.Diagnostics.Entity.Tests.Helpers; +using Microsoft.AspNet.Hosting.Startup; namespace Microsoft.AspNet.Diagnostics.Entity.Tests { @@ -70,13 +71,6 @@ namespace Microsoft.AspNet.Diagnostics.Entity.Tests TestServer server = TestServer.Create(app => { - app.UseServices(services => - { - services.AddEntityFramework().AddSqlServer(); - services.AddScoped(); - services.AddInstance(optionsBuilder.Options); - }); - if (useCustomPath) { app.UseMigrationsEndPoint(new MigrationsEndPointOptions { Path = path }); @@ -85,6 +79,12 @@ namespace Microsoft.AspNet.Diagnostics.Entity.Tests { app.UseMigrationsEndPoint(); } + }, + services => + { + services.AddEntityFramework().AddSqlServer(); + services.AddScoped(); + services.AddInstance(optionsBuilder.Options); }); using (var db = BloggingContextWithMigrations.CreateWithoutExternalServiceProvider(optionsBuilder.Options)) @@ -155,14 +155,9 @@ namespace Microsoft.AspNet.Diagnostics.Entity.Tests [Fact] public async Task Context_not_registered_in_services() { - var server = TestServer.Create(app => - { - app.UseServices(services => - { - services.AddEntityFramework().AddSqlServer(); - }); - app.UseMigrationsEndPoint(); - }); + var server = TestServer.Create( + app => app.UseMigrationsEndPoint(), + services => services.AddEntityFramework().AddSqlServer()); var formData = new FormUrlEncodedContent(new List> { @@ -185,18 +180,15 @@ namespace Microsoft.AspNet.Diagnostics.Entity.Tests var optionsBuilder = new DbContextOptionsBuilder(); optionsBuilder.UseSqlServer(database.ConnectionString); - TestServer server = TestServer.Create(app => - { - app.UseServices(services => + TestServer server = TestServer.Create( + app => app.UseMigrationsEndPoint(), + services => { services.AddEntityFramework().AddSqlServer(); services.AddScoped(); - services.AddInstance(optionsBuilder.Options); + services.AddInstance(optionsBuilder.Options); }); - app.UseMigrationsEndPoint(); - }); - var formData = new FormUrlEncodedContent(new List> { new KeyValuePair("context", typeof(BloggingContextWithSnapshotThatThrows).AssemblyQualifiedName)