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 redisConnection = new RedisMessagingGatewayConfiguration { RedisConnectionString = "localhost:6379?connectTimeout=1&sendTImeout=1000&", MaxPoolSize = 10, MessageTimeToLive = TimeSpan.FromMinutes(10) }; var redisConsumerFactory = new RedisMessageConsumerFactory(redisConnection); services.AddServiceActivator(options => { options.Subscriptions = subscriptions; options.ChannelFactory = new ChannelFactory(redisConsumerFactory); }) .UseInMemoryOutbox() .UseExternalBus(new RedisMessageProducer(redisConnection)) .AutoFromAssemblies(); services.AddHostedService <ServiceActivatorHostedService>(); }) .UseConsoleLifetime() .UseSerilog() .Build(); await host.RunAsync(); }
/// <summary> /// Initializes a new instance of the <see cref="InputChannelFactory"/> class. /// </summary> /// <param name="messageConsumerFactory">The messageConsumerFactory.</param> public InputChannelFactory(RedisMessageConsumerFactory messageConsumerFactory) { _messageConsumerFactory = messageConsumerFactory; }
static void Main(string[] args) { Log.Logger = new LoggerConfiguration() .WriteTo.LiterateConsole() .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 redisConnection = new RedisMessagingGatewayConfiguration { RedisConnectionString = "localhost:6379?connectTimeout=1&sendTImeout=1000&", MaxPoolSize = 10, MessageTimeToLive = TimeSpan.FromMinutes(10) }; var redisConsumerFactory = new RedisMessageConsumerFactory(redisConnection); var dispatcher = DispatchBuilder.With() .CommandProcessor(CommandProcessorBuilder.With() .Handlers(new HandlerConfiguration(subscriberRegistry, handlerFactory)) .Policies(policyRegistry) .NoTaskQueues() .RequestContextFactory(new InMemoryRequestContextFactory()) .Build()) .MessageMappers(messageMapperRegistry) .DefaultChannelFactory(new InputChannelFactory(redisConsumerFactory)) .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(); }