private static DbContext TryCreateContextFromStartup(Type type) { #if ASPNET50 || ASPNETCORE50 try { // TODO: Let Hosting do this the right way (See aspnet/Hosting#85) var hostingServices = new ServiceCollection() .Add(HostingServices.GetDefaultServices()) .AddInstance <IHostingEnvironment>(new HostingEnvironment { EnvironmentName = "Development" }) .BuildServiceProvider(CallContextServiceLocator.Locator.ServiceProvider); var assembly = type.GetTypeInfo().Assembly; var startupType = assembly.DefinedTypes.FirstOrDefault(t => t.Name.Equals("Startup", StringComparison.Ordinal)); var instance = ActivatorUtilities.GetServiceOrCreateInstance(hostingServices, startupType.AsType()); var servicesMethod = startupType.GetDeclaredMethod("ConfigureServices"); var services = new ServiceCollection() .Add(OptionsServices.GetDefaultServices()); servicesMethod.Invoke(instance, new[] { services }); var applicationServices = services.BuildServiceProvider(hostingServices); return(applicationServices.GetService(type) as DbContext); } catch { } #endif return(null); }
public void RequestServicesAvailableOnlyAfterRequestServices() { var baseServiceProvider = new ServiceCollection() .Add(HostingServices.GetDefaultServices()) .BuildServiceProvider(); var builder = new ApplicationBuilder(baseServiceProvider); bool foundRequestServicesBefore = false; builder.Use(next => async c => { foundRequestServicesBefore = c.RequestServices != null; await next.Invoke(c); }); builder.UseRequestServices(); bool foundRequestServicesAfter = false; builder.Use(next => async c => { foundRequestServicesAfter = c.RequestServices != null; await next.Invoke(c); }); var context = new DefaultHttpContext(); builder.Build().Invoke(context); Assert.False(foundRequestServicesBefore); Assert.True(foundRequestServicesAfter); }
public void DuplicateHubNamesThrows() { // Arrange var mockHub = new Mock <IHub>(); var mockHubManager = new Mock <IHubManager>(); mockHubManager.Setup(m => m.GetHub("foo")).Returns(new HubDescriptor { Name = "foo", HubType = mockHub.Object.GetType() }); var serviceProvider = new ServiceCollection() .Add(OptionsServices.GetDefaultServices()) .Add(HostingServices.GetDefaultServices()) .Add(SignalRServices.GetDefaultServices().Where(descriptor => descriptor.ServiceType != typeof(IHubManager))) .AddInstance <IHubManager>(mockHubManager.Object) .BuildServiceProvider(); var dispatcher = new HubDispatcher(serviceProvider.GetRequiredService <IOptions <SignalROptions> >()); var testContext = new TestContext("/ignorePath", new Dictionary <string, string> { { "connectionData", @"[{name: ""foo""}, {name: ""Foo""}]" }, }); // Act & Assert dispatcher.Initialize(serviceProvider); Assert.Throws <InvalidOperationException>(() => dispatcher.Authorize(testContext.MockRequest.Object)); }
public Task <int> Main(string[] args) { //Add command line configuration source to read command line parameters. var config = new Configuration(); config.AddCommandLine(args); var serviceCollection = new ServiceCollection(); serviceCollection.Add(HostingServices.GetDefaultServices(config)); var services = serviceCollection.BuildServiceProvider(_hostServiceProvider); var context = new HostingContext() { Services = services, Configuration = config, ServerName = "Microsoft.AspNet.Server.WebListener", ApplicationName = "BugTracker" }; var engine = services.GetService <IHostingEngine>(); if (engine == null) { throw new Exception("TODO: IHostingEngine service not available exception"); } using (engine.Start(context)) { Console.WriteLine("Started the server.."); Console.WriteLine("Press any key to stop the server"); Console.ReadLine(); } return(Task.FromResult(0)); }
public static IServiceProvider CreateServiceProvider(Action <IServiceCollection> configure) { var collection = new ServiceCollection() .Add(OptionsServices.GetDefaultServices()) .Add(HostingServices.GetDefaultServices()) .Add(SignalRServices.GetDefaultServices()); configure(collection); return(collection.BuildServiceProvider(CallContextServiceLocator.Locator.ServiceProvider)); }
public static TestServer Create(IServiceProvider provider, Action <IBuilder> app) { var collection = new ServiceCollection(); var hostingServices = HostingServices.GetDefaultServices(); var config = new Configuration(); collection.Add(hostingServices); var serviceProvider = collection.BuildServiceProvider(provider); return(new TestServer(config, serviceProvider, app)); }
public void GetHubContextRejectsInvalidTypes() { //var resolver = new DefaultDependencyResolver(); var serviceProvider = new ServiceCollection() .Add(OptionsServices.GetDefaultServices()) .Add(HostingServices.GetDefaultServices()) .Add(SignalRServices.GetDefaultServices()) .BuildServiceProvider(CallContextServiceLocator.Locator.ServiceProvider); var manager = serviceProvider.GetService <IConnectionManager>(); Assert.Throws <InvalidOperationException>(() => manager.GetHubContext <DemoHub, IDontReturnVoidOrTask>()); Assert.Throws <InvalidOperationException>(() => manager.GetHubContext <DemoHub, IHaveOutParameter>()); Assert.Throws <InvalidOperationException>(() => manager.GetHubContext <DemoHub, IHaveRefParameter>()); Assert.Throws <InvalidOperationException>(() => manager.GetHubContext <DemoHub, IHaveProperties>()); Assert.Throws <InvalidOperationException>(() => manager.GetHubContext <DemoHub, IHaveIndexer>()); Assert.Throws <InvalidOperationException>(() => manager.GetHubContext <DemoHub, IHaveEvent>()); Assert.Throws <InvalidOperationException>(() => manager.GetHubContext <DemoHub, NotAnInterface>()); }
public static TestServer Create(IServiceProvider provider, Action <IApplicationBuilder> app) { var appEnv = provider.GetRequiredService <IApplicationEnvironment>(); var hostingEnv = new HostingEnvironment() { EnvironmentName = DefaultEnvironmentName, WebRoot = HostingUtilities.GetWebRoot(appEnv.ApplicationBasePath), }; var collection = new ServiceCollection(); collection.Add(HostingServices.GetDefaultServices()); collection.AddInstance <IHostingEnvironment>(hostingEnv); var appServices = collection.BuildServiceProvider(provider); var config = new Configuration(); return(new TestServer(config, appServices, app)); }
public void EnsureRequestServicesSetsRequestServices(bool initializeApplicationServices) { var baseServiceProvider = new ServiceCollection() .Add(HostingServices.GetDefaultServices()) .BuildServiceProvider(); var builder = new ApplicationBuilder(baseServiceProvider); bool foundRequestServicesBefore = false; builder.Use(next => async c => { foundRequestServicesBefore = c.RequestServices != null; await next.Invoke(c); }); builder.Use(next => async c => { using (var container = RequestServicesContainer.EnsureRequestServices(c, baseServiceProvider)) { await next.Invoke(c); } }); bool foundRequestServicesAfter = false; builder.Use(next => async c => { foundRequestServicesAfter = c.RequestServices != null; await next.Invoke(c); }); var context = new DefaultHttpContext(); if (initializeApplicationServices) { context.ApplicationServices = baseServiceProvider; } builder.Build().Invoke(context); Assert.False(foundRequestServicesBefore); Assert.True(foundRequestServicesAfter); }
public void Main(string[] args) { var applicationRoot = Directory.GetCurrentDirectory(); var serverPort = 2000; var traceType = TraceType.Information; var enumerator = args.GetEnumerator(); while (enumerator.MoveNext()) { var arg = (string)enumerator.Current; if (arg == "-s") { enumerator.MoveNext(); applicationRoot = Path.GetFullPath((string)enumerator.Current); } else if (arg == "-p") { enumerator.MoveNext(); serverPort = int.Parse((string)enumerator.Current); } else if (arg == "-v") { traceType = TraceType.Verbose; } } var environment = new OmnisharpEnvironment(applicationRoot, serverPort, traceType); var hostingEnv = new HostingEnvironment { EnvironmentName = "Development" }; var config = new Configuration() .AddCommandLine(new[] { "--server.urls", "http://localhost:" + serverPort }); var serviceCollection = new ServiceCollection(); serviceCollection.Add(HostingServices.GetDefaultServices(config)); serviceCollection.AddInstance <IOmnisharpEnvironment>(environment); serviceCollection.AddInstance <IHostingEnvironment>(hostingEnv); var services = serviceCollection .BuildServiceProvider(_serviceProvider); var appEnv = services.GetRequiredService <IApplicationEnvironment>(); var context = new HostingContext() { Services = services, Configuration = config, ServerName = "Kestrel", ApplicationName = appEnv.ApplicationName, EnvironmentName = hostingEnv.EnvironmentName, }; var engine = services.GetRequiredService <IHostingEngine>(); var appShutdownService = _serviceProvider.GetRequiredService <IApplicationShutdown>(); var shutdownHandle = new ManualResetEventSlim(false); var serverShutdown = engine.Start(context); appShutdownService.ShutdownRequested.Register(() => { serverShutdown.Dispose(); shutdownHandle.Set(); }); #if ASPNETCORE50 var ignored = Task.Run(() => { Console.WriteLine("Started"); Console.ReadLine(); appShutdownService.RequestShutdown(); }); #else Console.CancelKeyPress += (sender, e) => { appShutdownService.RequestShutdown(); }; #endif shutdownHandle.Wait(); }