private RequestDelegate buildRequestDelegate(IServer server, ServiceRegistry originalServices) { var factory = new ApplicationBuilderFactory(Container); var builder = factory.CreateBuilder(server.Features); builder.ApplicationServices = Container; var startupFilters = Container.QuickBuildAll <IStartupFilter>(); // Hate, hate, hate this code, but I blame the ASP.Net team // some how Action <IApplicationBuilder> configure = app => configureAppBuilder(app, originalServices); foreach (var filter in startupFilters.Reverse()) { configure = filter.Configure(configure); } configure(builder); return(builder.Build()); }
public void ConfigureTest() { var mockHostingEnvironment = new Mock <IHostingEnvironment>(); mockHostingEnvironment.SetupGet(h => h.EnvironmentName).Returns("Development"); var sartup = new Startup(mockHostingEnvironment.Object, new Mock <ILoggerFactory>().Object); var serviceCollection = new ServiceCollection(); sartup.ConfigureServices(serviceCollection); var factory = new ApplicationBuilderFactory(serviceCollection.BuildServiceProvider()); sartup.Configure(factory.CreateBuilder(new Mock <IFeatureCollection>().Object)); }
public void ConfigureTest() { var mockHostingEnvironment = new Mock <IHostingEnvironment>(); mockHostingEnvironment.SetupGet(h => h.EnvironmentName).Returns("Development"); mockHostingEnvironment.SetupGet(h => h.ContentRootPath).Returns(Directory.GetCurrentDirectory()); mockHostingEnvironment.SetupGet(h => h.WebRootPath).Returns(Path.Combine(Directory.GetCurrentDirectory(), "wwwroo")); var startup = new Startup(mockHostingEnvironment.Object, new Mock <ILoggerFactory>().Object); var serviceCollection = new ServiceCollection(); startup.ConfigureServices(serviceCollection); var factory = new ApplicationBuilderFactory(serviceCollection.BuildServiceProvider()); startup.Configure(factory.CreateBuilder(new Mock <IFeatureCollection>().Object)); }
public async Task StartAsync(CancellationToken cancellationToken) { var transportOptions = new SocketTransportOptions { }; _server = new KestrelServer( Options.Create(_options), new SocketTransportFactory(Options.Create(transportOptions), _loggerFactory), _loggerFactory); var app = new ApplicationBuilderFactory(_provider).CreateBuilder(_server.Features); _pipeline.Configure(app); var requestDelegate = app.Build(); await _server.StartAsync(new GrpcApplication(requestDelegate, _provider), cancellationToken); }
public async Task TestUseConfig() { var listener = new DiagnosticListener("Microsoft.AspNetCore"); Service.TryAddSingleton <DiagnosticListener>(listener); Service.AddOptions(); Service.AddRouting(); await Service.AddBrochureServer(); var provider = Service.BuildPluginServiceProvider(); var manager = provider.GetService <IPluginManagers>(); var middleManager = provider.GetService <IMiddleManager>(); var builderFactory = new ApplicationBuilderFactory(provider); var applicationBuilder = builderFactory.CreateBuilder(new FeatureCollection()); var pluginbuilderFactory = new PluginApplicationBuilderFactory(provider as IPluginServiceProvider, manager); var builder = pluginbuilderFactory.CreateBuilder(new FeatureCollection()); builder.ConfigPlugin(); }
public async Task TestUseRouting() { var listener = new DiagnosticListener("Microsoft.AspNetCore"); Service.TryAddSingleton <DiagnosticListener>(listener); Service.AddOptions(); Service.AddRouting(); await Service.AddBrochureServer(); var provider = Service.BuildPluginServiceProvider(); var manager = provider.GetService <IPluginManagers>(); var middleManager = provider.GetService <IMiddleManager>(); var builderFactory = new ApplicationBuilderFactory(provider); var applicationBuilder = builderFactory.CreateBuilder(new FeatureCollection()); var pluginbuilderFactory = new PluginApplicationBuilderFactory(provider as IPluginServiceProvider, manager); var builder = pluginbuilderFactory.CreateBuilder(new FeatureCollection()); builder.IntertMiddle("main-routing", Guid.Empty, 10, () => builder.UseRouting()); var count = middleManager.GetMiddlesList().Count; Assert.AreEqual(1, count); }
public async Task StartAsync(CancellationToken cancellationToken) { HostingEventSource.Log.HostStart(); var serverAddressesFeature = Server.Features?.Get <IServerAddressesFeature>(); var addresses = serverAddressesFeature?.Addresses; if (addresses != null && !addresses.IsReadOnly && addresses.Count == 0) { var urls = Configuration[WebHostDefaults.ServerUrlsKey]; if (!string.IsNullOrEmpty(urls)) { serverAddressesFeature.PreferHostingUrls = WebHostUtilities.ParseBool(Configuration, WebHostDefaults.PreferHostingUrlsKey); foreach (var value in urls.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries)) { addresses.Add(value); } } } RequestDelegate application = null; try { Action <IApplicationBuilder> configure = Options.ConfigureApplication; if (configure == null) { throw new InvalidOperationException($"No application configured. Please specify an application via IWebHostBuilder.UseStartup, IWebHostBuilder.Configure, or specifying the startup assembly via {nameof(WebHostDefaults.StartupAssemblyKey)} in the web host configuration."); } var builder = ApplicationBuilderFactory.CreateBuilder(Server.Features); foreach (var filter in StartupFilters.Reverse()) { configure = filter.Configure(configure); } configure(builder); // Build the request pipeline application = builder.Build(); } catch (Exception ex) { Logger.ApplicationError(ex); if (!Options.WebHostOptions.CaptureStartupErrors) { throw; } application = BuildErrorPageApplication(ex); } var httpApplication = new HostingApplication(application, Logger, DiagnosticListener, HttpContextFactory); await Server.StartAsync(httpApplication, cancellationToken); if (addresses != null) { foreach (var address in addresses) { LifetimeLogger.LogInformation("Now listening on: {address}", address); } } if (Logger.IsEnabled(LogLevel.Debug)) { foreach (var assembly in Options.WebHostOptions.GetFinalHostingStartupAssemblies()) { Logger.LogDebug("Loaded hosting startup assembly {assemblyName}", assembly); } } if (Options.HostingStartupExceptions != null) { foreach (var exception in Options.HostingStartupExceptions.InnerExceptions) { Logger.HostingStartupAssemblyError(exception); } } }
public async Task StartAsync(CancellationToken cancellationToken) { HostingEventSource.Log.HostStart(); var serverAddressesFeature = Server.Features.Get <IServerAddressesFeature>(); var addresses = serverAddressesFeature?.Addresses; if (addresses != null && !addresses.IsReadOnly && addresses.Count == 0) { // We support reading "urls" from app configuration var urls = Configuration[WebHostDefaults.ServerUrlsKey]; // But fall back to host settings if (string.IsNullOrEmpty(urls)) { urls = Options.WebHostOptions.ServerUrls; } if (!string.IsNullOrEmpty(urls)) { // We support reading "preferHostingUrls" from app configuration var preferHostingUrlsConfig = Configuration[WebHostDefaults.PreferHostingUrlsKey]; // But fall back to host settings if (!string.IsNullOrEmpty(preferHostingUrlsConfig)) { serverAddressesFeature !.PreferHostingUrls = WebHostUtilities.ParseBool(preferHostingUrlsConfig); } else { serverAddressesFeature !.PreferHostingUrls = Options.WebHostOptions.PreferHostingUrls; } foreach (var value in urls.Split(';', StringSplitOptions.RemoveEmptyEntries)) { addresses.Add(value); } } } RequestDelegate?application = null; try { var configure = Options.ConfigureApplication; if (configure == null) { throw new InvalidOperationException($"No application configured. Please specify an application via IWebHostBuilder.UseStartup, IWebHostBuilder.Configure, or specifying the startup assembly via {nameof(WebHostDefaults.StartupAssemblyKey)} in the web host configuration."); } var builder = ApplicationBuilderFactory.CreateBuilder(Server.Features); foreach (var filter in StartupFilters.Reverse()) { configure = filter.Configure(configure); } configure(builder); // Build the request pipeline application = builder.Build(); } catch (Exception ex) { Logger.ApplicationError(ex); if (!Options.WebHostOptions.CaptureStartupErrors) { throw; } var showDetailedErrors = HostingEnvironment.IsDevelopment() || Options.WebHostOptions.DetailedErrors; application = ErrorPageBuilder.BuildErrorPageApplication(HostingEnvironment.ContentRootFileProvider, Logger, showDetailedErrors, ex); } var httpApplication = new HostingApplication(application, Logger, DiagnosticListener, ActivitySource, Propagator, HttpContextFactory); await Server.StartAsync(httpApplication, cancellationToken); HostingEventSource.Log.ServerReady(); if (addresses != null) { foreach (var address in addresses) { Log.ListeningOnAddress(LifetimeLogger, address); } } if (Logger.IsEnabled(LogLevel.Debug)) { foreach (var assembly in Options.WebHostOptions.GetFinalHostingStartupAssemblies()) { Log.StartupAssemblyLoaded(Logger, assembly); } } if (Options.HostingStartupExceptions != null) { foreach (var exception in Options.HostingStartupExceptions.InnerExceptions) { Logger.HostingStartupAssemblyError(exception); } } }
/// <summary> /// Adds a HTTP HMAC Authentication scheme to the <see cref="IApplicationBuilder"/> request execution pipeline. /// </summary> /// <param name="builder">The type that provides the mechanisms to configure an application’s request pipeline.</param> /// <param name="setup">The HTTP <see cref="HmacAuthenticationOptions"/> middleware which need to be configured.</param> /// <returns>A reference to this instance after the operation has completed.</returns> public static IApplicationBuilder UseHmacAuthentication(this IApplicationBuilder builder, Action <HmacAuthenticationOptions> setup = null) { return(ApplicationBuilderFactory.UseMiddlewareConfigurable <HmacAuthenticationMiddleware, HmacAuthenticationOptions>(builder, setup)); }