예제 #1
0
        public PublishingToMultipleSubscribersAsyncTests()
        {
            var registry = new SubscriberRegistry();

            registry.RegisterAsync <MyEvent, MyEventHandlerAsync>();
            registry.RegisterAsync <MyEvent, MyOtherEventHandlerAsync>();
            registry.RegisterAsync <MyEvent, MyThrowingEventHandlerAsync>();

            var container = new ServiceCollection();

            container.AddTransient <MyEventHandlerAsync>();
            container.AddTransient <MyOtherEventHandlerAsync>();
            container.AddTransient <MyThrowingEventHandlerAsync>();
            container.AddSingleton(_receivedMessages);
            container.AddSingleton <IBrighterOptions>(new BrighterOptions()
            {
                HandlerLifetime = ServiceLifetime.Transient
            });

            var handlerFactory = new ServiceProviderHandlerFactory(container.BuildServiceProvider());


            _commandProcessor = new CommandProcessor(registry, handlerFactory, new InMemoryRequestContextFactory(), new PolicyRegistry());
            PipelineBuilder <MyEvent> .ClearPipelineCache();
        }
        public void Establish()
        {
            var registry = new SubscriberRegistry();

            registry.RegisterAsync <MyEvent, MyEventHandlerAsync>();
            registry.RegisterAsync <MyEvent, MyOtherEventHandlerAsync>();

            var container      = new TinyIoCContainer();
            var handlerFactory = new TinyIocHandlerFactoryAsync(container);

            container.Register <IHandleRequestsAsync <MyEvent>, MyEventHandlerAsync>("MyEventHandlerAsync");
            container.Register <IHandleRequestsAsync <MyEvent>, MyOtherEventHandlerAsync>("MyOtherHandlerAsync");

            _commandProcessor = new CommandProcessor(registry, handlerFactory, new InMemoryRequestContextFactory(), new PolicyRegistry());
        }
        public MonitorHandlerMustObserveButRethrowTests()
        {
            _controlBusSender = new SpyControlBusSender();
            var registry = new SubscriberRegistry();

            registry.RegisterAsync <MyCommand, MyMonitoredHandlerThatThrowsAsync>();

            var container      = new TinyIoCContainer();
            var handlerFactory = new TinyIocHandlerFactoryAsync(container);

            container.Register <IHandleRequestsAsync <MyCommand>, MyMonitoredHandlerThatThrowsAsync>();
            container.Register <IHandleRequestsAsync <MyCommand>, MonitorHandlerAsync <MyCommand> >();
            container.Register <IAmAControlBusSenderAsync>(_controlBusSender);
            container.Register(new MonitorConfiguration {
                IsMonitoringEnabled = true, InstanceName = "UnitTests"
            });

            _commandProcessor = new CommandProcessor(registry, handlerFactory, new InMemoryRequestContextFactory(), new PolicyRegistry());

            _command = new MyCommand();

            _originalRequestAsJson = JsonConvert.SerializeObject(_command);

            _at = DateTime.UtcNow.AddMilliseconds(-500);
        }
예제 #4
0
        public CommandProcessorUsingInboxAsyncTests()
        {
            _inbox = new InMemoryInbox();

            var registry = new SubscriberRegistry();

            registry.RegisterAsync <MyCommand, MyStoredCommandHandlerAsync>();

            var container = new ServiceCollection();

            container.AddTransient <MyStoredCommandHandlerAsync>();
            container.AddTransient <MyStoredCommandToFailHandlerAsync>();
            container.AddSingleton(_inbox);
            container.AddTransient <UseInboxHandlerAsync <MyCommand> >();

            var handlerFactory = new ServiceProviderHandlerFactory(container.BuildServiceProvider());

            _contextKey = typeof(MyStoredCommandHandlerAsync).FullName;

            _command = new MyCommand {
                Value = "My Test String"
            };

            _commandProcessor = new CommandProcessor(registry, (IAmAHandlerFactoryAsync)handlerFactory, new InMemoryRequestContextFactory(), new PolicyRegistry());
        }
예제 #5
0
        public void Establish()
        {
            _controlBusSender = new SpyControlBusSender();
            var registry = new SubscriberRegistry();

            registry.RegisterAsync <MyCommand, MyMonitoredHandlerAsync>();

            var container      = new TinyIoCContainer();
            var handlerFactory = new TinyIocHandlerFactoryAsync(container);

            container.Register <IHandleRequestsAsync <MyCommand>, MyMonitoredHandlerAsync>();
            container.Register <IHandleRequestsAsync <MyCommand>, MonitorHandlerAsync <MyCommand> >();
            container.Register <IAmAControlBusSenderAsync>(_controlBusSender);
            container.Register <MonitorConfiguration>(new MonitorConfiguration {
                IsMonitoringEnabled = true, InstanceName = "UnitTests"
            });

            _commandProcessor = new CommandProcessor(registry, handlerFactory, new InMemoryRequestContextFactory(), new PolicyRegistry());

            _command = new MyCommand();

            _originalRequestAsJson = JsonConvert.SerializeObject(_command);

            _at = DateTime.UtcNow;
            Clock.OverrideTime = _at;
        }
        public MessagePumpDispatchAsyncTests()
        {
            var subscriberRegistry = new SubscriberRegistry();

            subscriberRegistry.RegisterAsync <MyEvent, MyEventHandlerAsyncWithContinuation>();

            var handlerFactory   = new TestHandlerFactoryAsync <MyEvent, MyEventHandlerAsyncWithContinuation>(() => new MyEventHandlerAsyncWithContinuation());
            var commandProcessor = new CommandProcessor(
                subscriberRegistry,
                handlerFactory,
                new InMemoryRequestContextFactory(),
                new PolicyRegistry());

            var channel = new FakeChannel();
            var mapper  = new MyEventMessageMapper();

            _messagePump = new MessagePumpAsync <MyEvent>(commandProcessor, mapper)
            {
                Channel = channel, TimeoutInMilliseconds = 5000
            };

            var message = new Message(new MessageHeader(Guid.NewGuid(), "MyTopic", MessageType.MT_EVENT), new MessageBody(JsonConvert.SerializeObject(_myEvent)));

            channel.Add(message);
            var quitMessage = new Message(new MessageHeader(Guid.Empty, "", MessageType.MT_QUIT), new MessageBody(""));

            channel.Add(quitMessage);
        }
예제 #7
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddSingleton <IGameRepository, GameRepository>();

            services.AddDarker()
            .AddHandlersFromAssemblies(typeof(GameQueryHandler).Assembly);

            services.AddTransient <NewGameActionHandlerAsync>();

            services.AddTransient <IAmACommandProcessor>((provider) => {
                var registry = new SubscriberRegistry();
                registry.RegisterAsync <NewGameAction, NewGameActionHandlerAsync>();

                var commandProcessorBuilder = CommandProcessorBuilder.With()
                                              .Handlers(new HandlerConfiguration(registry, new ActionHandlerFactoryAsync(provider)))
                                              .DefaultPolicy()
                                              .NoTaskQueues()
                                              .RequestContextFactory(new InMemoryRequestContextFactory());

                return(commandProcessorBuilder.Build());
            });

            services.AddControllers();

            services.AddCors();

            // Register the Swagger generator, defining 1 or more Swagger documents
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo {
                    Title = "Yacht Tea API", Version = "v1"
                });
            });
        }
        public MonitorHandlerPipelineAsyncTests()
        {
            _controlBusSender = new SpyControlBusSender();
            var registry = new SubscriberRegistry();

            registry.RegisterAsync <MyCommand, MyMonitoredHandlerAsync>();

            var container = new ServiceCollection();

            container.AddTransient <MyMonitoredHandlerAsync>();
            container.AddTransient <MonitorHandlerAsync <MyCommand> >();
            container.AddSingleton <IAmAControlBusSenderAsync>(_controlBusSender);
            container.AddSingleton(new MonitorConfiguration {
                IsMonitoringEnabled = true, InstanceName = "UnitTests"
            });

            var handlerFactory = new ServiceProviderHandlerFactory(container.BuildServiceProvider());

            _commandProcessor = new CommandProcessor(registry, (IAmAHandlerFactoryAsync)handlerFactory, new InMemoryRequestContextFactory(), new PolicyRegistry());

            _command = new MyCommand();

            _originalRequestAsJson = JsonConvert.SerializeObject(_command);

            _at = DateTime.UtcNow.AddMilliseconds(-500);
        }
예제 #9
0
        public CommandProcessorWithCircuitBreakerAsyncTests()
        {
            var registry = new SubscriberRegistry();

            registry.RegisterAsync <MyCommand, MyFailsWithDivideByZeroHandlerAsync>();

            var container = new ServiceCollection();

            container.AddSingleton <MyFailsWithDivideByZeroHandlerAsync>();
            container.AddSingleton <ExceptionPolicyHandlerAsync <MyCommand> >();

            var handlerFactory = new ServiceProviderHandlerFactory(container.BuildServiceProvider());


            var policyRegistry = new PolicyRegistry();

            var policy = Policy
                         .Handle <DivideByZeroException>()
                         .CircuitBreakerAsync(2, TimeSpan.FromMinutes(1));

            policyRegistry.Add("MyDivideByZeroPolicy", policy);

            MyFailsWithDivideByZeroHandlerAsync.ReceivedCommand = false;

            _commandProcessor = new CommandProcessor(registry, (IAmAHandlerFactoryAsync)handlerFactory, new InMemoryRequestContextFactory(), policyRegistry);
        }
예제 #10
0
        public PipelineGlobalInboxNoInboxAttributeAsyncTests()
        {
            _inbox = new InMemoryInbox();

            var registry = new SubscriberRegistry();

            registry.RegisterAsync <MyCommand, MyNoInboxCommandHandlerAsync>();

            var container = new ServiceCollection();

            container.AddTransient <MyNoInboxCommandHandlerAsync>();
            container.AddSingleton <IAmAnInboxSync>(_inbox);
            container.AddSingleton <IBrighterOptions>(new BrighterOptions()
            {
                HandlerLifetime = ServiceLifetime.Transient
            });

            var handlerFactory = new ServiceProviderHandlerFactory(container.BuildServiceProvider());

            _requestContext = new RequestContext();

            _inboxConfiguration = new InboxConfiguration();

            _chainBuilder = new PipelineBuilder <MyCommand>(registry, (IAmAHandlerFactoryAsync)handlerFactory, _inboxConfiguration);
        }
        //TODO: Because we use a global logger with Serilog, this won't run in parallel
        //[Fact]
        public async Task When_A_Request_Logger_Is_In_The_Pipeline_Async()
        {
            Log.Logger = new LoggerConfiguration().MinimumLevel.Information().WriteTo.TestCorrelator().CreateLogger();
            using (var context = TestCorrelator.CreateContext())
            {
                var myCommand = new MyCommand();

                var registry = new SubscriberRegistry();
                registry.RegisterAsync <MyCommand, MyLoggedHandlerAsync>();

                var container = new ServiceCollection();
                container.AddTransient <MyLoggedHandlerAsync, MyLoggedHandlerAsync>();
                container.AddTransient(typeof(RequestLoggingHandlerAsync <>), typeof(RequestLoggingHandlerAsync <>));

                var handlerFactory = new ServiceProviderHandlerFactory(container.BuildServiceProvider());

                var commandProcessor = new CommandProcessor(registry, handlerFactory, handlerFactory,
                                                            new InMemoryRequestContextFactory(), new PolicyRegistry());


                await commandProcessor.SendAsync(myCommand);

                //_should_log_the_request_handler_call
                //_should_log_the_type_of_handler_in_the_call
                TestCorrelator.GetLogEventsFromContextGuid(context.Guid)
                .Should().Contain(x => x.MessageTemplate.Text.StartsWith("Logging handler pipeline call"))
                .Which.Properties["1"].ToString().Should().Be($"\"{typeof(MyCommand)}\"");

                commandProcessor?.Dispose();
            }
        }
예제 #12
0
        private void RegisterCommandProcessor()
        {
            //create handler
            var servicesHandlerFactory = new ServicesHandlerFactoryAsync(_container);
            var subscriberRegistry     = new SubscriberRegistry();

            _container.Register <IHandleRequestsAsync <AddGreetingCommand>, AddGreetingCommandHandlerAsync>(Lifestyle.Scoped);

            subscriberRegistry.RegisterAsync <AddGreetingCommand, AddGreetingCommandHandlerAsync>();

            //create policies
            var retryPolicy               = Policy.Handle <Exception>().WaitAndRetry(new[] { TimeSpan.FromMilliseconds(50), TimeSpan.FromMilliseconds(100), TimeSpan.FromMilliseconds(150) });
            var circuitBreakerPolicy      = Policy.Handle <Exception>().CircuitBreaker(1, TimeSpan.FromMilliseconds(500));
            var retryPolicyAsync          = Policy.Handle <Exception>().WaitAndRetryAsync(new[] { TimeSpan.FromMilliseconds(50), TimeSpan.FromMilliseconds(100), TimeSpan.FromMilliseconds(150) });
            var circuitBreakerPolicyAsync = Policy.Handle <Exception>().CircuitBreakerAsync(1, TimeSpan.FromMilliseconds(500));
            var policyRegistry            = new PolicyRegistry()
            {
                { CommandProcessor.RETRYPOLICY, retryPolicy },
                { CommandProcessor.CIRCUITBREAKER, circuitBreakerPolicy },
                { CommandProcessor.RETRYPOLICYASYNC, retryPolicyAsync },
                { CommandProcessor.CIRCUITBREAKERASYNC, circuitBreakerPolicyAsync }
            };


            var commandProcessor = CommandProcessorBuilder.With()
                                   .Handlers(new Paramore.Brighter.HandlerConfiguration(subscriberRegistry, servicesHandlerFactory))
                                   .Policies(policyRegistry)
                                   .NoTaskQueues()
                                   .RequestContextFactory(new Paramore.Brighter.InMemoryRequestContextFactory())
                                   .Build();

            _container.RegisterSingleton <IAmACommandProcessor>(commandProcessor);
        }
예제 #13
0
        public PipelineGlobalInboxWhenUseInboxAsyncTests()
        {
            _inbox = new InMemoryInbox();

            var registry = new SubscriberRegistry();

            registry.RegisterAsync <MyCommand, MyCommandInboxedHandlerAsync>();

            var container = new ServiceCollection();

            container.AddTransient <MyCommandInboxedHandlerAsync>();
            container.AddSingleton <IAmAnInboxAsync>((IAmAnInboxAsync)_inbox);
            container.AddTransient <UseInboxHandlerAsync <MyCommand> >();

            var handlerFactory = new ServiceProviderHandlerFactory(container.BuildServiceProvider());



            _requestContext = new RequestContext();

            _inboxConfiguration = new InboxConfiguration(
                scope: InboxScope.All,
                onceOnly: true,
                actionOnExists: OnceOnlyAction.Throw);

            _chainBuilder = new PipelineBuilder <MyCommand>(registry, (IAmAHandlerFactoryAsync)handlerFactory, _inboxConfiguration);
        }
        public CommandProcessorPublishMultipleMatchesAsyncTests()
        {
            var registry = new SubscriberRegistry();

            registry.RegisterAsync <MyEvent, MyEventHandlerAsync>();
            registry.RegisterAsync <MyEvent, MyOtherEventHandlerAsync>();

            var container      = new TinyIoCContainer();
            var handlerFactory = new TinyIocHandlerFactoryAsync(container);

            container.Register <IHandleRequestsAsync <MyEvent>, MyEventHandlerAsync>();
            container.Register <IHandleRequestsAsync <MyEvent>, MyOtherEventHandlerAsync>();
            container.Register(_receivedMessages);

            _commandProcessor = new CommandProcessor(registry, handlerFactory, new InMemoryRequestContextFactory(), new PolicyRegistry());
        }
예제 #15
0
        public OnceOnlyAttributeAsyncTests()
        {
            _inbox = new InMemoryInbox();

            var registry = new SubscriberRegistry();

            registry.RegisterAsync <MyCommand, MyStoredCommandHandlerAsync>();

            var container = new ServiceCollection();

            container.AddTransient <MyStoredCommandHandlerAsync>();
            container.AddSingleton(_inbox);
            container.AddTransient <UseInboxHandlerAsync <MyCommand> >();
            container.AddSingleton <IBrighterOptions>(new BrighterOptions()
            {
                HandlerLifetime = ServiceLifetime.Transient
            });


            var handlerFactory = new ServiceProviderHandlerFactory(container.BuildServiceProvider());

            _command = new MyCommand {
                Value = "My Test String"
            };

            _commandProcessor = new CommandProcessor(registry, handlerFactory, new InMemoryRequestContextFactory(), new PolicyRegistry());
        }
 public void RegisterAsync <TRequest, TImplementation>()
     where TRequest : class, IRequest
     where TImplementation : class, IHandleRequestsAsync <TRequest>
 {
     _services.AddTransient <TImplementation>();
     _registry.RegisterAsync <TRequest, TImplementation>();
 }
예제 #17
0
        public MonitorHandlerMustObserveButRethrowTests()
        {
            _controlBusSender = new SpyControlBusSender();
            var registry = new SubscriberRegistry();

            registry.RegisterAsync <MyCommand, MyMonitoredHandlerThatThrowsAsync>();

            var container = new ServiceCollection();

            container.AddTransient <MyMonitoredHandlerThatThrowsAsync>();
            container.AddTransient <MonitorHandlerAsync <MyCommand> >();
            container.AddSingleton <IAmAControlBusSenderAsync>(_controlBusSender);
            container.AddSingleton(new MonitorConfiguration {
                IsMonitoringEnabled = true, InstanceName = "UnitTests"
            });
            container.AddSingleton <IBrighterOptions>(new BrighterOptions()
            {
                HandlerLifetime = ServiceLifetime.Transient
            });

            var handlerFactory = new ServiceProviderHandlerFactory(container.BuildServiceProvider());

            _commandProcessor = new CommandProcessor(registry, handlerFactory, new InMemoryRequestContextFactory(), new PolicyRegistry());

            _command = new MyCommand();

            _originalRequestAsJson = JsonSerializer.Serialize(_command, JsonSerialisationOptions.Options);

            _at = DateTime.UtcNow.AddMilliseconds(-500);
        }
예제 #18
0
        public void Establish()
        {
            var registry = new SubscriberRegistry();

            registry.RegisterAsync <MyCommand, MyFailsWithFallbackDivideByZeroHandlerAsync>();

            var container      = new TinyIoCContainer();
            var handlerFactory = new TinyIocHandlerFactoryAsync(container);

            container.Register <IHandleRequestsAsync <MyCommand>, MyFailsWithFallbackDivideByZeroHandlerAsync>().AsSingleton();
            container.Register <IHandleRequestsAsync <MyCommand>, ExceptionPolicyHandlerAsync <MyCommand> >().AsSingleton();

            var policyRegistry = new PolicyRegistry();

            var policy = Policy
                         .Handle <DivideByZeroException>()
                         .WaitAndRetryAsync(new[]
            {
                1.Seconds(),
                2.Seconds(),
                3.Seconds()
            }, (exception, timeSpan) =>
            {
                s_retryCount++;
            });

            policyRegistry.Add("MyDivideByZeroPolicy", policy);

            MyFailsWithFallbackDivideByZeroHandlerAsync.ReceivedCommand = false;

            s_commandProcessor = new CommandProcessor(registry, handlerFactory, new InMemoryRequestContextFactory(), policyRegistry);
        }
        public CommandProcessorWithRetryPolicyAsyncTests()
        {
            var registry = new SubscriberRegistry();

            registry.RegisterAsync <MyCommand, MyFailsWithFallbackDivideByZeroHandlerAsync>();

            var container = new ServiceCollection();

            container.AddSingleton <MyFailsWithFallbackDivideByZeroHandlerAsync>();
            container.AddSingleton <ExceptionPolicyHandlerAsync <MyCommand> >();
            container.AddSingleton <FallbackPolicyHandlerRequestHandlerAsync <MyCommand> >();

            _provider = container.BuildServiceProvider();
            var handlerFactory = new ServiceProviderHandlerFactory(_provider);

            var policyRegistry = new PolicyRegistry();

            var policy = Policy
                         .Handle <DivideByZeroException>()
                         .WaitAndRetryAsync(new[]
            {
                10.Milliseconds(),
                20.Milliseconds(),
                30.Milliseconds()
            }, (exception, timeSpan) =>
            {
                _retryCount++;
            });

            policyRegistry.Add("MyDivideByZeroPolicy", policy);

            _provider.GetService <MyFailsWithFallbackDivideByZeroHandlerAsync>().ReceivedCommand = false;

            _commandProcessor = new CommandProcessor(registry, (IAmAHandlerFactoryAsync)handlerFactory, new InMemoryRequestContextFactory(), policyRegistry);
        }
예제 #20
0
        public void Establish()
        {
            var registry = new SubscriberRegistry();

            registry.RegisterAsync <MyCommand, MyCommandHandlerAsync>();
            registry.RegisterAsync <MyCommand, MyImplicitHandlerAsync>();

            var container      = new TinyIoCContainer();
            var handlerFactory = new TinyIocHandlerFactoryAsync(container);

            container.Register <IHandleRequestsAsync <MyCommand>, MyCommandHandlerAsync>("DefaultHandler");
            container.Register <IHandleRequestsAsync <MyCommand>, MyImplicitHandlerAsync>("ImplicitHandler");
            container.Register <IHandleRequestsAsync <MyCommand>, MyLoggingHandlerAsync <MyCommand> >();

            _commandProcessor = new CommandProcessor(registry, handlerFactory, new InMemoryRequestContextFactory(), new PolicyRegistry());
        }
 public void RegisterAsync <TRequest, TImplementation>()
     where TRequest : class, IRequest
     where TImplementation : class, IHandleRequestsAsync <TRequest>
 {
     _services.Add(new ServiceDescriptor(typeof(TImplementation), typeof(TImplementation), ServiceLifetime.Transient));
     _registry.RegisterAsync <TRequest, TImplementation>();
 }
예제 #22
0
        public CommandProcessorSendMissingHandlerFactoryAsyncTests()
        {
            var registry = new SubscriberRegistry();

            registry.RegisterAsync <MyCommand, MyCommandHandlerAsync>();

            _commandProcessor = new CommandProcessor(registry, (IAmAHandlerFactoryAsync)null, new InMemoryRequestContextFactory(), new PolicyRegistry());
        }
예제 #23
0
        public CommandProcessorSendWithMultipleMatchesAsyncTests()
        {
            var registry = new SubscriberRegistry();

            registry.RegisterAsync <MyCommand, MyCommandHandlerAsync>();
            registry.RegisterAsync <MyCommand, MyImplicitHandlerAsync>();

            var container      = new TinyIoCContainer();
            var handlerFactory = new TinyIocHandlerFactoryAsync(container);

            container.Register <IHandleRequestsAsync <MyCommand>, MyCommandHandlerAsync>();
            container.Register <IHandleRequestsAsync <MyCommand>, MyImplicitHandlerAsync>();
            container.Register <IHandleRequestsAsync <MyCommand>, MyLoggingHandlerAsync <MyCommand> >();
            container.Register(_receivedMessages);

            _commandProcessor = new CommandProcessor(registry, handlerFactory, new InMemoryRequestContextFactory(), new PolicyRegistry());
        }
예제 #24
0
        public CancellingAsyncPipelineTests()
        {
            var registry = new SubscriberRegistry();

            registry.RegisterAsync <MyCommand, MyCancellableCommandHandlerAsync>();
            var handlerFactory = new TestHandlerFactoryAsync <MyCommand, MyCommandHandlerAsync>(() => new MyCommandHandlerAsync(_receivedMessages));

            _commandProcessor = new CommandProcessor(registry, handlerFactory, new InMemoryRequestContextFactory(), new PolicyRegistry());
        }
예제 #25
0
        public void Establish()
        {
            var registry = new SubscriberRegistry();

            registry.RegisterAsync <MyCommand, MyCommandHandlerAsync>();
            var handlerFactory = new TestHandlerFactoryAsync <MyCommand, MyCommandHandlerAsync>(() => new MyCommandHandlerAsync());

            _commandProcessor = new CommandProcessor(registry, handlerFactory, new InMemoryRequestContextFactory(), new PolicyRegistry());
        }
예제 #26
0
        public CommandProcessorPublishMultipleMatchesAsyncTests()
        {
            var registry = new SubscriberRegistry();

            registry.RegisterAsync <MyEvent, MyEventHandlerAsync>();
            registry.RegisterAsync <MyEvent, MyOtherEventHandlerAsync>();

            var container = new ServiceCollection();

            container.AddTransient <MyEventHandlerAsync>();
            container.AddTransient <MyOtherEventHandlerAsync>();
            container.AddSingleton(_receivedMessages);

            var handlerFactory = new ServiceProviderHandlerFactory(container.BuildServiceProvider());


            _commandProcessor = new CommandProcessor(registry, (IAmAHandlerFactoryAsync)handlerFactory, new InMemoryRequestContextFactory(), new PolicyRegistry());
            PipelineBuilder <MyEvent> .ClearPipelineCache();
        }
        public CommandProcessorSendAsyncTests()
        {
            var registry = new SubscriberRegistry();

            registry.RegisterAsync <MyCommand, MyCommandHandlerAsync>();
            var handlerFactory = new TestHandlerFactoryAsync <MyCommand, MyCommandHandlerAsync>(() => new MyCommandHandlerAsync(_receivedMessages));

            _commandProcessor = new CommandProcessor(registry, handlerFactory, new InMemoryRequestContextFactory(), new PolicyRegistry());
            PipelineBuilder <MyCommand> .ClearPipelineCache();
        }
예제 #28
0
        public PublishingToMultipleSubscribersAsyncTests()
        {
            var registry = new SubscriberRegistry();

            registry.RegisterAsync <MyEvent, MyEventHandlerAsync>();
            registry.RegisterAsync <MyEvent, MyOtherEventHandlerAsync>();
            registry.RegisterAsync <MyEvent, MyThrowingEventHandlerAsync>();

            var container      = new TinyIoCContainer();
            var handlerFactory = new TinyIocHandlerFactoryAsync(container);

            container.Register <IHandleRequestsAsync <MyEvent>, MyEventHandlerAsync>();
            container.Register <IHandleRequestsAsync <MyEvent>, MyOtherEventHandlerAsync>();
            container.Register <IHandleRequestsAsync <MyEvent>, MyThrowingEventHandlerAsync>();
            container.Register(_receivedMessages);

            _commandProcessor = new CommandProcessor(registry, handlerFactory, new InMemoryRequestContextFactory(), new PolicyRegistry());
            PipelineBuilder <MyEvent> .ClearPipelineCache();
        }
        protected override void Load(ContainerBuilder builder)
        {
            builder.Register(context =>
            {
                #region Policies
                var retryPolicy = Policy.Handle <Exception>().WaitAndRetry(new[]
                {
                    TimeSpan.FromMilliseconds(50), TimeSpan.FromMilliseconds(100), TimeSpan.FromMilliseconds(150)
                });
                var circuitBreakerPolicy = Policy.Handle <Exception>().CircuitBreaker(1, TimeSpan.FromMilliseconds(500));
                var retryPolicyAsync     = Policy.Handle <Exception>().WaitAndRetryAsync(new[]
                {
                    TimeSpan.FromMilliseconds(50), TimeSpan.FromMilliseconds(100), TimeSpan.FromMilliseconds(150)
                });
                var circuitBreakerPolicyAsync = Policy.Handle <Exception>().CircuitBreakerAsync(1, TimeSpan.FromMilliseconds(500));
                var policyRegistry            = new PolicyRegistry
                {
                    { CommandProcessor.RETRYPOLICY, retryPolicy },
                    { CommandProcessor.CIRCUITBREAKER, circuitBreakerPolicy },
                    { CommandProcessor.RETRYPOLICYASYNC, retryPolicyAsync },
                    { CommandProcessor.CIRCUITBREAKERASYNC, circuitBreakerPolicyAsync }
                };
                #endregion

                var subscriberRegistry = new SubscriberRegistry();
                subscriberRegistry.RegisterAsync <CreateArticolo, CreateArticoloCommandHandler>();
                subscriberRegistry.RegisterAsync <ModificaDescrizioneArticolo, ModificaDescrizioneArticoloCommandHandler>();

                subscriberRegistry.RegisterAsync <CreateCliente, CreateClienteCommandHandler>();


                var servicesHandlerFactory = context.Resolve <IAmAHandlerFactoryAsync>();

                return(CommandProcessorBuilder.With()
                       .Handlers(new HandlerConfiguration(subscriberRegistry, servicesHandlerFactory))
                       .Policies(policyRegistry)
                       .NoTaskQueues()
                       .RequestContextFactory(new InMemoryRequestContextFactory())
                       .Build());
            }
                             ).As <IAmACommandProcessor>().SingleInstance();
        }
        public void Establish()
        {
            var registry = new SubscriberRegistry();

            registry.RegisterAsync <MyCommand, MyCommandHandlerAsync>();
            var handlerFactory = new TestHandlerFactoryAsync <MyCommand, MyCommandHandlerAsync>(() => new MyCommandHandlerAsync());

            s_requestContext = new RequestContext();

            s_chainBuilder = new PipelineBuilder <MyCommand>(registry, handlerFactory);
        }
예제 #31
0
        private static void Main()
        {
            var container = new TinyIoCContainer();
            var asyncHandlerFactory = new TinyIocHandlerFactory(container);

            var registry = new SubscriberRegistry();
            registry.RegisterAsync<GreetingEvent, WantToBeGreeted>();
            registry.RegisterAsync<GreetingEvent, WantToBeGreetedToo>();

            var builder = CommandProcessorBuilder.With()
                .Handlers(new HandlerConfiguration(registry, asyncHandlerFactory))
                .DefaultPolicy()
                .NoTaskQueues()
                .RequestContextFactory(new InMemoryRequestContextFactory());

            var commandProcessor = builder.Build();

            // Use bootstrappers for async/await in Console app
            MainAsync(commandProcessor).Wait();

            Console.ReadLine();
        }
예제 #32
0
        private static async Task MainAsync()
        {
            var registry = new SubscriberRegistry();
            registry.RegisterAsync<GreetingCommand, GreetingCommandRequestHandlerAsync>();

            var builder = CommandProcessorBuilder.With()
                .Handlers(new HandlerConfiguration(registry, new SimpleHandlerFactoryAsync()))
                .DefaultPolicy()
                .NoTaskQueues()
                .RequestContextFactory(new InMemoryRequestContextFactory());

            var commandProcessor = builder.Build();

            // To allow the command handler(s) to release the thread
            // while doing potentially long running operations,
            // await the async (but inline) handling of the command
            await commandProcessor.SendAsync(new GreetingCommand("Ian"));

            Console.ReadLine();
        }