private async Task CompilesInLimitedStackSpace(ServiceProviderMode mode, Type serviceType) { // Arrange var stackSize = 256 * 1024; var serviceCollection = new ServiceCollection(); CompilationTestDataProvider.Register(serviceCollection); var serviceProvider = serviceCollection.BuildServiceProvider(mode); // Act + Assert var tsc = new TaskCompletionSource <object>(); var thread = new Thread(() => { try { object service = null; for (int i = 0; i < 10; i++) { service = serviceProvider.GetService(serviceType); } tsc.SetResult(service); } catch (Exception ex) { tsc.SetException(ex); } }, stackSize); thread.Start(); thread.Join(); await tsc.Task; }
public ServiceProviderFactory(IUnityContainer container, ServiceProviderMode mode = ServiceProviderMode.ChildContainerPerProvider) { _container = container ?? new UnityContainer(); _mode = mode; ((UnityContainer)_container).AddExtension(new MdiExtension()); _container.RegisterInstance <IServiceProviderFactory <IUnityContainer> >(this, new ContainerControlledLifetimeManager()); _container.RegisterInstance <IServiceProviderFactory <IServiceCollection> >(this, new ExternallyControlledLifetimeManager()); }
private void NoServiceCallsite_DefaultValueNull_DoesNotThrow(ServiceProviderMode mode) { var descriptors = new ServiceCollection(); descriptors.AddTransient <ServiceG>(); var provider = descriptors.BuildServiceProvider(mode); ServiceF instance = ActivatorUtilities.CreateInstance <ServiceF>(provider); Assert.NotNull(instance); }
private void ScopedServiceResolvedFromSingletonAfterCompilation2(ServiceProviderMode mode) { ServiceProvider sp = new ServiceCollection() .AddScoped <A>() .AddSingleton <IFakeOpenGenericService <A>, FakeOpenGenericService <A> >() .BuildServiceProvider(mode); var scope = sp.CreateScope(); for (int i = 0; i < 50; i++) { scope.ServiceProvider.GetRequiredService <A>(); Thread.Sleep(10); // Give the background thread time to compile } Assert.Same(sp.GetRequiredService <IFakeOpenGenericService <A> >().Value, sp.GetRequiredService <A>()); }
public static ServiceProvider BuildServiceProvider(this IServiceCollection services, ServiceProviderMode mode) { if (mode == ServiceProviderMode.Default) { return(services.BuildServiceProvider()); } IServiceProviderEngine engine = mode switch { ServiceProviderMode.Dynamic => new DynamicServiceProviderEngine(services), ServiceProviderMode.Runtime => new RuntimeServiceProviderEngine(services), ServiceProviderMode.Expressions => new ExpressionsServiceProviderEngine(services), ServiceProviderMode.ILEmit => new ILEmitServiceProviderEngine(services), _ => throw new NotSupportedException() }; return(new ServiceProvider(services, engine, ServiceProviderOptions.Default)); } }
public static ServiceProvider BuildServiceProvider(this IServiceCollection services, ServiceProviderMode mode, ServiceProviderOptions options = null) { options ??= ServiceProviderOptions.Default; if (mode == ServiceProviderMode.Default) { return(services.BuildServiceProvider(options)); } var provider = new ServiceProvider(services, ServiceProviderOptions.Default); ServiceProviderEngine engine = mode switch { ServiceProviderMode.Dynamic => new DynamicServiceProviderEngine(provider), ServiceProviderMode.Runtime => RuntimeServiceProviderEngine.Instance, ServiceProviderMode.Expressions => new ExpressionsServiceProviderEngine(provider), ServiceProviderMode.ILEmit => new ILEmitServiceProviderEngine(provider), _ => throw new NotSupportedException() }; provider._engine = engine; return(provider); } }
public static IWebHostBuilder UseUnityServiceProvider(this IWebHostBuilder hostBuilder, IUnityContainer container = null, ServiceProviderMode mode = ServiceProviderMode.ChildContainerPerProvider) { _factory = new ServiceProviderFactory(container, mode); #if NETCOREAPP1_1 return(hostBuilder.ConfigureServices((services) => { services.Replace(ServiceDescriptor.Singleton <IServiceProviderFactory <IUnityContainer> >(_factory)); services.Replace(ServiceDescriptor.Singleton <IServiceProviderFactory <IServiceCollection> >(_factory)); })); #else return(hostBuilder.ConfigureServices((context, services) => { services.Replace(ServiceDescriptor.Singleton <IServiceProviderFactory <IUnityContainer> >(_factory)); services.Replace(ServiceDescriptor.Singleton <IServiceProviderFactory <IServiceCollection> >(_factory)); })); #endif }