public async Task <TResponse> Handle(TRequest message) { using (ThreadScopedLifestyle.BeginScope(_container)) { var result = await _decorateeFactory.Invoke().Handle(message); return(result); } }
static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); SetupContainer(); using (ThreadScopedLifestyle.BeginScope(container)) { Application.Run(container.GetInstance <Form1>()); } }
public void LifetimeScopeDispose_ExecutedOnDifferentThreadThanItWasStarted_DoesNotThrowExceptionException() { // Arrange var container = new Container(); var scope = ThreadScopedLifestyle.BeginScope(container); // Act Task.Factory.StartNew(() => scope.Dispose()).Wait(); }
static void Main(string[] args) { using (ThreadScopedLifestyle.BeginScope(container)) { Console.WriteLine("Start AppStore Payment Consumer. Ctrl-C to exit."); var paymentAppService = container.GetInstance <IPaymentAppService>(); var consumer = new PaymentConsumer(paymentAppService); consumer.Listen(msg => Console.WriteLine(msg)); } }
public void CanWriteOrgToDb() { using (ThreadScopedLifestyle.BeginScope(container)) { var db = Database; Organization org = new Organization(); org.Name = "super org!"; db.Organizations.Add(org); db.SaveChanges(); } }
public void BeginLifetimeScope_WithoutAnyLifetimeScopeRegistrations_Succeeds() { // Arrange var container = new Container(); // Act using (var scope = ThreadScopedLifestyle.BeginScope(container)) { container.GetInstance <ConcreteCommand>(); } }
public void WhenScopeEnds_NullActionArgument_ThrowsException() { // Arrange var lifestyle = new ThreadScopedLifestyle(); // Act Action action = () => lifestyle.WhenScopeEnds(new Container(), null); // Assert AssertThat.Throws <ArgumentNullException>(action); }
public void WhenScopeEnds_WithMultipleDisposableComponentsAndPropertyDependencyDependingOnEachOther_DependsComponentsInExpectedOrder() { // Arrange var expectedOrderOfDisposal = new List <Type> { typeof(Middle), typeof(Inner), typeof(PropertyDependency), }; var actualOrderOfDisposal = new List <Type>(); var container = new Container(); container.Options.DefaultScopedLifestyle = new ThreadScopedLifestyle(); // Allow PropertyDependency to be injected as property on Inner container.Options.PropertySelectionBehavior = new InjectProperties <ImportAttribute>(); // PropertyDependency, Middle and Inner all depend on Func<object> and call it when disposed. // This way we can check in which order the instances are disposed. container.RegisterInstance <Action <object> >(instance => actualOrderOfDisposal.Add(instance.GetType())); // Middle depends on Inner that depends on property PropertyDependency. // Registration is deliberately made in a different order to prevent that the order of // registration might influence the order of disposing. container.Register <PropertyDependency>(Lifestyle.Scoped); container.Register <Middle>(Lifestyle.Scoped); container.Register <Inner>(Lifestyle.Scoped); // Act Scope scope = ThreadScopedLifestyle.BeginScope(container); try { // Resolve the outer most object. container.GetInstance <Middle>(); } finally { // Act scope.Dispose(); } // Assert Assert.IsTrue( expectedOrderOfDisposal.SequenceEqual(actualOrderOfDisposal), "Types were expected to be disposed in the following order: {0}, " + "but they actually were disposed in the order: {1}. " + "Since PropertyDependency is injected as property into Inner, it is important that " + "PropertyDependency is disposed after Inner.", string.Join(", ", expectedOrderOfDisposal.Select(type => type.Name)), string.Join(", ", actualOrderOfDisposal.Select(type => type.Name))); }
static void Main() { ///pegar do aprender a alteracao/// ///pegar dependencia/// Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); using (ThreadScopedLifestyle.BeginScope(Dependencias.Container)) { Dependencias.Configurar(); Application.Run(Dependencias.Container.GetInstance <FrmMenuPrincipal>()); } }
public void Db_SalesService_DeleteArticleAsync() { using (ThreadScopedLifestyle.BeginScope(container)) { SalesService service = container.GetInstance <SalesService>(); Assert.That(async() => { await service.DeleteAsync(this.article); }, Throws.Nothing); } }
public async Task Db_SalesService_CreateArticleAsync() { using (ThreadScopedLifestyle.BeginScope(container)) { SalesService service = container.GetInstance <SalesService>(); this.article.Id = 0; int result = await service.CreateAsync(this.article); Assert.Greater(this.article.Id, 0); } }
public void GetInstance_RegistrationUsingFuncThreadScopedLifestyle_Succeeds() { // Arrange var container = new Container(); container.Register <ICommand>(() => new ConcreteCommand(), new ThreadScopedLifestyle()); using (ThreadScopedLifestyle.BeginScope(container)) { // Act container.GetInstance <ICommand>(); } }
public async Task Db_ErrorsReportingService_GetApplicationAsync() { using (ThreadScopedLifestyle.BeginScope(container)) { ErrorsReportingService service = container.GetInstance <ErrorsReportingService>(); ErrorReportApplication application = await service.GetApplicationAsync("TestApplicationAlreadyExisting", "a.a.a.a"); Assert.IsNotNull(application); Assert.Greater(application.Id, 0); Assert.AreEqual(new DateTime(2000, 1, 1), application.FirstRunDate); } }
public void Db_ErrorsReportingService_CreateApplicationAsync_AlreadyExists() { using (ThreadScopedLifestyle.BeginScope(container)) { ErrorsReportingService service = container.GetInstance <ErrorsReportingService>(); DalException ex = Assert.ThrowsAsync <DalException>(async() => { await service.CreateApplicationAsync("TestApplicationAlreadyExisting", "a.a.a.a"); }); Assert.That(ex.errorType, Is.EqualTo(DalErrorType.SqlUniqueConstraintViolation)); } }
public override JobActivatorScope BeginScope(JobActivatorContext context) { if (_lifestyle == null) { return(new SimpleInjectorScope(_container, _container.BeginExecutionContextScope())); } else if (_lifestyle == Lifestyle.Scoped) { return(new SimpleInjectorScope(_container, ThreadScopedLifestyle.BeginScope(_container))); } return(new SimpleInjectorScope(_container, _container.GetCurrentExecutionContextScope())); }
public async Task Db_ErrorsReportingService_CreateApplicationAsync() { using (ThreadScopedLifestyle.BeginScope(container)) { ErrorsReportingService service = container.GetInstance <ErrorsReportingService>(); ErrorReportApplication result = await service.CreateApplicationAsync("TestApplicationAsync", "a.a.a.a"); Assert.IsNotNull(result); Assert.Greater(result.Id, 0); this.dataSet.ApplicationsIds.Add(result.Id); } }
public void DefaultScopedLifestyle_ChangedBeforeAnyRegistrations_ChangesThePropertyToTheSetInstance() { // Arrange var expectedLifestyle = new ThreadScopedLifestyle(); var options = GetContainerOptions(); // Act options.DefaultScopedLifestyle = expectedLifestyle; // Assert Assert.AreSame(expectedLifestyle, options.DefaultScopedLifestyle, "The set_DefaultScopedLifestyle did not work."); }
static void Main(string[] args) { while (true) { using (ThreadScopedLifestyle.BeginScope(container)) { var service = container.GetInstance <IMyService>(); Console.WriteLine(service.HelloWorld()); } Thread.Sleep(TimeSpan.FromSeconds(1)); } }
public void RegisterForDisposal_WithNullArgument_ThrowsExpectedException() { // Arrange var container = new Container(); using (var scope = ThreadScopedLifestyle.BeginScope(container)) { // Act Action action = () => scope.RegisterForDisposal(null); // Assert AssertThat.Throws <ArgumentNullException>(action); } }
public void WhenScopeEnds_WithMultipleDisposableComponentsDependingOnEachOther_DependsComponentsInExpectedOrder() { // Arrange var expectedOrderOfDisposal = new List <Type> { typeof(Outer), typeof(Middle), typeof(Inner), }; var actualOrderOfDisposal = new List <Type>(); var container = new Container(); container.Options.DefaultScopedLifestyle = new ThreadScopedLifestyle(); // Outer, Middle and Inner all depend on Func<object> and call it when disposed. // This way we can check in which order the instances are disposed. container.RegisterInstance <Action <object> >(instance => actualOrderOfDisposal.Add(instance.GetType())); // Outer depends on Middle that depends on Inner. // Registration is deliberately made in a different order to prevent that the order of // registration might influence the order of disposing. container.Register <Middle>(Lifestyle.Scoped); container.Register <Inner>(Lifestyle.Scoped); container.Register <Outer>(Lifestyle.Scoped); Scope scope = ThreadScopedLifestyle.BeginScope(container); try { // Resolve the outer most object. container.GetInstance <Outer>(); } finally { // Act scope.Dispose(); } // Assert Assert.IsTrue( expectedOrderOfDisposal.SequenceEqual(actualOrderOfDisposal), "Types were expected to be disposed in the following order: {0}, " + "but they actually were disposed in the order: {1}. " + "This order is important, because when a components gets disposed, it might still want to " + "call the components it depends on, but at that time those components are already disposed.", string.Join(", ", expectedOrderOfDisposal.Select(type => type.Name)), string.Join(", ", actualOrderOfDisposal.Select(type => type.Name))); }
private void DoWork() { try { using (ThreadScopedLifestyle.BeginScope(this.container)) { var service = this.container.GetInstance <TService>(); this.settings.Action(service); } } catch (Exception ex) { //this.logger.LogError(ex, ex.Message); } }
public void GetInstance_ScopedRegistrationWithActiveScope_Succeeds() { // Arrange var container = new Container(); container.Options.DefaultScopedLifestyle = new ThreadScopedLifestyle(); container.Register <RealTimeProvider>(Lifestyle.Scoped); using (ThreadScopedLifestyle.BeginScope(container)) { // Act container.GetInstance <RealTimeProvider>(); } }
public void ScopeWrapper(Action executeWithinScope) { switch (_scopedLifetime) { case ScopedLifetime.ThreadScoped: using (ThreadScopedLifestyle.BeginScope(_container)) executeWithinScope(); break; case ScopedLifetime.AsyncScoped: using (AsyncScopedLifestyle.BeginScope(_container)) executeWithinScope(); break; } }
public void Db_ErrorsReportingService_GetApplicationAsync_NotExisting() { using (ThreadScopedLifestyle.BeginScope(container)) { ErrorsReportingService service = container.GetInstance <ErrorsReportingService>(); ErrorReportApplication application = null; Assert.That(async() => { application = await service.GetApplicationAsync("TestApplicationAlreadyExisting", "z.z.z.z"); }, Throws.Nothing); Assert.IsNull(application); } }
public void GetInstance_RequestingScopeWithActiveLifetimeScopeAndDefaultScopedLifestyleSet_ResolvesTheActiveScope() { // Arrange var container = ContainerFactory.New(); container.Options.DefaultScopedLifestyle = new ThreadScopedLifestyle(); Scope activeScope = ThreadScopedLifestyle.BeginScope(container); // Act var resolvedScope = container.GetInstance <Scope>(); // Assert Assert.AreSame(activeScope, resolvedScope); }
public void GetInstance_ResolvingAnInstanceDependingOnScopeWithAnActiveLifetimeScopeAndDefaultScopedLifestyleSet_InjectsTheActiveScope() { // Arrange var container = ContainerFactory.New(); container.Options.DefaultScopedLifestyle = new ThreadScopedLifestyle(); Scope activeScope = ThreadScopedLifestyle.BeginScope(container); // Act Scope injectedScope = container.GetInstance <ServiceDependingOn <Scope> >().Dependency; // Assert Assert.AreSame(activeScope, injectedScope); }
public void DefaultLifestyle_ChangedMultipleTimesBeforeAnyRegistrations_ChangesThePropertyToTheSetInstance() { // Arrange var expectedLifestyle = new ThreadScopedLifestyle(); var options = GetContainerOptions(); // Act options.DefaultLifestyle = Lifestyle.Singleton; options.DefaultLifestyle = Lifestyle.Transient; // Assert Assert.AreSame(Lifestyle.Transient, options.DefaultLifestyle, "The set_DefaultLifestyle did not work."); }
public void ResolveDefaultSingleton() { using (var container = new Container()) { container.Options.DefaultLifestyle = Lifestyle.Singleton; container.AddSolrNet("http://localhost:8983/solr"); container.Verify(); using (ThreadScopedLifestyle.BeginScope(container)) { var uow1 = container.GetInstance <ISolrOperations <Entity> >(); } } }
public void GetInstance_WithinLifetimeScope_ReturnsInstanceOfExpectedType() { // Arrange var container = new Container(); container.Register <ICommand, ConcreteCommand>(new ThreadScopedLifestyle()); using (ThreadScopedLifestyle.BeginScope(container)) { // Act var actualInstance = container.GetInstance <ICommand>(); // Assert AssertThat.IsInstanceOfType(typeof(ConcreteCommand), actualInstance); } }
public void GetInstance_OnLifetimeScopedObject_WillNotBeDisposedDuringLifetimeScope() { // Arrange var container = new Container(); container.Register <DisposableCommand>(new ThreadScopedLifestyle()); // Act using (ThreadScopedLifestyle.BeginScope(container)) { var command = container.GetInstance <DisposableCommand>(); // Assert Assert.IsFalse(command.HasBeenDisposed, "The instance should not be disposed inside the scope."); } }