// This method gets called by the runtime. Use this method to add services to the container. public IServiceProvider ConfigureServices(IServiceCollection services) { // Integrate with Simple Injector services.AddSimpleInjector(_simpleInjectorContainer); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); // Register ActorSystem services.AddSingleton <ActorSystem>(sp => { var system = ActorSystem.Create("bookstore", ConfigurationLoader.Load()); return(system); }); services.AddSingleton <IDependencyResolver>(sp => { var system = sp.GetService <ActorSystem>(); IDependencyResolver resolver = new SimpleInjectorDependencyResolver(_simpleInjectorContainer, system); return(resolver); }); services.AddActorProvider <BooksManagerActor>("BooksManagerActor"); _simpleInjectorContainer.AddInMemoryBookstoreStore(); var serviceProvider = services // Save yourself pain and agony and always use "validateScopes: true" .BuildServiceProvider(validateScopes: true) // Ensures framework components can be retrieved .UseSimpleInjector(_simpleInjectorContainer); return(serviceProvider); }
public AccountManagerActorShould() { ApiDependencyResolverSystem.Start(); var resolver = new SimpleInjectorDependencyResolver(ApiDependencyResolverSystem.GetContainer(), Sys); _accountManagerActorRef = Sys.ActorOf(Sys.DI().Props <AccountManagerActor>(), nameof(AccountManagerActor)); }
protected void Application_Start() { var container = new Container(); // Simple Injector for MVC container.Options.DefaultScopedLifestyle = new WebRequestLifestyle(); container.RegisterMvcControllers(Assembly.GetExecutingAssembly()); container.RegisterMvcIntegratedFilterProvider(); // Simple Injector - other services var baseContainer = new BaseContainer(container: container); var resolver = new SimpleInjectorDependencyResolver(baseContainer.Container); DependencyResolver.SetResolver(resolver); // Very Important MyServiceLocator.SetIoCContainer(baseContainer.Container); MyMvcServiceLocator.SetIoCContainer(resolver); // Initialize MVC settings AppStartInitializer.Initialize(); NHSessionManager.AddEntityAssemblies(new[] { "MultiTenancyFramework.Mvc" }); }
static async Task MainAsync(string[] args) { var container = new Container(); container.RegisterSingleton <IWorkProcessor, WorkProcessor>(); var argsParsed = CommandLine.Parser.Default.ParseArguments <Options>(args); var ops = argsParsed.MapResult(a => a, _ => throw new InvalidOperationException()); Console.WriteLine($"Agents set to {ops.Agents}"); ActorSystemInstance = ActorSystem.Create("TheActorSystem"); var system = ActorSystemInstance; // Create the dependency resolver IDependencyResolver resolver = new SimpleInjectorDependencyResolver(container, system); var workItems = Enumerable.Range(0, ops.Tasks) .Select(i => i % 2 == 0 ? new NumbersWorkDefinition(i + 1, ops.SleepingTimeIsMs) : new DoubleWorkDefinition(i + 1, ops.SleepingTimeIsMs)) .ToList(); var workPool = system.ActorOf(Props.Create <WorkPool>(workItems)); var router = system.ActorOf(resolver.Create <Agent>().WithRouter(new BroadcastPool(ops.Agents)), "some-pool"); var coordinator = system.ActorOf(Props.Create <Coordinator>(ops.Agents, router, workPool)); coordinator.Tell(new Coordinator.GoMsg()); // This blocks the current thread from exiting until MyActorSystem is shut down // The ConsoleReaderActor will shut down the ActorSystem once it receives an // "exit" command from the user await ActorSystemInstance.WhenTerminated; }
public void GetServices_should_return_registered_services( Container Container, SimpleInjectorDependencyResolver dependencyResolver, object firstRegisteredService, object secondRegisteredService, IEnumerable <object> retrievedServices) { "Given a service of a particular type" .Given(() => firstRegisteredService = new object()); "And another service of the same type" .And(() => secondRegisteredService = new object()); "And a Container" .And(() => Container = new Container()); "With both services registered" .And(() => Container.RegisterAll(firstRegisteredService, secondRegisteredService)); "And a SimpleInjectorDependencyResolver constructed with the Container" .And(() => dependencyResolver = new SimpleInjectorDependencyResolver(Container)); "When GetServices is called for the particular service type" .When(() => retrievedServices = dependencyResolver.GetServices(typeof(object))); "Then the registered services should be returned" .Then(() => retrievedServices.Should().BeEquivalentTo(firstRegisteredService, secondRegisteredService)); }
public void Unresolveable_Should_Return_Default() { var resolver = new SimpleInjectorDependencyResolver(); var result = resolver.Resolve <Class3>(); Assert.IsNotNull(result); }
public void Unresolveable_Should_Return_Null() { var resolver = new SimpleInjectorDependencyResolver(); var result = resolver.Resolve <IFace1>(); Assert.IsNull(result); }
public static void Main(string[] args) { var container = new Container(); var dependencyResolver = new SimpleInjectorDependencyResolver(container); container.Register<ILogger, ConsoleLogger>(); var helper = new ExampleConsoleApp(dependencyResolver); helper.Run(MainAsync); }
public void Configure(Action <SimpleInjectorDependencyResolver> reconfigurer) { _rabbitmqLogger = new RabbitMqLogEventListener(); var container = new Container(); container.RegisterSingleton <IConnectionFactory, ConnectionFactory>(); _configurer = new SimpleInjectorDependencyResolver(container); reconfigurer(_configurer); _configurer.Verify(); }
public void IsRegistered_Should_Return_True() { var resolver = new SimpleInjectorDependencyResolver(); resolver.RegisterType <IFace1, Class1>(); resolver.RegisterType <IFace1, Class2>(); resolver.RegisterInstance <IFace1>(new Class3()); var result = resolver.IsRegistered <IFace1>(); Assert.IsTrue(result); }
public void Array_Injector_Should_Inject_All_Registed_Types() { var resolver = new SimpleInjectorDependencyResolver(); resolver.RegisterType<IFace1, Class1>(); resolver.RegisterType<IFace1, Class2>(); resolver.RegisterInstance<IFace1>(new Class3()); var resolve = resolver.Resolve<Class4>(); Assert.AreEqual(3, resolve.Ifaces.Count()); Assert.IsTrue(resolve.Ifaces.OfType<Class1>().Any()); Assert.IsTrue(resolve.Ifaces.OfType<Class2>().Any()); Assert.IsTrue(resolve.Ifaces.OfType<Class3>().Any()); }
public void SimpleInjectorResolveSingleInstance() { var container = new global::SimpleInjector.Container(); var registered = new Registered(); container.RegisterSingle(typeof(Registered), registered); var resolver = new SimpleInjectorDependencyResolver(container); var resolved = resolver.GetInstance(typeof(Registered)); Assert.That(resolved, Is.EqualTo(registered)); }
public void RegisterFactory_WithSingleton_Should_Return_OneInstance() { var resolver = new SimpleInjectorDependencyResolver(); resolver.RegisterFactory(typeof(Class6), () => new Class6(), LifetimeScope.Singleton); var result = resolver.Resolve <Class6>(); var anotherResult = resolver.Resolve <Class6>(); Assert.IsNotNull(result); Assert.AreEqual(result, anotherResult); Assert.AreEqual(1, Class6.Instances); }
public void Array_Injector_Should_Inject_One_Registed_Type_As_Enumerable() { var resolver = new SimpleInjectorDependencyResolver(); resolver.RegisterType <IFace1, Class1>(); var resolve = resolver.Resolve <Class5>(); Assert.AreEqual(1, resolve.Ifaces.Count()); Assert.IsTrue(resolve.Ifaces.OfType <Class1>().Any()); Assert.IsFalse(resolve.Ifaces.OfType <Class2>().Any()); Assert.IsFalse(resolve.Ifaces.OfType <Class3>().Any()); }
public void BeginScope_should_return_the_dependency_resolver_itself( SimpleInjectorDependencyResolver dependencyResolver, IDependencyScope dependencyScope) { "Given a SimpleInjectorDependencyResolver" .Given(() => dependencyResolver = new SimpleInjectorDependencyResolver(new Container())); "When BeginScope is called" .When(() => dependencyScope = dependencyResolver.BeginScope()); "Then the dependency scope returned should be the dependency resolver itself" .Then(() => dependencyScope.Should().BeSameAs(dependencyResolver)); }
private static IServiceCollection ConfigureServices() { IServiceCollection services = new ServiceCollection(); services.AddSimpleInjector(Container); services.AddSingleton <ActorSystem>(sp => { var system = ActorSystem.Create("bookstore", ConfigurationLoader.Load()); return(system); }); services.AddSingleton <IDependencyResolver>(sp => { var system = sp.GetService <ActorSystem>(); IDependencyResolver resolver = new SimpleInjectorDependencyResolver(Container, system); return(resolver); }); services.AddSingleton <IActorFactory, MyActorFactory>(); Container.AddInMemoryBookstoreStore(); Container.Register <ILoggerFactory>(() => { LoggerFactory factory = new LoggerFactory(); var configuration = new ConfigurationBuilder() .AddJsonFile("logging.json") .Build(); //serilog provider configuration var logger = new LoggerConfiguration() .ReadFrom.Configuration(configuration) .CreateLogger(); factory.AddSerilog(logger); return(factory); }, Lifestyle.Singleton); Container.Register(typeof(ILogger <>), typeof(LoggingAdapter <>)); // services.AddActorProvider<BooksManagerActor>("BooksManagerActor"); // services.AddActorProvider<ConsoleReaderActor>("ConsoleReaderActor"); // IMPORTANT! Register our application entry point services.AddTransient <ConsoleApplication>(); return(services); }
public void Configure(Action <SimpleInjectorDependencyResolver> reconfigurer) { _rabbitmqLogger = new RabbitMqLogEventListener(); var container = new Container(); container.RegisterSingleton <IConnectionFactory, ConnectionFactory>(); container.RegisterSingleton <ISerializer>(() => new NewtonsoftSerializer(Encoding.UTF8)); container.RegisterSingleton <IMessagePublisherFactory, RabbitMqMessagePublisherFactory>(); container.RegisterSingleton <IRabbitMqPublisherSettings, DefaultRabbitMqPublisherSettings>(); _configurer = new SimpleInjectorDependencyResolver(container); reconfigurer(_configurer); _configurer.Verify(); }
protected void Application_Start() { AreaRegistration.RegisterAllAreas(); WebApiConfig.Register(GlobalConfiguration.Configuration); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); var container = InstanceFactory.RegisterModules(); var resolve = new SimpleInjectorDependencyResolver(container); DependencyResolver.SetResolver(resolve); }
public void Array_Injector_Should_Inject_All_Registed_Types() { var resolver = new SimpleInjectorDependencyResolver(); resolver.RegisterType <IFace1, Class1>(); resolver.RegisterType <IFace1, Class2>(); resolver.RegisterInstance <IFace1>(new Class3()); var resolve = resolver.Resolve <Class4>(); Assert.AreEqual(3, resolve.Ifaces.Count()); Assert.IsTrue(resolve.Ifaces.OfType <Class1>().Any()); Assert.IsTrue(resolve.Ifaces.OfType <Class2>().Any()); Assert.IsTrue(resolve.Ifaces.OfType <Class3>().Any()); }
public void ResolveAll_Should_Return_All_Registed_Types() { var resolver = new SimpleInjectorDependencyResolver(); resolver.RegisterType <IFace1, Class1>(); resolver.RegisterType <IFace1, Class2>(); resolver.RegisterInstance <IFace1>(new Class3()); var resolveAll = resolver.ResolveAll <IFace1>(); Assert.AreEqual(3, resolveAll.Count()); Assert.IsTrue(resolveAll.OfType <Class1>().Any()); Assert.IsTrue(resolveAll.OfType <Class2>().Any()); Assert.IsTrue(resolveAll.OfType <Class3>().Any()); }
private static Container InitializeSimpleInjector(IAppBuilder app, HttpConfiguration config) { // Set WepApi DependencyResolver var container = new Container(); var dependencyResolver = new SimpleInjectorDependencyResolver(container); Register(container, new WebApiRequestLifestyle()); container.RegisterWebApiControllers(config); container.Verify(); config.DependencyResolver = new SimpleInjectorWebApiDependencyResolver(container); return(container); }
/// <summary> /// Registers the set of services in the specified <paramref name="container" />. /// </summary> /// <param name="container">The container the set of services is registered into.</param> public void RegisterServices(Container container) { var resolver = new SimpleInjectorDependencyResolver(container); resolver.InitializeSplat(); resolver.InitializeReactiveUI(); RxApp.MainThreadScheduler = Avalonia.Threading.AvaloniaScheduler.Instance; RegisterAvaloniaServices(container); RegisterReactiveServices(container); RegisterConfigurations(container); RegisterViews(container); RegisterViewModels(container); RegisterImplementations(container); RegisterControls(container); }
public static void Initialize() { _container = new Container(); _container.Options.DefaultScopedLifestyle = new WebRequestLifestyle(true); InitializeContainer(_container); _container.RegisterMvcControllers(Assembly.GetExecutingAssembly()); _container.RegisterMvcIntegratedFilterProvider(); var dependencyResolver = new SimpleInjectorDependencyResolver(_container); DependencyResolver.SetResolver(dependencyResolver); DomainEvent.Container = new IoCContainer(dependencyResolver); _container.Verify(); }
public void GetServices_should_return_empty_enumerable_when_service_is_not_registered( Container Container, SimpleInjectorDependencyResolver dependencyResolver, IEnumerable <object> retrievedServices) { "Given a Container with no services registered" .Given(() => Container = new Container()); "And a SimpleInjectorDependencyResolver constructed with the Container" .And(() => dependencyResolver = new SimpleInjectorDependencyResolver(Container)); "When GetServices is called" .When(() => retrievedServices = dependencyResolver.GetServices(typeof(object))); "Then retrieved services should be an empty enumerable" .Then(() => retrievedServices.Should().BeEmpty()); }
public void GetService_should_return_null_when_service_is_not_registered( Container Container, SimpleInjectorDependencyResolver dependencyResolver, object retrievedService) { "Given a Container with no services registered" .Given(() => Container = new Container()); "And a SimpleInjectorDependencyResolver constructed with the Container" .And(() => dependencyResolver = new SimpleInjectorDependencyResolver(Container)); "When GetService is called" .When(() => retrievedService = dependencyResolver.GetService(typeof(object))); "Then retrieved service should be null" .Then(() => retrievedService.Should().BeNull()); }
public static void Initialize() { var container = new Container(); container.Options.DefaultScopedLifestyle = new WebRequestLifestyle(); InitializeContainer(container); container.RegisterMvcControllers(Assembly.GetExecutingAssembly()); var injector = new SimpleInjectorDependencyResolver(container); DependencyResolver.SetResolver(injector); SimpleInjectorInitializerFactory.SetContainer(container); container.Verify(); }
/// <summary>Initialize the container and register it as MVC3 Dependency Resolver.</summary> public static void Initialize() { // Did you know the container can diagnose your configuration? // Go to: https://simpleinjector.org/diagnostics var container = new Container(); InitializeContainer(container); container.RegisterMvcControllers(Assembly.GetExecutingAssembly()); container.Verify(); var resolver = new SimpleInjectorDependencyResolver(container); DependencyResolver.SetResolver(resolver); GlobalConfiguration.Configuration.DependencyResolver = new SimpleInjectorWebApiDependencyResolver(container); _notificationService = container.GetInstance <INotificationService>(); _notificationService.Start(); }
/// <summary>Initialize the container and register it as MVC3 Dependency Resolver.</summary> public static void Initialize() { var container = new Container(); container.Options.DefaultScopedLifestyle = new WebRequestLifestyle(); InitializeContainer(container); container.RegisterMvcControllers(Assembly.GetExecutingAssembly()); var resolver = new SimpleInjectorDependencyResolver(container); DependencyResolver.SetResolver(resolver); DomainEvent.Container = new DomainEventsContainer(resolver); container.Verify(); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters, container); }
public CreditHandlerActorShould() { var conf = new IgniteConfiguration { IgniteInstanceName = "Billingware.Cache.Tests." + Guid.NewGuid().ToString("N"), Logger = new CustomIgniteSerilogLogger(), ClientMode = false, // ConsistentId = Guid.NewGuid().ToString("N"), WorkDirectory = AppDomain.CurrentDomain.BaseDirectory, MetricsLogFrequency = TimeSpan.Zero, // MetricsUpdateFrequency = TimeSpan.Zero, Localhost = "0.0.0.0", DiscoverySpi = new TcpDiscoverySpi { IpFinder = new TcpDiscoveryStaticIpFinder() { Endpoints = new[] { "localhost" } }, LocalPort = 48500, LocalPortRange = 10, }, CommunicationSpi = new TcpCommunicationSpi { LocalPort = 48100 } }; CommonIgniterService.Start(conf, true); ApiDependencyResolverSystem.Start(); var resolver = new SimpleInjectorDependencyResolver(ApiDependencyResolverSystem.GetContainer(), Sys); _creditActorRef = Sys.ActorOf(Sys.DI().Props <CreditRequestActor>(), nameof(CreditRequestActor)); var accountingActorRef = Sys.ActorOf(Sys.DI().Props <AccountingActor>(), nameof(AccountingActor)); Sys.EventStream.Subscribe(accountingActorRef, typeof(DebitAccount)); Sys.EventStream.Subscribe(accountingActorRef, typeof(CreditAccount)); Sys.EventStream.Subscribe(accountingActorRef, typeof(PersistTransaction)); }
/// <summary> /// This method starts the Core ActorSystem /// </summary> public static void Start() { var conf = File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory + "\\core-remote-server.conf"); _actorSystem = ActorSystem.Create(nameof(CoreActorSystem), ConfigurationFactory.ParseString(conf)); ApiDependencyResolverSystem.Start(); _resolver = new SimpleInjectorDependencyResolver(ApiDependencyResolverSystem.GetContainer(), _actorSystem); TopLevelActors.DebitHandlerActor = _actorSystem.ActorOf(_actorSystem.DI() .Props <DebitRequestActor>() .WithSupervisorStrategy(GetDefaultSupervisorStrategy) .WithRouter(FromConfig.Instance), nameof(DebitRequestActor)); TopLevelActors.CreditsHandlerActor = _actorSystem.ActorOf(_actorSystem.DI() .Props <CreditRequestActor>() .WithSupervisorStrategy(GetDefaultSupervisorStrategy) .WithRouter(FromConfig.Instance), nameof(CreditRequestActor)); TopLevelActors.AccountingActor = _actorSystem.ActorOf(_actorSystem.DI() .Props <AccountingActor>() .WithSupervisorStrategy(GetDefaultSupervisorStrategy) .WithRouter(FromConfig.Instance), nameof(AccountingActor)); _actorSystem.EventStream.Subscribe(TopLevelActors.AccountingActor, typeof(DebitAccount)); _actorSystem.EventStream.Subscribe(TopLevelActors.AccountingActor, typeof(CreditAccount)); _actorSystem.EventStream.Subscribe(TopLevelActors.AccountingActor, typeof(PersistTransaction)); }
public void GetService_should_return_registered_service( Container Container, SimpleInjectorDependencyResolver dependencyResolver, object registeredService, object retrievedService) { "Given a service of a particular type" .Given(() => registeredService = new object()); "And a Container" .And(() => Container = new Container()); "With the service registered" .And(() => Container.RegisterSingle(() => registeredService)); "And a SimpleInjectorDependencyResolver constructed with the Container" .And(() => dependencyResolver = new SimpleInjectorDependencyResolver(Container)); "When GetService is called for the particular service type" .When(() => retrievedService = dependencyResolver.GetService(typeof(object))); "Then the registered service should be returned" .Then(() => retrievedService.Should().BeSameAs(registeredService)); }
public void IsRegistered_Should_Return_True() { var resolver = new SimpleInjectorDependencyResolver(); resolver.RegisterType<IFace1, Class1>(); resolver.RegisterType<IFace1, Class2>(); resolver.RegisterInstance<IFace1>(new Class3()); var result = resolver.IsRegistered<IFace1>(); Assert.IsTrue(result); }
public void SimpleInjectorResolveSingleInstance() { var container = new global::SimpleInjector.Container(); var registered = new Registered(); container.RegisterSingle(typeof(Registered), registered); var resolver = new SimpleInjectorDependencyResolver(container); var resolved = resolver.GetInstance(typeof (Registered)); Assert.That(resolved, Is.EqualTo(registered)); }
public void Array_Injector_Should_Inject_One_Registed_Type_As_Enumerable() { var resolver = new SimpleInjectorDependencyResolver(); resolver.RegisterType<IFace1, Class1>(); var resolve = resolver.Resolve<Class5>(); Assert.AreEqual(1, resolve.Ifaces.Count()); Assert.IsTrue(resolve.Ifaces.OfType<Class1>().Any()); Assert.IsFalse(resolve.Ifaces.OfType<Class2>().Any()); Assert.IsFalse(resolve.Ifaces.OfType<Class3>().Any()); }
public void ResolveAll_Should_Return_All_Registed_Types() { var resolver = new SimpleInjectorDependencyResolver(); resolver.RegisterType<IFace1, Class1>(); resolver.RegisterType<IFace1, Class2>(); resolver.RegisterInstance<IFace1>(new Class3()); var resolveAll = resolver.ResolveAll<IFace1>(); Assert.AreEqual(3, resolveAll.Count()); Assert.IsTrue(resolveAll.OfType<Class1>().Any()); Assert.IsTrue(resolveAll.OfType<Class2>().Any()); Assert.IsTrue(resolveAll.OfType<Class3>().Any()); }
public void Unresolveable_Should_Return_Default() { var resolver = new SimpleInjectorDependencyResolver(); var result = resolver.Resolve<Class3>(); Assert.IsNotNull(result); }
public void Unresolveable_Should_Return_Null() { var resolver = new SimpleInjectorDependencyResolver(); var result = resolver.Resolve<IFace1>(); Assert.IsNull(result); }