diff --git a/MetaPackages.sln b/MetaPackages.sln index c5c63d90c3..6de1068cba 100644 --- a/MetaPackages.sln +++ b/MetaPackages.sln @@ -20,8 +20,13 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "build", "build", "{F92CB7A1 build\repo.targets = build\repo.targets EndProjectSection EndProject + Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.RuntimeStore", "src\Microsoft.AspNetCore.RuntimeStore\Microsoft.AspNetCore.RuntimeStore.csproj", "{A4585E19-FC49-43B4-9416-0BD3120EAD32}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{192F583C-C4CA-43E5-B31C-D21B7806E274}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SampleApp", "samples\SampleApp\SampleApp.csproj", "{AF5BB04E-92F7-4737-8B98-F86F6244FAB2}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -36,6 +41,10 @@ Global {CC8F551E-213A-45E8-AECA-507C4DB4F164}.Debug|Any CPU.Build.0 = Debug|Any CPU {CC8F551E-213A-45E8-AECA-507C4DB4F164}.Release|Any CPU.ActiveCfg = Release|Any CPU {CC8F551E-213A-45E8-AECA-507C4DB4F164}.Release|Any CPU.Build.0 = Release|Any CPU + {AF5BB04E-92F7-4737-8B98-F86F6244FAB2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {AF5BB04E-92F7-4737-8B98-F86F6244FAB2}.Debug|Any CPU.Build.0 = Debug|Any CPU + {AF5BB04E-92F7-4737-8B98-F86F6244FAB2}.Release|Any CPU.ActiveCfg = Release|Any CPU + {AF5BB04E-92F7-4737-8B98-F86F6244FAB2}.Release|Any CPU.Build.0 = Release|Any CPU {A4585E19-FC49-43B4-9416-0BD3120EAD32}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {A4585E19-FC49-43B4-9416-0BD3120EAD32}.Debug|Any CPU.Build.0 = Debug|Any CPU {A4585E19-FC49-43B4-9416-0BD3120EAD32}.Release|Any CPU.ActiveCfg = Release|Any CPU @@ -48,6 +57,7 @@ Global {6F3D43F7-9546-4B41-AF04-CF4708B62051} = {ED834E68-51C3-4ADE-ACC8-6BA6D4207C09} {CC8F551E-213A-45E8-AECA-507C4DB4F164} = {ED834E68-51C3-4ADE-ACC8-6BA6D4207C09} {F92CB7A1-C38E-408C-A7EC-A5C040D041E1} = {97D53BEB-A511-4FBE-B784-AB407D9A219F} + {AF5BB04E-92F7-4737-8B98-F86F6244FAB2} = {192F583C-C4CA-43E5-B31C-D21B7806E274} {A4585E19-FC49-43B4-9416-0BD3120EAD32} = {ED834E68-51C3-4ADE-ACC8-6BA6D4207C09} EndGlobalSection EndGlobal diff --git a/build/Key.snk b/build/Key.snk new file mode 100644 index 0000000000..e10e4889c1 Binary files /dev/null and b/build/Key.snk differ diff --git a/build/common.props b/build/common.props index 30a0a74a67..dfd6c25f34 100644 --- a/build/common.props +++ b/build/common.props @@ -31,6 +31,8 @@ + + @@ -129,10 +131,8 @@ - - diff --git a/build/dependencies.props b/build/dependencies.props index b8b54468e7..f5484bca6f 100644 --- a/build/dependencies.props +++ b/build/dependencies.props @@ -5,5 +5,6 @@ 4.3.0 2.0.0-* 1.6.1 + 2.0.0-* \ No newline at end of file diff --git a/samples/SampleApp/Program.cs b/samples/SampleApp/Program.cs new file mode 100644 index 0000000000..e78eab19d1 --- /dev/null +++ b/samples/SampleApp/Program.cs @@ -0,0 +1,63 @@ +using System; +using Microsoft.AspNetCore; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Routing; + +namespace SampleApp +{ + public class Program + { + public static void Main(string[] args) + { + //HelloWorld(); + + //CustomUrl(); + + Router(); + + //StartupClass(args); + } + + private static void HelloWorld() + { + var host = WebHost.Start(context => context.Response.WriteAsync("Hello, World!")); + //host.WaitForShutdown(); // TODO: This method needs to be added to Hosting + Console.WriteLine("Press any key to shutdown..."); + Console.ReadKey(); + } + + private static void CustomUrl() + { + // Changing the listening URL + var host = WebHost.Start("http://localhost:8080", context => context.Response.WriteAsync("Hello, World!")); + //host.WaitForShutdown(); // TODO: This method needs to be added to Hosting + Console.WriteLine("Press any key to shutdown..."); + Console.ReadKey(); + } + + private static void Router() + { + // Using a router + var host = WebHost.Start(router => router + .MapGet("hello/{name}", (req, res, data) => res.WriteAsync($"Hello, {data.Values["name"]}")) + .MapGet("goodbye/{name}", (req, res, data) => res.WriteAsync($"Goodbye, {data.Values["name"]}")) + .MapGet("throw/{message?}", (req, res, data) => throw new Exception((string)data.Values["message"] ?? "Uh oh!")) + .MapGet("{greeting}/{name}", (req, res, data) => res.WriteAsync($"{data.Values["greeting"]}, {data.Values["name"]}")) + .MapGet("", (req, res, data) => res.WriteAsync($"Hello, World!")) + ); + //host.WaitForShutdown(); // TODO: This method needs to be added to Hosting + Console.WriteLine("Press any key to shutdown..."); + Console.ReadKey(); + } + + private static void StartupClass(string[] args) + { + // Using defaults with a Startup class + var host = WebHost.CreateDefaultBuilder(args) + .UseStartup() + .Build(); + host.Run(); + } + } +} diff --git a/samples/SampleApp/Properties/launchSettings.json b/samples/SampleApp/Properties/launchSettings.json new file mode 100644 index 0000000000..f0370cf22b --- /dev/null +++ b/samples/SampleApp/Properties/launchSettings.json @@ -0,0 +1,27 @@ +{ + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:53432/", + "sslPort": 0 + } + }, + "profiles": { + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "SampleApp": { + "commandName": "Project", + "launchBrowser": true, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + }, + "applicationUrl": "http://localhost:53433" + } + } +} diff --git a/samples/SampleApp/SampleApp.csproj b/samples/SampleApp/SampleApp.csproj new file mode 100644 index 0000000000..a3520ca464 --- /dev/null +++ b/samples/SampleApp/SampleApp.csproj @@ -0,0 +1,18 @@ + + + + + + netcoreapp2.0 + aspnetcore-MetaPackagesSampleApp-20170406180413 + + + + + + + + + + + diff --git a/samples/SampleApp/Startup.cs b/samples/SampleApp/Startup.cs new file mode 100644 index 0000000000..0f8e756160 --- /dev/null +++ b/samples/SampleApp/Startup.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; + +namespace SampleApp +{ + public class Startup + { + public void ConfigureServices(IServiceCollection services) + { + + } + + public void Configure(IApplicationBuilder app, IHostingEnvironment env) + { + app.Run(async (context) => + { + await context.Response.WriteAsync($"Hello from {nameof(Startup)}!"); + }); + } + } +} diff --git a/src/Microsoft.AspNetCore/Microsoft.AspNetCore.csproj b/src/Microsoft.AspNetCore/Microsoft.AspNetCore.csproj index 1299f3569e..1ce6696c7a 100644 --- a/src/Microsoft.AspNetCore/Microsoft.AspNetCore.csproj +++ b/src/Microsoft.AspNetCore/Microsoft.AspNetCore.csproj @@ -3,10 +3,14 @@ - false - netstandard1.3 + true + netstandard1.5 aspnetcore Microsoft.AspNetCore + false + ..\..\build\Key.snk + true + true diff --git a/src/Microsoft.AspNetCore/WebHost.cs b/src/Microsoft.AspNetCore/WebHost.cs new file mode 100644 index 0000000000..4aa1a87df8 --- /dev/null +++ b/src/Microsoft.AspNetCore/WebHost.cs @@ -0,0 +1,179 @@ +// 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.IO; +using System.Reflection; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Routing; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; + +namespace Microsoft.AspNetCore +{ + /// + /// Provides convenience methods for creating instances of and with pre-configured defaults. + /// + public static class WebHost + { + /// + /// Initializes and starts a new with pre-configured defaults. + /// See for details. + /// + /// A delegate that handles requests to the application. + /// A started that hosts the application. + public static IWebHost Start(RequestDelegate app) => + Start(null, app); + + /// + /// Initializes and starts a new with pre-configured defaults. + /// See for details. + /// + /// The URL the hosted application will listen on. + /// A delegate that handles requests to the application. + /// A started that hosts the application. + public static IWebHost Start(string url, RequestDelegate app) => + StartWith(url, appBuilder => appBuilder.Run(app)); + + /// + /// Initializes and starts a new with pre-configured defaults. + /// See for details. + /// + /// A delegate that configures the router for handling requests to the application. + /// A started that hosts the application. + public static IWebHost Start(Action routeBuilder) => + Start(null, routeBuilder); + + /// + /// Initializes and starts a new with pre-configured defaults. + /// See for details. + /// + /// The URL the hosted application will listen on. + /// A delegate that configures the router for handling requests to the application. + /// A started that hosts the application. + public static IWebHost Start(string url, Action routeBuilder) => + StartWith(url, services => services.AddRouting(), app => app.UseRouter(routeBuilder)); + + /// + /// Initializes and starts a new with pre-configured defaults. + /// See for details. + /// + /// A delegate that handles requests to the application. + /// A started that hosts the application. + public static IWebHost StartWith(Action app) => + StartWith(null, app); + + /// + /// Initializes and starts a new with pre-configured defaults. + /// See for details. + /// + /// The URL the hosted application will listen on. + /// The delegate that configures the . + /// A started that hosts the application. + public static IWebHost StartWith(string url, Action app) => + StartWith(url, null, app); + + private static IWebHost StartWith(string url, Action configureServices, Action app) + { + var builder = CreateDefaultBuilder(); + + if (!string.IsNullOrEmpty(url)) + { + builder.UseUrls(url); + } + + if (configureServices != null) + { + builder.ConfigureServices(configureServices); + } + + var host = builder + .Configure(app) + .Build(); + + host.Start(); + + return host; + } + + /// + /// Initializes a new instance of the class with pre-configured defaults. + /// + /// + /// The following defaults are applied to the returned : + /// use Kestrel as the web server, + /// set the to the result of , + /// load from 'appsettings.json' and 'appsettings.[].json', + /// load from User Secrets when is 'Development' using the entry assembly, + /// load from environment variables, + /// configures the to log to the console, + /// enables IIS integration, + /// and adds the developer exception page when is 'Development' + /// + /// The initialized . + public static IWebHostBuilder CreateDefaultBuilder() => + CreateDefaultBuilder(null); + + /// + /// Initializes a new instance of the class with pre-configured defaults. + /// + /// + /// The following defaults are applied to the returned : + /// use Kestrel as the web server, + /// set the to the result of , + /// load from 'appsettings.json' and 'appsettings.[].json', + /// load from User Secrets when is 'Development' using the entry assembly, + /// load from environment variables, + /// load from supplied command line args, + /// configures the to log to the console, + /// enables IIS integration, + /// and adds the developer exception page when is 'Development' + /// + /// The command line args. + /// The initialized . + public static IWebHostBuilder CreateDefaultBuilder(string[] args) + { + var builder = new WebHostBuilder() + .UseKestrel() + .UseContentRoot(Directory.GetCurrentDirectory()) + .ConfigureConfiguration((hostingContext, config) => + { + var env = hostingContext.HostingEnvironment; + + config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) + .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true); + + if (env.IsDevelopment()) + { + var appAssembly = Assembly.GetEntryAssembly(); + if (appAssembly != null) + { + config.AddUserSecrets(appAssembly); + } + } + + config.AddEnvironmentVariables(); + + if (args != null) + { + config.AddCommandLine(args); + } + }) + .ConfigureLogging(logging => + { + logging.AddConsole(); + }) + // TODO: Remove this when ANCM injects it by default + .UseIISIntegration() + .ConfigureServices(services => + { + services.AddSingleton(); + }); + + return builder; + } + } +} diff --git a/src/Microsoft.AspNetCore/WebHostStartupFilter.cs b/src/Microsoft.AspNetCore/WebHostStartupFilter.cs new file mode 100644 index 0000000000..43690799ce --- /dev/null +++ b/src/Microsoft.AspNetCore/WebHostStartupFilter.cs @@ -0,0 +1,26 @@ +// 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 Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.DependencyInjection; + +namespace Microsoft.AspNetCore +{ + internal class WebHostStartupFilter : IStartupFilter + { + public Action Configure(Action next) + { + return app => + { + var env = app.ApplicationServices.GetService(); + if (env.IsDevelopment()) + { + app.UseDeveloperExceptionPage(); + } + next(app); + }; + } + } +}