Reacting to new Hosting API

This commit is contained in:
John Luo 2015-12-16 18:37:37 -08:00
parent e384b7d4d0
commit c476f91152
35 changed files with 578 additions and 355 deletions

View File

@ -1,9 +1,9 @@
using System;
using Microsoft.AspNet.Builder;
using Microsoft.Data.Entity;
using System.Linq;
using Microsoft.Extensions.DependencyInjection;
using System.Threading.Tasks;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Hosting;
using Microsoft.Data.Entity;
using Microsoft.Extensions.DependencyInjection;
namespace DatabaseErrorPageSample
{
@ -26,6 +26,16 @@ namespace DatabaseErrorPageSample
return Task.FromResult(0);
});
}
public static void Main(string[] args)
{
var application = new WebApplicationBuilder()
.UseConfiguration(WebApplicationConfiguration.GetDefault(args))
.UseStartup<Startup>()
.Build();
application.Run();
}
}
public class MyContext : DbContext

View File

@ -0,0 +1,3 @@
{
"server": "Microsoft.AspNet.Server.Kestrel"
}

View File

@ -1,15 +1,15 @@
{
"webroot": "wwwroot",
"dependencies": {
"Microsoft.AspNet.Diagnostics.Entity": "7.0.0-*",
"EntityFramework.MicrosoftSqlServer": "7.0.0-*",
"EntityFramework.Commands": "7.0.0-*",
"Microsoft.AspNet.Server.WebListener": "1.0.0-*",
"Microsoft.AspNet.Server.Kestrel": "1.0.0-*"
},
"compilationOptions": {
"emitEntryPoint": true
},
"commands": {
"ef": "EntityFramework.Commands",
"web": "Microsoft.AspNet.Server.Kestrel"
"web": "DatabaseErrorPageSample"
},
"frameworks": {
"dnx451": { },

View File

@ -1,5 +1,6 @@
using System;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Hosting;
namespace DeveloperExceptionPageSample
{
@ -17,5 +18,15 @@ namespace DeveloperExceptionPageSample
"New Line 3"));
});
}
public static void Main(string[] args)
{
var application = new WebApplicationBuilder()
.UseConfiguration(WebApplicationConfiguration.GetDefault(args))
.UseStartup<Startup>()
.Build();
application.Run();
}
}
}

View File

@ -0,0 +1,3 @@
{
"server": "Microsoft.AspNet.Server.Kestrel"
}

View File

@ -1,11 +1,14 @@
{
"webroot" : "wwwroot",
"dependencies": {
"Microsoft.AspNet.Diagnostics": "1.0.0-*",
"Microsoft.AspNet.Server.WebListener": "1.0.0-*",
"Microsoft.AspNet.Server.IIS": "1.0.0-*"
"Microsoft.AspNet.Server.Kestrel": "1.0.0-*"
},
"compilationOptions": {
"emitEntryPoint": true
},
"commands": {
"web": "DeveloperExceptionPageSample"
},
"commands": { "web": "Microsoft.AspNet.Hosting server=Microsoft.AspNet.Server.WebListener server.urls=http://localhost:5001" },
"frameworks": {
"dnx451": {},
"dnxcore50": {}

View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<handlers>
<add name="httpPlatformHandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified" />
</handlers>
<httpPlatform processPath="%DNX_PATH%" arguments="%DNX_ARGS%" forwardWindowsAuthToken="false" startupTimeLimit="3600" />
</system.webServer>
</configuration>

View File

@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
@ -27,5 +28,15 @@ namespace ElmPageSample
app.UseMiddleware<HelloWorldMiddleware>();
}
public static void Main(string[] args)
{
var application = new WebApplicationBuilder()
.UseConfiguration(WebApplicationConfiguration.GetDefault(args))
.UseStartup<Startup>()
.Build();
application.Run();
}
}
}

View File

@ -0,0 +1,3 @@
{
"server": "Microsoft.AspNet.Server.Kestrel"
}

View File

@ -1,16 +1,17 @@
{
"webroot": "wwwroot",
"exclude": "wwwroot/**/*.*",
"dependencies": {
"Microsoft.AspNet.Diagnostics.Elm": "1.0.0-*",
"Microsoft.AspNet.Server.IIS": "1.0.0-*",
"Microsoft.AspNet.Server.WebListener": "1.0.0-*"
},
"commands": {
"web": "Microsoft.AspNet.Hosting --server Microsoft.AspNet.Server.WebListener --server.urls http://localhost:5000"
},
"frameworks": {
"dnx451" : { },
"dnxcore50" : { }
}
"exclude": "wwwroot/**/*.*",
"dependencies": {
"Microsoft.AspNet.Diagnostics.Elm": "1.0.0-*",
"Microsoft.AspNet.Server.Kestrel": "1.0.0-*"
},
"compilationOptions": {
"emitEntryPoint": true
},
"commands": {
"web": "ElmPageSample"
},
"frameworks": {
"dnx451": { },
"dnxcore50": { }
}
}

View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<handlers>
<add name="httpPlatformHandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified" />
</handlers>
<httpPlatform processPath="%DNX_PATH%" arguments="%DNX_ARGS%" forwardWindowsAuthToken="false" startupTimeLimit="3600" />
</system.webServer>
</configuration>

View File

@ -2,6 +2,7 @@ using System;
using System.Text.Encodings.Web;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Diagnostics;
using Microsoft.AspNet.Hosting;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Http.Features;
@ -54,5 +55,15 @@ namespace ExceptionHandlerSample
await context.Response.WriteAsync("</body></html>\r\n");
});
}
public static void Main(string[] args)
{
var application = new WebApplicationBuilder()
.UseConfiguration(WebApplicationConfiguration.GetDefault(args))
.UseStartup<Startup>()
.Build();
application.Run();
}
}
}

View File

@ -0,0 +1,3 @@
{
"server": "Microsoft.AspNet.Server.Kestrel"
}

View File

@ -1,17 +1,18 @@
{
"webroot": "wwwroot",
"exclude": "wwwroot/**/*.*",
"dependencies": {
"Microsoft.AspNet.Diagnostics": "1.0.0-*",
"Microsoft.AspNet.Server.IIS": "1.0.0-*",
"Microsoft.AspNet.Server.WebListener": "1.0.0-*",
"Microsoft.AspNet.StaticFiles": "1.0.0-*"
},
"commands": {
"web": "Microsoft.AspNet.Hosting --server Microsoft.AspNet.Server.WebListener --server.urls http://localhost:5000"
},
"frameworks": {
"dnx451" : { },
"dnxcore50" : { }
}
"exclude": "wwwroot/**/*.*",
"dependencies": {
"Microsoft.AspNet.Diagnostics": "1.0.0-*",
"Microsoft.AspNet.Server.Kestrel": "1.0.0-*",
"Microsoft.AspNet.StaticFiles": "1.0.0-*"
},
"compilationOptions": {
"emitEntryPoint": true
},
"commands": {
"web": "ExceptionHandlerSample"
},
"frameworks": {
"dnx451": { },
"dnxcore50": { }
}
}

View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<handlers>
<add name="httpPlatformHandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified" />
</handlers>
<httpPlatform processPath="%DNX_PATH%" arguments="%DNX_ARGS%" forwardWindowsAuthToken="false" startupTimeLimit="3600" />
</system.webServer>
</configuration>

View File

@ -1,6 +1,7 @@
using System;
using System.Diagnostics;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Hosting;
using Microsoft.AspNet.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DiagnosticAdapter;
@ -82,6 +83,16 @@ namespace MiddlewareAnaysisSample
// Note there's always a default 404 middleware at the end of the pipeline.
}
public static void Main(string[] args)
{
var application = new WebApplicationBuilder()
.UseConfiguration(WebApplicationConfiguration.GetDefault(args))
.UseStartup<Startup>()
.Build();
application.Run();
}
public class TestDiagnosticListener
{
[DiagnosticName("Microsoft.AspNet.MiddlewareAnalysis.MiddlewareStarting")]

View File

@ -0,0 +1,3 @@
{
"server": "Microsoft.AspNet.Server.Kestrel"
}

View File

@ -4,13 +4,14 @@
"Microsoft.AspNet.IISPlatformHandler": "1.0.0-*",
"Microsoft.AspNet.MiddlewareAnalysis": "1.0.0-*",
"Microsoft.AspNet.Server.Kestrel": "1.0.0-*",
"Microsoft.AspNet.Server.WebListener": "1.0.0-*",
"Microsoft.Extensions.DiagnosticAdapter": "1.0.0-*",
"Microsoft.Extensions.Logging.Console": "1.0.0-*"
},
"compilationOptions": {
"emitEntryPoint": true
},
"commands": {
"web": "Microsoft.AspNet.Server.Kestrel",
"weblistener": "Microsoft.AspNet.Server.WebListener"
"web": "MiddlewareAnalysisSample"
},
"frameworks": {
"dnx451": { },

View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<handlers>
<add name="httpPlatformHandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified" />
</handlers>
<httpPlatform processPath="%DNX_PATH%" arguments="%DNX_ARGS%" forwardWindowsAuthToken="false" startupTimeLimit="3600" />
</system.webServer>
</configuration>

View File

@ -1,8 +1,6 @@
using System;
using System.Threading.Tasks;
using System.Threading.Tasks;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Diagnostics;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Hosting;
namespace RuntimeInfoPageSample
{
@ -20,5 +18,15 @@ namespace RuntimeInfoPageSample
return Task.FromResult(0);
});
}
public static void Main(string[] args)
{
var application = new WebApplicationBuilder()
.UseConfiguration(WebApplicationConfiguration.GetDefault(args))
.UseStartup<Startup>()
.Build();
application.Run();
}
}
}

View File

@ -0,0 +1,3 @@
{
"server": "Microsoft.AspNet.Server.Kestrel"
}

View File

@ -1,12 +1,17 @@
{
"webroot" : "wwwroot",
"exclude": "wwwroot/**/*.*",
"dependencies": {
"Microsoft.AspNet.Diagnostics": "",
"Microsoft.AspNet.Server.IIS": "1.0.0-*"
},
"frameworks" : {
"dnx451" : { },
"dnxcore50" : { }
}
"exclude": "wwwroot/**/*.*",
"dependencies": {
"Microsoft.AspNet.Diagnostics": "",
"Microsoft.AspNet.Server.Kestrel": "1.0.0-*"
},
"compilationOptions": {
"emitEntryPoint": true
},
"commands": {
"web": "RuntimeInfoPageSample"
},
"frameworks": {
"dnx451": { },
"dnxcore50": { }
}
}

View File

@ -8,6 +8,7 @@ using System.Text.Encodings.Web;
using System.Threading.Tasks;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Diagnostics;
using Microsoft.AspNet.Hosting;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Http.Features;
@ -100,5 +101,15 @@ namespace StatusCodePagesSample
await context.Response.WriteAsync(builder.ToString());
});
}
public static void Main(string[] args)
{
var application = new WebApplicationBuilder()
.UseConfiguration(WebApplicationConfiguration.GetDefault(args))
.UseStartup<Startup>()
.Build();
application.Run();
}
}
}

View File

@ -0,0 +1,4 @@
{
"server": "Microsoft.AspNet.Server.Kestrel",
"server.urls": "http://localhost:5001/base"
}

View File

@ -1,24 +1,25 @@
{
"webroot": "wwwroot",
"version": "1.0.0-*",
"exclude": [
"wwwroot"
],
"packExclude": [
"node_modules",
"bower_components",
"**.kproj",
"**.user",
"**.vspscc"
],
"commands": { "web": "Microsoft.AspNet.Hosting server=Microsoft.AspNet.Server.WebListener server.urls=http://localhost:5001/base" },
"dependencies": {
"Microsoft.AspNet.Diagnostics": "1.0.0-*",
"Microsoft.AspNet.Server.IIS": "1.0.0-*",
"Microsoft.AspNet.Server.WebListener": "1.0.0-*"
},
"frameworks" : {
"dnx451" : { },
"dnxcore50" : { }
}
"version": "1.0.0-*",
"exclude": [
"wwwroot"
],
"packExclude": [
"node_modules",
"bower_components",
"**.kproj",
"**.user",
"**.vspscc"
],
"compilationOptions": {
"emitEntryPoint": true
},
"commands": { "web": "StatusCodePagesSample" },
"dependencies": {
"Microsoft.AspNet.Diagnostics": "1.0.0-*",
"Microsoft.AspNet.Server.Kestrel": "1.0.0-*"
},
"frameworks": {
"dnx451": { },
"dnxcore50": { }
}
}

View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<handlers>
<add name="httpPlatformHandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified" />
</handlers>
<httpPlatform processPath="%DNX_PATH%" arguments="%DNX_ARGS%" forwardWindowsAuthToken="false" startupTimeLimit="3600" />
</system.webServer>
</configuration>

View File

@ -1,5 +1,5 @@
using Microsoft.AspNet;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Hosting;
namespace WelcomePageSample
{
@ -9,5 +9,15 @@ namespace WelcomePageSample
{
app.UseWelcomePage();
}
public static void Main(string[] args)
{
var application = new WebApplicationBuilder()
.UseConfiguration(WebApplicationConfiguration.GetDefault(args))
.UseStartup<Startup>()
.Build();
application.Run();
}
}
}

View File

@ -0,0 +1,3 @@
{
"server": "Microsoft.AspNet.Server.Kestrel"
}

View File

@ -1,13 +1,14 @@
{
"webroot": "wwwroot",
"dependencies": {
"Microsoft.AspNet.Diagnostics": "",
"Microsoft.AspNet.Server.WebListener": "1.0.0-*",
"Microsoft.AspNet.Server.IIS": "1.0.0-*"
},
"commands": { "web": "Microsoft.AspNet.Hosting server=Microsoft.AspNet.Server.WebListener server.urls=http://localhost:5001" },
"frameworks": {
"dnx451": { },
"dnxcore50": { }
}
"dependencies": {
"Microsoft.AspNet.Diagnostics": "",
"Microsoft.AspNet.Server.Kestrel": "1.0.0-*"
},
"compilationOptions": {
"emitEntryPoint": true
},
"commands": { "web": "WelcomePageSample" },
"frameworks": {
"dnx451": { },
"dnxcore50": { }
}
}

View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<handlers>
<add name="httpPlatformHandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified" />
</handlers>
<httpPlatform processPath="%DNX_PATH%" arguments="%DNX_ARGS%" forwardWindowsAuthToken="false" startupTimeLimit="3600" />
</system.webServer>
</configuration>

View File

@ -12,6 +12,7 @@ using System.Threading.Tasks;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Diagnostics.Entity.FunctionalTests.Helpers;
using Microsoft.AspNet.Diagnostics.Entity.Tests.Helpers;
using Microsoft.AspNet.Hosting;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.TestHost;
using Microsoft.AspNet.Testing.xunit;
@ -28,9 +29,10 @@ namespace Microsoft.AspNet.Diagnostics.Entity.Tests
[Fact]
public async Task Successful_requests_pass_thru()
{
TestServer server = TestServer.Create(app => app
var builder = new WebApplicationBuilder().Configure(app => app
.UseDatabaseErrorPage()
.UseMiddleware<SuccessMiddleware>());
var server = new TestServer(builder);
HttpResponseMessage response = await server.CreateClient().GetAsync("http://localhost/");
@ -53,9 +55,10 @@ namespace Microsoft.AspNet.Diagnostics.Entity.Tests
[Fact]
public async Task Non_database_exceptions_pass_thru()
{
TestServer server = TestServer.Create(app => app
var builder = new WebApplicationBuilder().Configure(app => app
.UseDatabaseErrorPage()
.UseMiddleware<ExceptionMiddleware>());
var server = new TestServer(builder);
var ex = await Assert.ThrowsAsync<InvalidOperationException>(async () =>
await server.CreateClient().GetAsync("http://localhost/"));
@ -252,25 +255,27 @@ namespace Microsoft.AspNet.Diagnostics.Entity.Tests
using (var database = SqlServerTestStore.CreateScratch())
{
var server = TestServer.Create(app =>
{
app.UseDatabaseErrorPage(options =>
var builder = new WebApplicationBuilder()
.Configure(app =>
{
options.EnableAll();
options.MigrationsEndPointPath = new PathString(migrationsEndpoint);
app.UseDatabaseErrorPage(options =>
{
options.EnableAll();
options.MigrationsEndPointPath = new PathString(migrationsEndpoint);
});
app.UseMiddleware<PendingMigrationsMiddleware>();
})
.ConfigureServices(services =>
{
services.AddEntityFramework().AddSqlServer();
services.AddScoped<BloggingContextWithMigrations>();
var optionsBuilder = new DbContextOptionsBuilder();
optionsBuilder.UseSqlServer(database.ConnectionString);
services.AddSingleton<DbContextOptions>(optionsBuilder.Options);
});
app.UseMiddleware<PendingMigrationsMiddleware>();
},
services =>
{
services.AddEntityFramework().AddSqlServer();
services.AddScoped<BloggingContextWithMigrations>();
var optionsBuilder = new DbContextOptionsBuilder();
optionsBuilder.UseSqlServer(database.ConnectionString);
services.AddSingleton<DbContextOptions>(optionsBuilder.Options);
});
var server = new TestServer(builder);
HttpResponseMessage response = await server.CreateClient().GetAsync("http://localhost/");
@ -289,26 +294,29 @@ namespace Microsoft.AspNet.Diagnostics.Entity.Tests
{
var logProvider = new TestLoggerProvider();
var server = TestServer.Create(app =>
{
app.UseDatabaseErrorPage();
app.UseMiddleware<ContextNotRegisteredInServicesMiddleware>();
app.ApplicationServices.GetService<ILoggerFactory>().AddProvider(logProvider);
},
services =>
{
services.AddEntityFramework().AddSqlServer();
var optionsBuilder = new DbContextOptionsBuilder();
if (!PlatformHelper.IsMono)
var builder = new WebApplicationBuilder()
.Configure(app =>
{
optionsBuilder.UseSqlServer(database.ConnectionString);
}
else
app.UseDatabaseErrorPage();
app.UseMiddleware<ContextNotRegisteredInServicesMiddleware>();
app.ApplicationServices.GetService<ILoggerFactory>().AddProvider(logProvider);
})
.ConfigureServices(
services =>
{
optionsBuilder.UseInMemoryDatabase();
}
services.AddSingleton<DbContextOptions>(optionsBuilder.Options);
});
services.AddEntityFramework().AddSqlServer();
var optionsBuilder = new DbContextOptionsBuilder();
if (!PlatformHelper.IsMono)
{
optionsBuilder.UseSqlServer(database.ConnectionString);
}
else
{
optionsBuilder.UseInMemoryDatabase();
}
services.AddSingleton<DbContextOptions>(optionsBuilder.Options);
});
var server = new TestServer(builder);
var ex = await Assert.ThrowsAsync<SqlException>(async () =>
await server.CreateClient().GetAsync("http://localhost/"));
@ -404,35 +412,37 @@ namespace Microsoft.AspNet.Diagnostics.Entity.Tests
{
using (var database = SqlServerTestStore.CreateScratch())
{
return TestServer.Create(app =>
{
app.UseDatabaseErrorPage();
app.UseMiddleware<TMiddleware>();
if (logProvider != null)
var builder = new WebApplicationBuilder()
.Configure(app =>
{
app.ApplicationServices.GetService<ILoggerFactory>().AddProvider(logProvider);
}
},
services =>
{
services.AddEntityFramework()
.AddSqlServer();
app.UseDatabaseErrorPage();
services.AddScoped<TContext>();
app.UseMiddleware<TMiddleware>();
var optionsBuilder = new DbContextOptionsBuilder();
if (!PlatformHelper.IsMono)
if (logProvider != null)
{
app.ApplicationServices.GetService<ILoggerFactory>().AddProvider(logProvider);
}
})
.ConfigureServices(services =>
{
optionsBuilder.UseSqlServer(database.ConnectionString);
}
else
{
optionsBuilder.UseInMemoryDatabase();
}
services.AddSingleton(optionsBuilder.Options);
});
services.AddEntityFramework()
.AddSqlServer();
services.AddScoped<TContext>();
var optionsBuilder = new DbContextOptionsBuilder();
if (!PlatformHelper.IsMono)
{
optionsBuilder.UseSqlServer(database.ConnectionString);
}
else
{
optionsBuilder.UseInMemoryDatabase();
}
services.AddSingleton(optionsBuilder.Options);
});
return new TestServer(builder);
}
}

View File

@ -9,6 +9,7 @@ using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Diagnostics.Entity.Tests.Helpers;
using Microsoft.AspNet.Hosting;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.TestHost;
using Microsoft.AspNet.Testing.xunit;
@ -26,9 +27,10 @@ namespace Microsoft.AspNet.Diagnostics.Entity.Tests
[Fact]
public async Task Non_migration_requests_pass_thru()
{
TestServer server = TestServer.Create(app => app
var builder = new WebApplicationBuilder().Configure(app => app
.UseMigrationsEndPoint()
.UseMiddleware<SuccessMiddleware>());
var server = new TestServer(builder);
HttpResponseMessage response = await server.CreateClient().GetAsync("http://localhost/");
@ -71,23 +73,25 @@ namespace Microsoft.AspNet.Diagnostics.Entity.Tests
var path = useCustomPath ? new PathString("/EndPoints/ApplyMyMigrations") : MigrationsEndPointOptions.DefaultPath;
TestServer server = TestServer.Create(app =>
{
if (useCustomPath)
var builder = new WebApplicationBuilder()
.Configure(app =>
{
app.UseMigrationsEndPoint(o => o.Path = path);
}
else
if (useCustomPath)
{
app.UseMigrationsEndPoint(o => o.Path = path);
}
else
{
app.UseMigrationsEndPoint();
}
})
.ConfigureServices(services =>
{
app.UseMigrationsEndPoint();
}
},
services =>
{
services.AddEntityFramework().AddSqlServer();
services.AddScoped<BloggingContextWithMigrations>();
services.AddSingleton(optionsBuilder.Options);
});
services.AddEntityFramework().AddSqlServer();
services.AddScoped<BloggingContextWithMigrations>();
services.AddSingleton(optionsBuilder.Options);
});
var server = new TestServer(builder);
using (var db = BloggingContextWithMigrations.CreateWithoutExternalServiceProvider(optionsBuilder.Options))
{
@ -118,10 +122,12 @@ namespace Microsoft.AspNet.Diagnostics.Entity.Tests
[Fact]
public async Task Context_type_not_specified()
{
var server = TestServer.Create(app =>
{
app.UseMigrationsEndPoint();
});
var builder = new WebApplicationBuilder()
.Configure(app =>
{
app.UseMigrationsEndPoint();
});
var server = new TestServer(builder);
var formData = new FormUrlEncodedContent(new List<KeyValuePair<string, string>>());
@ -136,10 +142,12 @@ namespace Microsoft.AspNet.Diagnostics.Entity.Tests
[Fact]
public async Task Invalid_context_type_specified()
{
var server = TestServer.Create(app =>
{
app.UseMigrationsEndPoint();
});
var builder = new WebApplicationBuilder()
.Configure(app =>
{
app.UseMigrationsEndPoint();
});
var server = new TestServer(builder);
var typeName = "You won't find this type ;)";
var formData = new FormUrlEncodedContent(new List<KeyValuePair<string, string>>
@ -158,9 +166,10 @@ namespace Microsoft.AspNet.Diagnostics.Entity.Tests
[Fact]
public async Task Context_not_registered_in_services()
{
var server = TestServer.Create(
app => app.UseMigrationsEndPoint(),
services => services.AddEntityFramework().AddSqlServer());
var builder = new WebApplicationBuilder()
.Configure(app => app.UseMigrationsEndPoint())
.ConfigureServices(services => services.AddEntityFramework().AddSqlServer());
var server = new TestServer(builder);
var formData = new FormUrlEncodedContent(new List<KeyValuePair<string, string>>
{
@ -184,14 +193,15 @@ namespace Microsoft.AspNet.Diagnostics.Entity.Tests
var optionsBuilder = new DbContextOptionsBuilder();
optionsBuilder.UseSqlServer(database.ConnectionString);
TestServer server = TestServer.Create(
app => app.UseMigrationsEndPoint(),
services =>
var builder = new WebApplicationBuilder()
.Configure(app => app.UseMigrationsEndPoint())
.ConfigureServices(services =>
{
services.AddEntityFramework().AddSqlServer();
services.AddScoped<BloggingContextWithSnapshotThatThrows>();
services.AddSingleton(optionsBuilder.Options);
});
var server = new TestServer(builder);
var formData = new FormUrlEncodedContent(new List<KeyValuePair<string, string>>
{

View File

@ -13,6 +13,7 @@ using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.FileProviders;
using Microsoft.AspNet.Hosting;
using Microsoft.AspNet.TestHost;
using Microsoft.AspNet.Testing;
using Microsoft.Extensions.PlatformAbstractions;
@ -485,15 +486,17 @@ namespace Microsoft.AspNet.Diagnostics
{
// Arrange
DiagnosticListener diagnosticListener = null;
var server = TestServer.Create(app =>
{
diagnosticListener = app.ApplicationServices.GetRequiredService<DiagnosticListener>();
app.UseDeveloperExceptionPage();
app.Run(context =>
var builder = new WebApplicationBuilder()
.Configure(app =>
{
throw new Exception("Test exception");
diagnosticListener = app.ApplicationServices.GetRequiredService<DiagnosticListener>();
app.UseDeveloperExceptionPage();
app.Run(context =>
{
throw new Exception("Test exception");
});
});
});
var server = new TestServer(builder);
var listener = new TestDiagnosticListener();
diagnosticListener.SubscribeWithAdapter(listener);

View File

@ -10,6 +10,7 @@ using System.Net;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Hosting;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.TestHost;
using Microsoft.Extensions.DependencyInjection;
@ -25,25 +26,28 @@ namespace Microsoft.AspNet.Diagnostics
[InlineData(HttpStatusCode.InternalServerError)]
public async Task OnlyHandles_UnhandledExceptions(HttpStatusCode expectedStatusCode)
{
using (var server = TestServer.Create(app =>
{
app.UseExceptionHandler("/handle-errors");
app.Map("/handle-errors", (innerAppBuilder) =>
var builder = new WebApplicationBuilder()
.Configure(app =>
{
innerAppBuilder.Run(async (httpContext) =>
app.UseExceptionHandler("/handle-errors");
app.Map("/handle-errors", (innerAppBuilder) =>
{
await httpContext.Response.WriteAsync("Handled error in a custom way.");
innerAppBuilder.Run(async (httpContext) =>
{
await httpContext.Response.WriteAsync("Handled error in a custom way.");
});
});
app.Run((RequestDelegate)(async (context) =>
{
context.Response.StatusCode = (int)expectedStatusCode;
context.Response.ContentType = "text/plain; charset=utf-8";
await context.Response.WriteAsync("An error occurred while adding a product");
}));
});
app.Run((RequestDelegate)(async (context) =>
{
context.Response.StatusCode = (int)expectedStatusCode;
context.Response.ContentType = "text/plain; charset=utf-8";
await context.Response.WriteAsync("An error occurred while adding a product");
}));
}))
using (var server = new TestServer(builder))
{
var client = server.CreateClient();
var response = await client.GetAsync(string.Empty);
@ -55,40 +59,43 @@ namespace Microsoft.AspNet.Diagnostics
[Fact]
public async Task DoesNotHandle_UnhandledExceptions_WhenResponseAlreadyStarted()
{
using (var server = TestServer.Create(app =>
{
app.Use(async (httpContext, next) =>
var builder = new WebApplicationBuilder()
.Configure(app =>
{
Exception exception = null;
try
app.Use(async (httpContext, next) =>
{
await next();
}
catch (InvalidOperationException ex)
Exception exception = null;
try
{
await next();
}
catch (InvalidOperationException ex)
{
exception = ex;
}
Assert.NotNull(exception);
Assert.Equal("Something bad happened", exception.Message);
});
app.UseExceptionHandler("/handle-errors");
app.Map("/handle-errors", (innerAppBuilder) =>
{
exception = ex;
}
innerAppBuilder.Run(async (httpContext) =>
{
await httpContext.Response.WriteAsync("Handled error in a custom way.");
});
});
Assert.NotNull(exception);
Assert.Equal("Something bad happened", exception.Message);
});
app.UseExceptionHandler("/handle-errors");
app.Map("/handle-errors", (innerAppBuilder) =>
{
innerAppBuilder.Run(async (httpContext) =>
app.Run(async (httpContext) =>
{
await httpContext.Response.WriteAsync("Handled error in a custom way.");
await httpContext.Response.WriteAsync("Hello");
throw new InvalidOperationException("Something bad happened");
});
});
app.Run(async (httpContext) =>
{
await httpContext.Response.WriteAsync("Hello");
throw new InvalidOperationException("Something bad happened");
});
}))
using (var server = new TestServer(builder))
{
var client = server.CreateClient();
var response = await client.GetAsync(string.Empty);
@ -101,51 +108,54 @@ namespace Microsoft.AspNet.Diagnostics
public async Task ClearsResponseBuffer_BeforeRequestIsReexecuted()
{
var expectedResponseBody = "New response body";
using (var server = TestServer.Create(app =>
{
// add response buffering
app.Use(async (httpContext, next) =>
var builder = new WebApplicationBuilder()
.Configure(app =>
{
var response = httpContext.Response;
var originalResponseBody = response.Body;
var bufferingStream = new MemoryStream();
response.Body = bufferingStream;
try
// add response buffering
app.Use(async (httpContext, next) =>
{
await next();
var response = httpContext.Response;
var originalResponseBody = response.Body;
var bufferingStream = new MemoryStream();
response.Body = bufferingStream;
response.Body = originalResponseBody;
bufferingStream.Seek(0, SeekOrigin.Begin);
await bufferingStream.CopyToAsync(response.Body);
}
finally
try
{
await next();
response.Body = originalResponseBody;
bufferingStream.Seek(0, SeekOrigin.Begin);
await bufferingStream.CopyToAsync(response.Body);
}
finally
{
response.Body = originalResponseBody;
}
});
app.UseExceptionHandler("/handle-errors");
app.Map("/handle-errors", (innerAppBuilder) =>
{
response.Body = originalResponseBody;
}
});
innerAppBuilder.Run(async (httpContext) =>
{
Assert.True(httpContext.Response.Body.CanSeek);
Assert.Equal(0, httpContext.Response.Body.Position);
app.UseExceptionHandler("/handle-errors");
await httpContext.Response.WriteAsync(expectedResponseBody);
});
});
app.Map("/handle-errors", (innerAppBuilder) =>
{
innerAppBuilder.Run(async (httpContext) =>
app.Run(async (context) =>
{
Assert.True(httpContext.Response.Body.CanSeek);
Assert.Equal(0, httpContext.Response.Body.Position);
// Write some content into the response before throwing exception
await context.Response.WriteAsync(new string('a', 100));
await httpContext.Response.WriteAsync(expectedResponseBody);
throw new InvalidOperationException("Invalid input provided.");
});
});
app.Run(async (context) =>
{
// Write some content into the response before throwing exception
await context.Response.WriteAsync(new string('a', 100));
throw new InvalidOperationException("Invalid input provided.");
});
}))
using (var server = new TestServer(builder))
{
var client = server.CreateClient();
var response = await client.GetAsync(string.Empty);
@ -170,29 +180,32 @@ namespace Microsoft.AspNet.Diagnostics
{
var expiresTime = DateTime.UtcNow.AddDays(5).ToString("R");
var expectedResponseBody = "Handled error in a custom way.";
using (var server = TestServer.Create(app =>
{
app.UseExceptionHandler("/handle-errors");
app.Map("/handle-errors", (innerAppBuilder) =>
var builder = new WebApplicationBuilder()
.Configure(app =>
{
innerAppBuilder.Run(async (httpContext) =>
{
httpContext.Response.Headers.Add("Cache-Control", new[] { "max-age=600" });
httpContext.Response.Headers.Add("Pragma", new[] { "max-age=600" });
httpContext.Response.Headers.Add(
"Expires", new[] { expiresTime });
httpContext.Response.Headers.Add("ETag", new[] { "12345" });
app.UseExceptionHandler("/handle-errors");
await httpContext.Response.WriteAsync(expectedResponseBody);
app.Map("/handle-errors", (innerAppBuilder) =>
{
innerAppBuilder.Run(async (httpContext) =>
{
httpContext.Response.Headers.Add("Cache-Control", new[] { "max-age=600" });
httpContext.Response.Headers.Add("Pragma", new[] { "max-age=600" });
httpContext.Response.Headers.Add(
"Expires", new[] { expiresTime });
httpContext.Response.Headers.Add("ETag", new[] { "12345" });
await httpContext.Response.WriteAsync(expectedResponseBody);
});
});
app.Run((context) =>
{
throw new InvalidOperationException("Invalid input provided.");
});
});
app.Run((context) =>
{
throw new InvalidOperationException("Invalid input provided.");
});
}))
using (var server = new TestServer(builder))
{
var client = server.CreateClient();
var response = await client.GetAsync(string.Empty);
@ -217,28 +230,31 @@ namespace Microsoft.AspNet.Diagnostics
{
var expiresTime = DateTime.UtcNow.AddDays(10).ToString("R");
var expectedResponseBody = "Hello world!";
using (var server = TestServer.Create(app =>
{
app.UseExceptionHandler("/handle-errors");
app.Map("/handle-errors", (innerAppBuilder) =>
var builder = new WebApplicationBuilder()
.Configure(app =>
{
innerAppBuilder.Run(async (httpContext) =>
app.UseExceptionHandler("/handle-errors");
app.Map("/handle-errors", (innerAppBuilder) =>
{
await httpContext.Response.WriteAsync("Handled error in a custom way.");
innerAppBuilder.Run(async (httpContext) =>
{
await httpContext.Response.WriteAsync("Handled error in a custom way.");
});
});
app.Run(async (httpContext) =>
{
httpContext.Response.Headers.Add("Cache-Control", new[] { "max-age=3600" });
httpContext.Response.Headers.Add("Pragma", new[] { "max-age=3600" });
httpContext.Response.Headers.Add("Expires", new[] { expiresTime });
httpContext.Response.Headers.Add("ETag", new[] { "abcdef" });
await httpContext.Response.WriteAsync(expectedResponseBody);
});
});
app.Run(async (httpContext) =>
{
httpContext.Response.Headers.Add("Cache-Control", new[] { "max-age=3600" });
httpContext.Response.Headers.Add("Pragma", new[] { "max-age=3600" });
httpContext.Response.Headers.Add("Expires", new[] { expiresTime });
httpContext.Response.Headers.Add("ETag", new[] { "abcdef" });
await httpContext.Response.WriteAsync(expectedResponseBody);
});
}))
using (var server = new TestServer(builder))
{
var client = server.CreateClient();
var response = await client.GetAsync(string.Empty);
@ -264,46 +280,49 @@ namespace Microsoft.AspNet.Diagnostics
public async Task DoesNotClearCacheHeaders_WhenResponseHasAlreadyStarted()
{
var expiresTime = DateTime.UtcNow.AddDays(10).ToString("R");
using (var server = TestServer.Create(app =>
{
app.Use(async (httpContext, next) =>
var builder = new WebApplicationBuilder()
.Configure(app =>
{
Exception exception = null;
try
app.Use(async (httpContext, next) =>
{
await next();
}
catch (InvalidOperationException ex)
Exception exception = null;
try
{
await next();
}
catch (InvalidOperationException ex)
{
exception = ex;
}
Assert.NotNull(exception);
Assert.Equal("Something bad happened", exception.Message);
});
app.UseExceptionHandler("/handle-errors");
app.Map("/handle-errors", (innerAppBuilder) =>
{
exception = ex;
}
innerAppBuilder.Run(async (httpContext) =>
{
await httpContext.Response.WriteAsync("Handled error in a custom way.");
});
});
Assert.NotNull(exception);
Assert.Equal("Something bad happened", exception.Message);
});
app.UseExceptionHandler("/handle-errors");
app.Map("/handle-errors", (innerAppBuilder) =>
{
innerAppBuilder.Run(async (httpContext) =>
app.Run(async (httpContext) =>
{
await httpContext.Response.WriteAsync("Handled error in a custom way.");
httpContext.Response.Headers.Add("Cache-Control", new[] { "max-age=3600" });
httpContext.Response.Headers.Add("Pragma", new[] { "max-age=3600" });
httpContext.Response.Headers.Add("Expires", new[] { expiresTime });
httpContext.Response.Headers.Add("ETag", new[] { "abcdef" });
await httpContext.Response.WriteAsync("Hello");
throw new InvalidOperationException("Something bad happened");
});
});
app.Run(async (httpContext) =>
{
httpContext.Response.Headers.Add("Cache-Control", new[] { "max-age=3600" });
httpContext.Response.Headers.Add("Pragma", new[] { "max-age=3600" });
httpContext.Response.Headers.Add("Expires", new[] { expiresTime });
httpContext.Response.Headers.Add("ETag", new[] { "abcdef" });
await httpContext.Response.WriteAsync("Hello");
throw new InvalidOperationException("Something bad happened");
});
}))
using (var server = new TestServer(builder))
{
var client = server.CreateClient();
var response = await client.GetAsync(string.Empty);
@ -331,23 +350,25 @@ namespace Microsoft.AspNet.Diagnostics
// Arrange
DiagnosticListener diagnosticListener = null;
var server = TestServer.Create(app =>
{
diagnosticListener = app.ApplicationServices.GetRequiredService<DiagnosticListener>();
app.UseExceptionHandler("/handle-errors");
app.Map("/handle-errors", (innerAppBuilder) =>
var builder = new WebApplicationBuilder()
.Configure(app =>
{
innerAppBuilder.Run(async (httpContext) =>
diagnosticListener = app.ApplicationServices.GetRequiredService<DiagnosticListener>();
app.UseExceptionHandler("/handle-errors");
app.Map("/handle-errors", (innerAppBuilder) =>
{
await httpContext.Response.WriteAsync("Handled error in a custom way.");
innerAppBuilder.Run(async (httpContext) =>
{
await httpContext.Response.WriteAsync("Handled error in a custom way.");
});
});
app.Run(context =>
{
throw new Exception("Test exception");
});
});
app.Run(context =>
{
throw new Exception("Test exception");
});
});
var server = new TestServer(builder);
var listener = new TestDiagnosticListener();
diagnosticListener.SubscribeWithAdapter(listener);

View File

@ -5,6 +5,7 @@ using System;
using System.Diagnostics;
using System.Threading.Tasks;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Hosting;
using Microsoft.AspNet.TestHost;
using Microsoft.Extensions.DependencyInjection;
using Xunit;
@ -18,16 +19,19 @@ namespace Microsoft.AspNet.MiddlewareAnalysis
{
DiagnosticListener diagnosticListener = null;
var server = TestServer.Create(app =>
{
diagnosticListener = app.ApplicationServices.GetRequiredService<DiagnosticListener>();
app.UseDeveloperExceptionPage();
app.Run(context =>
var builder = new WebApplicationBuilder()
.Configure(app =>
{
throw new Exception("Test exception");
});
}, services => services.AddMiddlewareAnalysis());
diagnosticListener = app.ApplicationServices.GetRequiredService<DiagnosticListener>();
app.UseDeveloperExceptionPage();
app.Run(context =>
{
throw new Exception("Test exception");
});
})
.ConfigureServices(services => services.AddMiddlewareAnalysis());
var server = new TestServer(builder);
var listener = new TestDiagnosticListener();
diagnosticListener.SubscribeWithAdapter(listener);