From bd10d507f8d9530d601ed17edce1c6d0b0e19f15 Mon Sep 17 00:00:00 2001 From: John Luo Date: Wed, 16 Dec 2015 12:34:36 -0800 Subject: [PATCH] Reacting to new Hosting API --- .../Microsoft.AspNet.Hosting.ini | 3 -- samples/LargeResponseApp/Startup.cs | 13 ++++- samples/LargeResponseApp/hosting.json | 4 ++ samples/LargeResponseApp/project.json | 8 ++-- samples/SampleApp/Program.cs | 17 ------- samples/SampleApp/Startup.cs | 16 +++++++ samples/SampleApp/hosting.json | 4 ++ samples/SampleApp/project.json | 37 ++++++++------- samples/SampleApp/wwwroot/web.config | 9 ++++ .../Program.cs | 17 ------- .../AddressRegistrationTests.cs | 9 ++-- .../PathBaseTests.cs | 5 +- .../RequestTests.cs | 47 ++++++++++--------- .../ResponseTests.cs | 38 ++++++++------- .../ThreadCountTests.cs | 23 ++++----- 15 files changed, 133 insertions(+), 117 deletions(-) delete mode 100644 samples/LargeResponseApp/Microsoft.AspNet.Hosting.ini create mode 100644 samples/LargeResponseApp/hosting.json delete mode 100644 samples/SampleApp/Program.cs create mode 100644 samples/SampleApp/hosting.json create mode 100644 samples/SampleApp/wwwroot/web.config delete mode 100644 src/Microsoft.AspNet.Server.Kestrel/Program.cs diff --git a/samples/LargeResponseApp/Microsoft.AspNet.Hosting.ini b/samples/LargeResponseApp/Microsoft.AspNet.Hosting.ini deleted file mode 100644 index c566ee4f7d..0000000000 --- a/samples/LargeResponseApp/Microsoft.AspNet.Hosting.ini +++ /dev/null @@ -1,3 +0,0 @@ - -Server = Microsoft.AspNet.Server.Kestrel -Server.Urls = http://localhost:5001/ diff --git a/samples/LargeResponseApp/Startup.cs b/samples/LargeResponseApp/Startup.cs index 6bae65f890..5052e96f59 100644 --- a/samples/LargeResponseApp/Startup.cs +++ b/samples/LargeResponseApp/Startup.cs @@ -1,9 +1,10 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. -using Microsoft.AspNet.Builder; using System.Text; using System.Threading.Tasks; +using Microsoft.AspNet.Builder; +using Microsoft.AspNet.Hosting; namespace LargeResponseApp { @@ -34,5 +35,15 @@ namespace LargeResponseApp } }); } + + public static void Main(string[] args) + { + var application = new WebApplicationBuilder() + .UseConfiguration(WebApplicationConfiguration.GetDefault(args)) + .UseStartup() + .Build(); + + application.Run(); + } } } diff --git a/samples/LargeResponseApp/hosting.json b/samples/LargeResponseApp/hosting.json new file mode 100644 index 0000000000..7e1c57a3f0 --- /dev/null +++ b/samples/LargeResponseApp/hosting.json @@ -0,0 +1,4 @@ +{ + "server": "Microsoft.AspNet.Server.Kestrel", + "server.urls": "http://localhost:5001/" +} \ No newline at end of file diff --git a/samples/LargeResponseApp/project.json b/samples/LargeResponseApp/project.json index 7754a7b6f7..2fb9fdb1a0 100644 --- a/samples/LargeResponseApp/project.json +++ b/samples/LargeResponseApp/project.json @@ -3,14 +3,14 @@ "dependencies": { "Microsoft.AspNet.Server.Kestrel": "1.0.0-*" }, - + "compilationOptions": { + "emitEntryPoint": true + }, "frameworks": { "dnx451": { }, "dnxcore50": { } }, - "commands": { - "run": "Microsoft.AspNet.Server.Kestrel", - "web": "Microsoft.AspNet.Hosting" + "web": "LargeResponseApp" } } diff --git a/samples/SampleApp/Program.cs b/samples/SampleApp/Program.cs deleted file mode 100644 index 16e2d1f487..0000000000 --- a/samples/SampleApp/Program.cs +++ /dev/null @@ -1,17 +0,0 @@ -// Copyright (c) .NET Foundation. 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; - -namespace SampleApp -{ - public class Program - { - public static void Main(string[] args) - { - var mergedArgs = new[] { "--server", "Microsoft.AspNet.Server.Kestrel" }.Concat(args).ToArray(); - Microsoft.AspNet.Hosting.Program.Main(mergedArgs); - } - } -} diff --git a/samples/SampleApp/Startup.cs b/samples/SampleApp/Startup.cs index b6004d9c0c..8d4b97eda7 100644 --- a/samples/SampleApp/Startup.cs +++ b/samples/SampleApp/Startup.cs @@ -5,6 +5,7 @@ using System; using System.IO; using System.Security.Cryptography.X509Certificates; using Microsoft.AspNet.Builder; +using Microsoft.AspNet.Hosting; using Microsoft.AspNet.Http; using Microsoft.AspNet.Http.Features; using Microsoft.AspNet.Server.Kestrel; @@ -61,5 +62,20 @@ namespace SampleApp await context.Response.WriteAsync("Hello world"); }); } + + public static void Main(string[] args) + { + var application = new WebApplicationBuilder() + .UseConfiguration(WebApplicationConfiguration.GetDefault(args)) + .UseStartup() + .Build(); + + // The following section should be used to demo sockets + //var addresses = application.GetAddresses(); + //addresses.Clear(); + //addresses.Add("http://unix:/tmp/kestrel-test.sock"); + + application.Run(); + } } } \ No newline at end of file diff --git a/samples/SampleApp/hosting.json b/samples/SampleApp/hosting.json new file mode 100644 index 0000000000..66f354cbc2 --- /dev/null +++ b/samples/SampleApp/hosting.json @@ -0,0 +1,4 @@ +{ + "server": "Microsoft.AspNet.Server.Kestrel", + "server.urls": "http://localhost:5000;https://localhost:5001" +} diff --git a/samples/SampleApp/project.json b/samples/SampleApp/project.json index f9e44a2a05..42f19ecb21 100644 --- a/samples/SampleApp/project.json +++ b/samples/SampleApp/project.json @@ -1,22 +1,23 @@ { - "version": "1.0.0-*", - "dependencies": { - "Microsoft.AspNet.Server.Kestrel": "1.0.0-*", - "Microsoft.AspNet.Server.Kestrel.Https": "1.0.0-*", - "Microsoft.Extensions.Logging.Console": "1.0.0-*" + "version": "1.0.0-*", + "dependencies": { + "Microsoft.AspNet.Server.Kestrel": "1.0.0-*", + "Microsoft.AspNet.Server.Kestrel.Https": "1.0.0-*", + "Microsoft.Extensions.Logging.Console": "1.0.0-*" + }, + "compilationOptions": { + "emitEntryPoint": true + }, + "frameworks": { + "dnx451": { }, - "frameworks": { - "dnx451": { - }, - "dnxcore50": { - "dependencies": { - "System.Console": "4.0.0-*" - } - } - }, - "commands": { - "web": "Microsoft.AspNet.Hosting --server Microsoft.AspNet.Server.Kestrel --server.urls http://localhost:5000;https://localhost:5001", - "kestrel": "Microsoft.AspNet.Server.Kestrel --server.urls http://localhost:5000;https://localhost:5001", - "run-socket": "Microsoft.AspNet.Server.Kestrel --server.urls http://unix:/tmp/kestrel-test.sock" + "dnxcore50": { + "dependencies": { + "System.Console": "4.0.0-*" + } } + }, + "commands": { + "web": "SampleApp" + } } diff --git a/samples/SampleApp/wwwroot/web.config b/samples/SampleApp/wwwroot/web.config new file mode 100644 index 0000000000..9a0d90abf8 --- /dev/null +++ b/samples/SampleApp/wwwroot/web.config @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/src/Microsoft.AspNet.Server.Kestrel/Program.cs b/src/Microsoft.AspNet.Server.Kestrel/Program.cs deleted file mode 100644 index b0f18d7c30..0000000000 --- a/src/Microsoft.AspNet.Server.Kestrel/Program.cs +++ /dev/null @@ -1,17 +0,0 @@ -// Copyright (c) .NET Foundation. 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; - -namespace Microsoft.AspNet.Server.Kestrel -{ - public class Program - { - public static void Main(string[] args) - { - var mergedArgs = new[] { "--server", "Microsoft.AspNet.Server.Kestrel" }.Concat(args).ToArray(); - Microsoft.AspNet.Hosting.Program.Main(mergedArgs); - } - } -} diff --git a/test/Microsoft.AspNet.Server.Kestrel.FunctionalTests/AddressRegistrationTests.cs b/test/Microsoft.AspNet.Server.Kestrel.FunctionalTests/AddressRegistrationTests.cs index 9921a3d7fc..da0cd8d1f9 100644 --- a/test/Microsoft.AspNet.Server.Kestrel.FunctionalTests/AddressRegistrationTests.cs +++ b/test/Microsoft.AspNet.Server.Kestrel.FunctionalTests/AddressRegistrationTests.cs @@ -40,11 +40,12 @@ namespace Microsoft.AspNet.Server.Kestrel.FunctionalTests }) .Build(); - var hostBuilder = new WebHostBuilder(config); - hostBuilder.UseServerFactory("Microsoft.AspNet.Server.Kestrel"); - hostBuilder.UseStartup(ConfigureEchoAddress); + var applicationBuilder = new WebApplicationBuilder() + .UseConfiguration(config) + .UseServerFactory("Microsoft.AspNet.Server.Kestrel") + .Configure(ConfigureEchoAddress); - using (var app = hostBuilder.Build().Start()) + using (var app = applicationBuilder.Build().Start()) { using (var client = new HttpClient()) { diff --git a/test/Microsoft.AspNet.Server.Kestrel.FunctionalTests/PathBaseTests.cs b/test/Microsoft.AspNet.Server.Kestrel.FunctionalTests/PathBaseTests.cs index 95df0477ab..700150e2aa 100644 --- a/test/Microsoft.AspNet.Server.Kestrel.FunctionalTests/PathBaseTests.cs +++ b/test/Microsoft.AspNet.Server.Kestrel.FunctionalTests/PathBaseTests.cs @@ -79,9 +79,10 @@ namespace Microsoft.AspNet.Server.Kestrel.FunctionalTests { "server.urls", registerAddress } }).Build(); - var builder = new WebHostBuilder(config) + var builder = new WebApplicationBuilder() + .UseConfiguration(config) .UseServerFactory("Microsoft.AspNet.Server.Kestrel") - .UseStartup(app => + .Configure(app => { app.Run(async context => { diff --git a/test/Microsoft.AspNet.Server.Kestrel.FunctionalTests/RequestTests.cs b/test/Microsoft.AspNet.Server.Kestrel.FunctionalTests/RequestTests.cs index 861a2a84c3..a74f999eaf 100644 --- a/test/Microsoft.AspNet.Server.Kestrel.FunctionalTests/RequestTests.cs +++ b/test/Microsoft.AspNet.Server.Kestrel.FunctionalTests/RequestTests.cs @@ -29,31 +29,32 @@ namespace Microsoft.AspNet.Server.Kestrel.FunctionalTests }) .Build(); - var hostBuilder = new WebHostBuilder(config); - hostBuilder.UseServerFactory("Microsoft.AspNet.Server.Kestrel"); - hostBuilder.UseStartup(app => - { - app.Run(async context => + var applicationBuilder = new WebApplicationBuilder() + .UseConfiguration(config) + .UseServerFactory("Microsoft.AspNet.Server.Kestrel") + .Configure(app => { - // Read the full request body - var total = 0; - var bytes = new byte[1024]; - var count = await context.Request.Body.ReadAsync(bytes, 0, bytes.Length); - while (count > 0) + app.Run(async context => { - for (int i = 0; i < count; i++) + // Read the full request body + var total = 0; + var bytes = new byte[1024]; + var count = await context.Request.Body.ReadAsync(bytes, 0, bytes.Length); + while (count > 0) { - Assert.Equal(total % 256, bytes[i]); - total++; + for (int i = 0; i < count; i++) + { + Assert.Equal(total % 256, bytes[i]); + total++; + } + count = await context.Request.Body.ReadAsync(bytes, 0, bytes.Length); } - count = await context.Request.Body.ReadAsync(bytes, 0, bytes.Length); - } - await context.Response.WriteAsync(total.ToString(CultureInfo.InvariantCulture)); + await context.Response.WriteAsync(total.ToString(CultureInfo.InvariantCulture)); + }); }); - }); - using (var app = hostBuilder.Build().Start()) + using (var app = applicationBuilder.Build().Start()) { using (var client = new HttpClient()) { @@ -97,9 +98,10 @@ namespace Microsoft.AspNet.Server.Kestrel.FunctionalTests { "server.urls", "http://localhost:8791" } }).Build(); - var builder = new WebHostBuilder(config) + var builder = new WebApplicationBuilder() + .UseConfiguration(config) .UseServerFactory("Microsoft.AspNet.Server.Kestrel") - .UseStartup(app => + .Configure(app => { app.Run(async context => { @@ -126,9 +128,10 @@ namespace Microsoft.AspNet.Server.Kestrel.FunctionalTests { "server.urls", $"http://{registerAddress}:{port}" } }).Build(); - var builder = new WebHostBuilder(config) + var builder = new WebApplicationBuilder() + .UseConfiguration(config) .UseServerFactory("Microsoft.AspNet.Server.Kestrel") - .UseStartup(app => + .Configure(app => { app.Run(async context => { diff --git a/test/Microsoft.AspNet.Server.Kestrel.FunctionalTests/ResponseTests.cs b/test/Microsoft.AspNet.Server.Kestrel.FunctionalTests/ResponseTests.cs index 67e8203e05..b1db1a5091 100644 --- a/test/Microsoft.AspNet.Server.Kestrel.FunctionalTests/ResponseTests.cs +++ b/test/Microsoft.AspNet.Server.Kestrel.FunctionalTests/ResponseTests.cs @@ -28,28 +28,29 @@ namespace Microsoft.AspNet.Server.Kestrel.FunctionalTests }) .Build(); - var hostBuilder = new WebHostBuilder(config); - hostBuilder.UseServerFactory("Microsoft.AspNet.Server.Kestrel"); - hostBuilder.UseStartup(app => - { - app.Run(async context => + var applicationBuilder = new WebApplicationBuilder() + .UseConfiguration(config) + .UseServerFactory("Microsoft.AspNet.Server.Kestrel") + .Configure(app => { - var bytes = new byte[1024]; - for (int i = 0; i < bytes.Length; i++) + app.Run(async context => { - bytes[i] = (byte)i; - } + var bytes = new byte[1024]; + for (int i = 0; i < bytes.Length; i++) + { + bytes[i] = (byte)i; + } - context.Response.ContentLength = bytes.Length * 1024; + context.Response.ContentLength = bytes.Length * 1024; - for (int i = 0; i < 1024; i++) - { - await context.Response.Body.WriteAsync(bytes, 0, bytes.Length); - } + for (int i = 0; i < 1024; i++) + { + await context.Response.Body.WriteAsync(bytes, 0, bytes.Length); + } + }); }); - }); - using (var app = hostBuilder.Build().Start()) + using (var app = applicationBuilder.Build().Start()) { using (var client = new HttpClient()) { @@ -85,9 +86,10 @@ namespace Microsoft.AspNet.Server.Kestrel.FunctionalTests }) .Build(); - var hostBuilder = new WebHostBuilder(config) + var hostBuilder = new WebApplicationBuilder() + .UseConfiguration(config) .UseServerFactory("Microsoft.AspNet.Server.Kestrel") - .UseStartup(app => + .Configure(app => { app.Run(async context => { diff --git a/test/Microsoft.AspNet.Server.Kestrel.FunctionalTests/ThreadCountTests.cs b/test/Microsoft.AspNet.Server.Kestrel.FunctionalTests/ThreadCountTests.cs index 4bd434df68..f67cff786f 100644 --- a/test/Microsoft.AspNet.Server.Kestrel.FunctionalTests/ThreadCountTests.cs +++ b/test/Microsoft.AspNet.Server.Kestrel.FunctionalTests/ThreadCountTests.cs @@ -24,19 +24,20 @@ namespace Microsoft.AspNet.Server.Kestrel.FunctionalTests }) .Build(); - var hostBuilder = new WebHostBuilder(config); - hostBuilder.UseServerFactory("Microsoft.AspNet.Server.Kestrel"); - hostBuilder.UseStartup(app => - { - var serverInfo = app.ServerFeatures.Get(); - serverInfo.ThreadCount = threadCount; - app.Run(context => + var applicationBuilder = new WebApplicationBuilder() + .UseConfiguration(config) + .UseServerFactory("Microsoft.AspNet.Server.Kestrel") + .Configure(app => { - return context.Response.WriteAsync("Hello World"); - }); - }); + var serverInfo = app.ServerFeatures.Get(); + serverInfo.ThreadCount = threadCount; + app.Run(context => + { + return context.Response.WriteAsync("Hello World"); + }); + }); - using (var app = hostBuilder.Build().Start()) + using (var app = applicationBuilder.Build().Start()) { using (var client = new HttpClient()) {