internal static (ServiceRegistry, AssemblyScanner[]) ExplodeSynchronously(IServiceCollection services) { var scanners = new AssemblyScanner[0]; var registry = new ServiceRegistry(services); while (registry.HasScanners()) { var(registry2, operations) = ParseToOperations(registry); var additional = operations.OfType <AssemblyScanner>().ToArray(); registry = registry2; scanners = scanners.Concat(additional).ToArray(); registry.RemoveAll(x => x.ServiceType == typeof(AssemblyScanner)); Task.WhenAll(additional.Select(x => x.TypeFinder)).Wait(TimeSpan.FromSeconds(2)); foreach (var operation in operations) { if (operation is AssemblyScanner scanner) { scanner.ApplyRegistrations(registry); } if (operation is ServiceDescriptor[] descriptors) { registry.AddRange(descriptors); } } } return(registry, scanners); }
internal static async Task <(ServiceRegistry, AssemblyScanner[])> Explode(IServiceCollection services) { var scanners = new AssemblyScanner[0]; var registry = new ServiceRegistry(services); var registriesEncountered = new List <Type>(); while (registry.HasScanners()) { var(registry2, operations) = ParseToOperations(registry, registriesEncountered); var additional = operations.OfType <AssemblyScanner>().ToArray(); registry = registry2; scanners = scanners.Concat(additional).ToArray(); registry.RemoveAll(x => x.ServiceType == typeof(AssemblyScanner)); foreach (var operation in operations) { if (operation is AssemblyScanner scanner) { await scanner.TypeFinder; scanner.ApplyRegistrations(registry); } if (operation is ServiceDescriptor[] descriptors) { registry.AddRange(descriptors); } } } return(registry, scanners); }
internal ServiceRegistry CombineServices(IServiceCollection fromWebHostBuilder) { // Do Settings here var environment = fromWebHostBuilder .Where(x => x.ServiceType == typeof(IHostingEnvironment)) .Select(x => x.ImplementationInstance).OfType <IHostingEnvironment>() .FirstOrDefault(); var configuration = fromWebHostBuilder .Where(x => x.ServiceType == typeof(IConfiguration)) .Select(x => x.ImplementationInstance).OfType <IConfiguration>() .FirstOrDefault(); Settings.Bootstrap(new WebHostBuilderContext { Configuration = configuration, HostingEnvironment = environment }); var all = fromWebHostBuilder.Concat(_baseServices).Concat(ExtensionServices).Concat(_applicationServices); var combined = new ServiceRegistry(); combined.AddRange(all); combined.Insert(0, ServiceDescriptor.Singleton <IHostedService, MessagingActivator>()); combined.For <IDurableMessagingFactory>().UseIfNone <NulloDurableMessagingFactory>(); combined.For <IServer>().UseIfNone <NulloServer>(); return(combined); }
public void ScanTypes(TypeSet types, ServiceRegistry registry) { types.FindTypes(TypeClassification.Closed | TypeClassification.Concretes) .Where(IsPublicRegistry) .Each(type => { var found = Activator.CreateInstance(type).As <ServiceRegistry>(); registry.AddRange(found); }); }
// Integration Testing scenario public static IContainer CreateContainer( ContainerConfiguration containerConfig, IReadOnlyCollection <ServiceDescriptor> overrideDescriptors) { var registry = new ServiceRegistry(); PopulateRegistry(containerConfig, registry); registry.AddRange(overrideDescriptors); var container = new Container(registry); LogAndInitializeSingleton(container); return(container); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureContainer(ServiceRegistry services) { services.AddRange(new ApplicationRegistry()); services.Configure <CookiePolicyOptions>(options => { // This lambda determines whether user consent for non-essential cookies is needed for a given request. options.CheckConsentNeeded = context => true; options.MinimumSameSitePolicy = SameSiteMode.None; }); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); }
public static async Task <IServiceCollection> Combine(this IServiceCollection[] serviceCollections) { if (!serviceCollections.Any()) { return(new ServiceRegistry()); } foreach (var services in serviceCollections) { await services.ApplyScannedTypes(); } if (serviceCollections.Length == 1) { return(serviceCollections[0]); } var response = new ServiceRegistry(); response.AddRange(serviceCollections.SelectMany(x => x)); return(response); }
public void Apply(ServiceRegistry services) { services.AddRange(Overrides); }
internal ServiceRegistry CompileConfigurationAndServicesForIdiomaticBootstrapping() { _baseServices.Insert(0, ServiceDescriptor.Singleton <IHostedService, MessagingActivator>()); // Add services var options = new WebHostOptions(EnvironmentConfiguration); var contentRootPath = ResolveContentRootPath(options.ContentRootPath, AppContext.BaseDirectory); // Initialize the hosting environment // TODO -- Watch this, if there are bugs in bootstrapping!!!!!! _hostingEnvironment.Initialize(contentRootPath, options); _hostingEnvironment.ApplicationName = ApplicationAssembly.FullName; var context = new WebHostBuilderContext { Configuration = EnvironmentConfiguration, HostingEnvironment = _hostingEnvironment }; Services.AddSingleton(_hostingEnvironment); Services.AddSingleton(context); var builder = Configuration .SetBasePath(_hostingEnvironment.ContentRootPath) .AddInMemoryCollection(EnvironmentConfiguration.AsEnumerable()); foreach (var configureAppConfiguration in _configureAppConfigurationBuilderDelegates) { configureAppConfiguration(context, builder); } var configuration = builder.Build(); Services.AddSingleton <IConfiguration>(configuration); Services.AddSingleton <IConfigurationRoot>(configuration); context.Configuration = configuration; Settings.Bootstrap(new WebHostBuilderContext { HostingEnvironment = _hostingEnvironment, Configuration = configuration }); var listener = new DiagnosticListener("Microsoft.AspNetCore"); Services.AddSingleton(listener); Services.AddSingleton <DiagnosticSource>(listener); Services.AddTransient <IApplicationBuilderFactory, ApplicationBuilderFactory>(); Services.AddTransient <IHttpContextFactory, HttpContextFactory>(); Services.AddScoped <IMiddlewareFactory, MiddlewareFactory>(); Services.AddOptions(); Services.AddLogging(); Services.AddSingleton <Microsoft.AspNetCore.Hosting.IApplicationLifetime, ApplicationLifetime>(); // Conjure up a RequestServices // TODO -- want this to be optional Services.AddTransient <IStartupFilter, AutoRequestServicesStartupFilter>(); foreach (var configureServices in _configureServicesDelegates) { configureServices(context, Services); } var all = _baseServices.Concat(ExtensionServices).Concat(_applicationServices); var combined = new ServiceRegistry(); combined.AddRange(all); combined.For <IDurableMessagingFactory>().UseIfNone <NulloDurableMessagingFactory>(); combined.For <IServer>().UseIfNone <NulloServer>(); return(combined); }