private static Dispatcher BuildDispatcher() { var container = new UnityContainer(); var handlerFactory = new UnityHandlerFactory(container); // var asyncHandlerFactory = new UnityAsyncHandlerFactory(container); var messageMapperFactory = new UnityMessageMapperFactory(container); container.RegisterType <IHandleRequests <GreetingEvent>, GreetingEventHandler>(); var subscriberRegistry = new SubscriberRegistry(); subscriberRegistry.Register <GreetingEvent, GreetingEventHandler>(); subscriberRegistry.Register <GreetingCommand, GreetingCommandHandler>(); //create message mappers var messageMapperRegistry = new MessageMapperRegistry(messageMapperFactory) { { typeof(GreetingEvent), typeof(GreetingEventMessageMapper) } }; var connectionString = @"Database=BrighterSqlQueue;Server=localhost;Integrated Security=SSPI;"; var gatewayConfig = new MsSqlMessagingGatewayConfiguration(connectionString, "QueueData"); var messageConsumerFactory = new MsSqlMessageConsumerFactory(gatewayConfig); var commandProcessor = CommandProcessorBuilder.With() .Handlers(new HandlerConfiguration(subscriberRegistry, handlerFactory)) .DefaultPolicy() .NoTaskQueues() .RequestContextFactory(new InMemoryRequestContextFactory()) .Build(); var dispatcher = DispatchBuilder.With() .CommandProcessor(commandProcessor) .MessageMappers(messageMapperRegistry) .DefaultChannelFactory(new MsSqlInputChannelFactory(messageConsumerFactory)) .Connections(new Connection[] { new Connection <GreetingEvent>( new ConnectionName("paramore.example.greeting"), new ChannelName("greeting.event"), new RoutingKey("greeting.event"), timeoutInMilliseconds: 200) } ).Build(); container.RegisterInstance <IAmACommandProcessor>(commandProcessor); container.RegisterType <IHandleRequests <GreetingCommand>, GreetingCommandHandler>(); container.RegisterType <MyValidationHandler <GreetingCommand>, GreetingCommandValidationHandler>(); container.RegisterType <MyPostAuditHandler <GreetingCommand>, GreetingCommandPostAuditHandler>(); return(dispatcher); }
public static async Task Main(string[] args) { Log.Logger = new LoggerConfiguration() .MinimumLevel.Debug() .Enrich.FromLogContext() .WriteTo.Console() .CreateLogger(); var host = new HostBuilder() .ConfigureServices((hostContext, services) => { var subscriptions = new Subscription[] { new Subscription <GreetingEvent>( new SubscriptionName("paramore.example.greeting"), new ChannelName("greeting.event"), new RoutingKey("greeting.event"), timeoutInMilliseconds: 200) }; //create the gateway var messagingConfiguration = new MsSqlConfiguration( @"Database=BrighterSqlQueue;Server=.\sqlexpress;Integrated Security=SSPI;", queueStoreTable: "QueueData"); var messageConsumerFactory = new MsSqlMessageConsumerFactory(messagingConfiguration); services.AddServiceActivator(options => { options.Subscriptions = subscriptions; options.ChannelFactory = new ChannelFactory(messageConsumerFactory); }) .UseInMemoryOutbox() .UseExternalBus(new MsSqlMessageProducer(messagingConfiguration)) .AutoFromAssemblies(); services.AddHostedService <ServiceActivatorHostedService>(); }) .UseConsoleLifetime() .UseSerilog() .Build(); await host.RunAsync(); }
private static async Task Main() { Log.Logger = new LoggerConfiguration() .MinimumLevel.Warning() .WriteTo.Console() .CreateLogger(); var host = new HostBuilder() .ConfigureServices((hostContext, services) => { var subscriptions = new Subscription[] { new Subscription <CompetingConsumerCommand>( new SubscriptionName("paramore.example.multipleconsumer.command"), new ChannelName("multipleconsumer.command"), new RoutingKey("multipleconsumer.command"), timeoutInMilliseconds: 200) }; var messagingConfiguration = new MsSqlConfiguration(@"Database=BrighterSqlQueue;Server=.\sqlexpress;Integrated Security=SSPI;", queueStoreTable: "QueueData"); var messageConsumerFactory = new MsSqlMessageConsumerFactory(messagingConfiguration); services.AddServiceActivator(options => { options.Subscriptions = subscriptions; options.ChannelFactory = new ChannelFactory(messageConsumerFactory); }) .UseInMemoryOutbox() .UseExternalBus(new MsSqlMessageProducer(messagingConfiguration)) .AutoFromAssemblies(); services.AddHostedService <ServiceActivatorHostedService>(); services.AddHostedService <RunStuff>(); services.AddSingleton <IAmACommandCounter, CommandCounter>(); }) .UseConsoleLifetime() .UseSerilog() .Build(); await host.RunAsync(); }
private static void Main() { Log.Logger = new LoggerConfiguration() .MinimumLevel.Warning() .WriteTo.LiterateConsole() .CreateLogger(); var container = new TinyIoCContainer(); var handlerFactory = new TinyIocHandlerFactory(container); var messageMapperFactory = new TinyIoCMessageMapperFactory(container); container.Register <IAmACommandCounter, CommandCounter>(); container.Register <IHandleRequests <CompetingConsumerCommand>, CompetingConsumerCommandHandler>(); var subscriberRegistry = new SubscriberRegistry(); subscriberRegistry.Register <CompetingConsumerCommand, CompetingConsumerCommandHandler>(); //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 policyRegistry = new PolicyRegistry { { CommandProcessor.RETRYPOLICY, retryPolicy }, { CommandProcessor.CIRCUITBREAKER, circuitBreakerPolicy } }; //create message mappers var messageMapperRegistry = new MessageMapperRegistry(messageMapperFactory) { { typeof(CompetingConsumerCommand), typeof(CompetingConsumerCommandMessageMapper) } }; //create the gateway var messagingConfiguration = new MsSqlMessagingGatewayConfiguration( @"Database=BrighterSqlQueue;Server=.\sqlexpress;Integrated Security=SSPI;", "QueueData"); var messageConsumerFactory = new MsSqlMessageConsumerFactory(messagingConfiguration); var dispatcher = DispatchBuilder.With() .CommandProcessor(CommandProcessorBuilder.With() .Handlers(new HandlerConfiguration(subscriberRegistry, handlerFactory)) .Policies(policyRegistry) .NoTaskQueues() .RequestContextFactory(new InMemoryRequestContextFactory()) .Build()) .MessageMappers(messageMapperRegistry) .DefaultChannelFactory(new MsSqlInputChannelFactory(messageConsumerFactory)) .Connections(new Connection[] { new Connection <CompetingConsumerCommand>( new ConnectionName("paramore.example.multipleconsumer.command"), new ChannelName("multipleconsumer.command"), new RoutingKey("multipleconsumer.command"), timeoutInMilliseconds: 200) }).Build(); dispatcher.Receive(); Console.WriteLine("Press Enter to stop receiving ..."); Console.ReadLine(); dispatcher.End().Wait(); var count = container.Resolve <IAmACommandCounter>().Counter; Console.WriteLine($"There were {count} commands handled by this consumer"); Console.WriteLine("Press Enter to exit ..."); Console.ReadLine(); }
public static void Main(string[] args) { Log.Logger = new LoggerConfiguration() .WriteTo.Console() .CreateLogger(); var container = new TinyIoCContainer(); var handlerFactory = new TinyIocHandlerFactory(container); var messageMapperFactory = new TinyIoCMessageMapperFactory(container); container.Register <IHandleRequests <GreetingEvent>, GreetingEventHandler>(); var subscriberRegistry = new SubscriberRegistry(); subscriberRegistry.Register <GreetingEvent, GreetingEventHandler>(); //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 policyRegistry = new PolicyRegistry { { CommandProcessor.RETRYPOLICY, retryPolicy }, { CommandProcessor.CIRCUITBREAKER, circuitBreakerPolicy } }; //create message mappers var messageMapperRegistry = new MessageMapperRegistry(messageMapperFactory) { { typeof(GreetingEvent), typeof(GreetingEventMessageMapper) } }; //create the gateway var messagingConfiguration = new MsSqlMessagingGatewayConfiguration( @"Database=BrighterSqlQueue;Server=.\sqlexpress;Integrated Security=SSPI;", "QueueData"); var messageConsumerFactory = new MsSqlMessageConsumerFactory(messagingConfiguration); var dispatcher = DispatchBuilder.With() .CommandProcessor(CommandProcessorBuilder.With() .Handlers(new HandlerConfiguration(subscriberRegistry, handlerFactory)) .Policies(policyRegistry) .NoTaskQueues() .RequestContextFactory(new InMemoryRequestContextFactory()) .Build()) .MessageMappers(messageMapperRegistry) .DefaultChannelFactory(new ChannelFactory(messageConsumerFactory)) .Connections(new Connection[] { new Connection <GreetingEvent>( new ConnectionName("paramore.example.greeting"), new ChannelName("greeting.event"), new RoutingKey("greeting.event"), timeoutInMilliseconds: 200) }).Build(); dispatcher.Receive(); Console.WriteLine("Press Enter to stop ..."); Console.ReadLine(); dispatcher.End().Wait(); }