public void CreateScope() { var serCol = new ServiceCollection(); serCol.AddScoped <IA, A>(); var factory = new ServiceProviderFactory(); var defCol = factory.CreateBuilder(serCol); var usp = factory.CreateServiceProvider(defCol); IA a, b, c; using (var scope1 = usp.CreateScope()) { a = scope1.ServiceProvider.GetService <IA>(); b = scope1.ServiceProvider.GetService <IA>(); Assert.False(a.Disposed); Assert.False(b.Disposed); using (var scope2 = usp.CreateScope()) { c = scope2.ServiceProvider.GetService <IA>(); Assert.False(c.Disposed); Assert.Same(a, b); Assert.NotSame(a, c); } Assert.True(c.Disposed); } Assert.True(a.Disposed); Assert.True(b.Disposed); }
public static void init(string baseUri) { BaseUri = baseUri; serviceProvider = ServiceProviderFactory.CreateServiceProvider(BaseUri, "StockQuote Service Provider", "Sample OSLC Service Provider for a Stock Quote service", new Publisher("Codeplex OSLC4Net", "urn:codeplex:oslc4net"), new Type[] { typeof(StockQuoteController) }); //Register prefix definitions this service will use PrefixDefinition[] prefixDefinitions = { new PrefixDefinition(OslcConstants.DCTERMS_NAMESPACE_PREFIX, new Uri(OslcConstants.DCTERMS_NAMESPACE)), new PrefixDefinition(OslcConstants.OSLC_CORE_NAMESPACE_PREFIX, new Uri(OslcConstants.OSLC_CORE_NAMESPACE)), new PrefixDefinition(OslcConstants.RDF_NAMESPACE_PREFIX, new Uri(OslcConstants.RDF_NAMESPACE)), new PrefixDefinition(OslcConstants.RDFS_NAMESPACE_PREFIX, new Uri(OslcConstants.RDFS_NAMESPACE)), new PrefixDefinition(Constants.STOCK_QUOTE_NAMESPACE_PREFIX, new Uri(Constants.STOCK_QUOTE_NAMESPACE)) }; serviceProvider.SetPrefixDefinitions(prefixDefinitions); About = new Uri(BaseUri + "/" + Constants.PATH_STOCK_QUOTE); serviceProvider.SetAbout(About); ServiceProviderUri = new Uri(BaseUri + "/" + SERVICE_PROVIDER_PATH); }
public void Issue_28() { var serviceCollection = new ServiceCollection(); serviceCollection.AddHttpClient(); IUnityContainer container = new UnityContainer().CreateChildContainer(); var factory = new ServiceProviderFactory(container); var sp = factory.CreateServiceProvider(serviceCollection); var scopeFactory = sp.GetRequiredService <IServiceScopeFactory>(); var httpFactory0 = sp.GetRequiredService <IHttpClientFactory>(); using (var scope = scopeFactory.CreateScope()) { var httpFactory = scope.ServiceProvider.GetRequiredService <IHttpClientFactory>(); var sp1 = httpFactory.GetType().GetField("_services", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)?.GetValue(httpFactory) as IServiceProvider; var c1 = sp1.GetType().GetField("_container", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(sp1); Assert.NotNull(c1); // "In scope" } { var httpFactory = sp.GetRequiredService <IHttpClientFactory>(); var sp1 = httpFactory.GetType().GetField("_services", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)?.GetValue(httpFactory) as IServiceProvider; var c1 = sp1.GetType().GetField("_container", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(sp1); Assert.NotNull(c1); // "after scope" } }
public IServiceProvider CreateServiceProvider(IServiceCollection containerBuilder) { var a = new ServiceProviderFactory(null); var childchild = a.CreateBuilder(containerBuilder); var sp = a.CreateServiceProvider(childchild); return(sp); }
public IUnityContainer InitializeScope(IUnityContainer container) { var child = fac.CreateBuilder(new ServiceCollection()); fac.CreateServiceProvider(child); return(child); }
private void RegisterAspCore() { var factory = new ServiceProviderFactory(this.container); var builder = factory.CreateBuilder(this.CreateServiceCollection()); this.container = (UnityContainer)factory.CreateServiceProvider(builder) .GetService(typeof(IUnityContainer)); }
public void Null() { var serCol = new ServiceCollection(); var factory = new ServiceProviderFactory(); var defCol = factory.CreateBuilder(serCol); var usp = factory.CreateServiceProvider(defCol); Assert.Null(usp.GetService <IEnum>()); }
public void GetRequiredService() { var serCol = new ServiceCollection(); var factory = new ServiceProviderFactory(); var defCol = factory.CreateBuilder(serCol); var usp = factory.CreateServiceProvider(defCol); Assert.Throws <NoSuchBeanDefinitionException>(() => usp.GetRequiredService <IA>()); }
public void EnumerableEmpty() { var serCol = new ServiceCollection(); var factory = new ServiceProviderFactory(); var defCol = factory.CreateBuilder(serCol); var usp = factory.CreateServiceProvider(defCol); Assert.Empty(usp.GetService <IEnumerable <IEnum> >()); Assert.IsType <IEnum[]>(usp.GetService <IEnumerable <IEnum> >()); }
public void ConcreteGenericType() { var serCol = new ServiceCollection(); serCol.AddSingleton <IGeneric <int>, ConcreteGeneric>(); var factory = new ServiceProviderFactory(); var defCol = factory.CreateBuilder(serCol); var usp = factory.CreateServiceProvider(defCol); Assert.IsType <ConcreteGeneric>(usp.GetService <IGeneric <int> >()); }
public void NoSatisfiedCtor() { var serCol = new ServiceCollection(); serCol.AddSingleton <FailService>(); var factory = new ServiceProviderFactory(); var defCol = factory.CreateBuilder(serCol); var ex = Assert.Throws <BeanCreationException>(() => factory.CreateServiceProvider(defCol)); Assert.Equal("Fail to satisfy any of these constructors\r\n- Void .ctor(UnityAddon.CoreTest.Bean.Mocks.ConstructorResolver.ID)", ex.Message); }
public void GetService() { var serCol = new ServiceCollection(); serCol.AddSingleton <IA, A>(); var factory = new ServiceProviderFactory(); var defCol = factory.CreateBuilder(serCol); var usp = factory.CreateServiceProvider(defCol); Assert.IsType <A>(usp.GetService <IA>()); }
public void GenericType() { var serCol = new ServiceCollection(); serCol.AddSingleton(typeof(IGeneric <>), typeof(Generic <>)); var factory = new ServiceProviderFactory(); var defCol = factory.CreateBuilder(serCol); var usp = factory.CreateServiceProvider(defCol); Assert.IsType <Generic <string> >(usp.GetService <IGeneric <string> >()); Assert.IsType <Generic <int> >(usp.GetService <IGeneric <int> >()); }
public void ResolveNull() { var serCol = new ServiceCollection(); serCol.AddSingleton <string>(sp => null); var factory = new ServiceProviderFactory(); var defCol = factory.CreateBuilder(serCol); var usp = (IUnityAddonSP)factory.CreateServiceProvider(defCol); Assert.Null(usp.GetService <string>()); Assert.True(usp.IsRegistered <string>()); }
public void Enumerable() { var serCol = new ServiceCollection(); serCol.AddSingleton <IEnum, E1>(); serCol.AddSingleton <IEnum, E2>(); var factory = new ServiceProviderFactory(); var defCol = factory.CreateBuilder(serCol); var usp = (IUnityAddonSP)factory.CreateServiceProvider(defCol); Assert.Equal(usp.GetService <IEnumerable <IEnum> >(), new IEnum[] { usp.GetService <IEnum>("E1"), usp.GetService <IEnum>("E2") }); Assert.IsType <IEnum[]>(usp.GetService <IEnumerable <IEnum> >()); }
public void CanResolve() { var serCol = new ServiceCollection(); serCol.AddSingleton <IA, A>(); var factory = new ServiceProviderFactory(); var defCol = factory.CreateBuilder(serCol); var usp = (IUnityAddonSP)factory.CreateServiceProvider(defCol); Assert.True(usp.CanResolve(typeof(IA))); Assert.True(usp.CanResolve(typeof(IEnumerable <IA>))); Assert.False(usp.CanResolve(typeof(A))); }
public void NoArgCtor() { var choice = 0; var serCol = new ServiceCollection(); serCol.AddSingleton <Service>(); var sp = serCol.BuildServiceProvider(); var factory = new ServiceProviderFactory(); var defCol = factory.CreateBuilder(serCol); var usp = factory.CreateServiceProvider(defCol); Assert.Equal(choice, sp.GetRequiredService <Service>().Choice); Assert.Equal(choice, usp.GetRequiredService <Service>().Choice); }
public void GetAllServices() { var serCol = new ServiceCollection(); serCol.AddSingleton <IA, A>(); serCol.AddSingleton <IA, A2>(); var factory = new ServiceProviderFactory(); var defCol = factory.CreateBuilder(serCol); var usp = (IUnityAddonSP)factory.CreateServiceProvider(defCol); var serAs = new IA[] { usp.GetService <IA>("A"), usp.GetService <IA>("A2") }; Assert.Equal(usp.GetServices <IA>(), serAs); Assert.IsType <IA[]>(usp.GetServices <IA>()); }
public void AmbiguousCtor() { var serCol = new ServiceCollection(); serCol.AddSingleton <Service>(); serCol.AddSingleton <IA, A>(); serCol.AddSingleton <IB, B>(); serCol.AddSingleton <IC, C>(); var factory = new ServiceProviderFactory(); var defCol = factory.CreateBuilder(serCol); var ex = Assert.Throws <BeanCreationException>(() => factory.CreateServiceProvider(defCol)); Assert.Equal("Ambiguous constructors are found\r\n- Void .ctor(UnityAddon.CoreTest.Bean.Mocks.ConstructorResolver.IA, UnityAddon.CoreTest.Bean.Mocks.ConstructorResolver.IB)\r\n- Void .ctor(UnityAddon.CoreTest.Bean.Mocks.ConstructorResolver.IA, UnityAddon.CoreTest.Bean.Mocks.ConstructorResolver.IC)", ex.Message); }
public void IsRegistered() { var serCol = new ServiceCollection(); serCol.AddSingleton <IA, A>(); var factory = new ServiceProviderFactory(); var defCol = factory.CreateBuilder(serCol); var usp = (IUnityAddonSP)factory.CreateServiceProvider(defCol); Assert.True(usp.IsRegistered(typeof(IA))); Assert.True(usp.IsRegistered(typeof(IA), "A")); Assert.False(usp.IsRegistered(typeof(A))); Assert.False(usp.IsRegistered(typeof(IA), "B")); }
public async Task Should_resolve_dependencies_from_serviceprovider() { IUnityContainer container = null; IServiceProvider serviceProvider = null; var context = await Scenario.Define <Context>() .WithEndpoint <EndpointWithExternallyManagedContainer>(endpointBuilder => { var serviceCollection = new ServiceCollection(); // register service using the IServiceCollection API: serviceCollection.AddSingleton <ServiceCollectionService>(); // register service using the NServiceBus container API: endpointBuilder.CustomConfig(endpointConfiguration => endpointConfiguration .RegisterComponents(c => c .RegisterSingleton(new InternalApiService()))); endpointBuilder.ToCreateInstance( configuration => Task.FromResult(EndpointWithExternallyManagedServiceProvider .Create(configuration, serviceCollection)), startableEndpoint => { IServiceProviderFactory <IUnityContainer> factory = new ServiceProviderFactory(container); container = factory.CreateBuilder(serviceCollection); // register service using the container native API: container.RegisterSingleton <NativeApiService>(); serviceProvider = factory.CreateServiceProvider(container); return(startableEndpoint.Start(serviceProvider)); }); endpointBuilder.When(session => session.SendLocal(new TestMessage())); }) .Done(c => c.InjectedInternalApiService != null) .Run(); Assert.AreSame(context.InjectedNativeApiService, container.Resolve <NativeApiService>()); Assert.AreSame(context.InjectedNativeApiService, serviceProvider.GetService <NativeApiService>()); Assert.AreSame(context.InjectedInternalApiService, container.Resolve <InternalApiService>()); Assert.AreSame(context.InjectedInternalApiService, serviceProvider.GetService <InternalApiService>()); Assert.AreSame(context.InjectedServiceCollectionService, container.Resolve <ServiceCollectionService>()); Assert.AreSame(context.InjectedServiceCollectionService, serviceProvider.GetService <ServiceCollectionService>()); }
public void ShouldUseDefaultValue() { //My own implementation, works // var container = new UnityContainer().WithAspNetCoreServiceProvider(); // container.Resolve<MyTestClass>(); var c = new UnityContainer(); var s1 = new ServiceCollection().AddOptions(); var s2 = new ServiceCollection().AddOptions(); s1.AddSingleton <ITest1, Test1>(); s2.AddSingleton <ITest1, Test1>(); var spf1 = new ServiceProviderFactory(c); var spf2 = new ServiceProviderFactory(c); var cc1 = spf1.CreateBuilder(s1); var cc2 = spf2.CreateBuilder(s2); var sp1 = spf1.CreateServiceProvider(cc1); var t1 = sp1.GetRequiredService <ITest1>(); var sp2 = spf2.CreateServiceProvider(cc2); var t2 = sp2.GetRequiredService <ITest1>(); Assert.AreNotSame(t2, t1); // var sp =spf.CreateServiceProvider(cc); // Assert.IsNotNull(sp.GetService<MyTestClass1>()); // var sp = cc.Resolve<IServiceProvider>(); // var ccc = cc.CreateChildContainer(); // var spp = ccc.Resolve<IServiceProvider>(); //cc.Resolve<MyTestClass>(); }
public void EnumerableWithQualifiers() { var factory = new ServiceProviderFactory(); var appCtx = factory.CreateBuilder(); appCtx.ServiceRegistry.AddSingleton <IEnum, E1>("A"); appCtx.ServiceRegistry.AddSingleton <IEnum, E2>("B"); appCtx.ServiceRegistry.AddSingleton <IEnum, E3>("A"); var usp = (IUnityAddonSP)factory.CreateServiceProvider(appCtx); var beannames = usp.GetRequiredService <IBeanDefinitionContainer>().GetAllBeanDefinitions(typeof(IEnum), "A").Select(d => d.Name); var beans = beannames.Select(n => usp.GetService <IEnum>(n)).ToArray(); Assert.IsType <E1>(beans[0]); Assert.IsType <E3>(beans[1]); Assert.Equal(2, beans.Length); Assert.Equal(usp.GetService <IEnumerable <IEnum> >("A"), beans); Assert.IsType <IEnum[]>(usp.GetService <IEnumerable <IEnum> >()); }
public static IServiceProvider UseAmbientDbConnection <TContext>(this IServiceCollection services) where TContext : DbContext => ServiceProviderFactory.CreateServiceProvider <TContext>(services);
public BaseController() { AppServiceLocator = ServiceProviderFactory.CreateServiceProvider(); }
protected override IServiceProvider CreateServiceProvider(IServiceCollection serviceCollection) { var factory = new ServiceProviderFactory(); return(factory.CreateServiceProvider(factory.CreateBuilder(serviceCollection))); }
public void TestChilds() { { var root = new UnityContainer(); root.RegisterType <AFactory>(new ContainerControlledLifetimeManager()); root.RegisterType <IAFactory, AFactory>(); var first = root.Resolve <IAFactory>(); var second = root.Resolve <IAFactory>(); Assert.AreEqual(first, second); var child = root.CreateChildContainer(); var third = child.Resolve <IAFactory>(); Assert.AreEqual(first, third); } // third.Disposed = true; { var root = new UnityContainer(); root.RegisterType <AFactory>(new ContainerControlledLifetimeManager()); root.RegisterType <IAFactory, AFactory>(); var child1 = root.CreateChildContainer(); var first = child1.Resolve <IAFactory>(); var child2 = root.CreateChildContainer(); var second = child2.Resolve <IAFactory>(); Assert.AreEqual(first, second); child1.Dispose(); global::Unity.Microsoft.DependencyInjection.ServiceProvider.ConfigureServices(new ServiceCollection()); } { var root = new UnityContainer(); root.RegisterType <AFactory>(new ContainerControlledLifetimeManager()); root.RegisterType <IAFactory, AFactory>(); var fac = new ServiceProviderFactory(root); var child = fac.CreateBuilder(new ServiceCollection()); var sp = fac.CreateServiceProvider(child); sp.GetRequiredService <IAFactory>(); if (sp is IDisposable dis) { dis.Dispose(); } var first = root.Resolve <IAFactory>(); var second = root.Resolve <IAFactory>(); Assert.AreEqual(first, second); } { var test = new UnityContainer(); } }