public CoreConfigurator RegisterSettings(IContainer container) { var settings = SettingsMapper.Map<AppSettings>(); container.Configure(config => config.For<AppSettings>().Singleton().Use(settings)); var serviceProvider = new StructureMapServiceProvider(container); container.Configure(config => config.For<IServiceProvider>().Singleton().Use(serviceProvider)); return this; }
public async Task Should_resolve_dependencies_from_serviceprovider() { Container 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 => { var registry = new Registry(); registry.Populate(serviceCollection); // register service using the container native API: registry.For <NativeApiService>().Singleton(); container = new Container(registry); serviceProvider = new StructureMapServiceProvider(container); return(startableEndpoint.Start(serviceProvider)); }); endpointBuilder.When(session => session.SendLocal(new TestMessage())); }) .Done(c => c.InjectedInternalApiService != null) .Run(); Assert.AreSame(context.InjectedNativeApiService, container.GetInstance <NativeApiService>()); Assert.AreSame(context.InjectedNativeApiService, serviceProvider.GetService <NativeApiService>()); Assert.AreSame(context.InjectedInternalApiService, container.GetInstance <InternalApiService>()); Assert.AreSame(context.InjectedInternalApiService, serviceProvider.GetService <InternalApiService>()); Assert.AreSame(context.InjectedServiceCollectionService, container.GetInstance <ServiceCollectionService>()); Assert.AreSame(context.InjectedServiceCollectionService, serviceProvider.GetService <ServiceCollectionService>()); }
public void can_start_and_tear_down_a_scope_from_the_provider() { var root = new Container(_ => { _.For <IWidget>().Use <BlueWidget>(); }); var provider = new StructureMapServiceProvider(root); Assert.Same(provider.Container, root); Assert.IsType <BlueWidget>(provider.GetRequiredService(typeof(IWidget))); provider.StartNewScope(); provider.Container.Configure(_ => _.For <IWidget>().Use <GreenWidget>()); Assert.IsType <GreenWidget>(provider.GetRequiredService(typeof(IWidget))); provider.TeardownScope(); Assert.IsType <BlueWidget>(provider.GetRequiredService(typeof(IWidget))); }