public MessagingService(IOutbox outbox, IServiceFactory factory, Profile profile = null, MessagingConfiguration configuration = null) { ProcessId = Process.GetCurrentProcess().Id; MachineName = Environment.MachineName; MachineAndProcessId = $"{MachineName}-{ProcessId}"; _configuration = configuration ?? new MessagingConfiguration(); if (outbox == null) { throw new InvalidOperationException("Outbox cannot be null. An Outbo is required to instantiate MessagingService"); } _outboxManager = new OutboxManager(outbox, this.DispatchCore, 1, _configuration.OutboxConsumerSemaphore, this.MachineAndProcessId); if (factory == null) { throw new InvalidOperationException("IServiceFactory cannot be null. An IServiceFactory is required to instantiate MessagingService"); } _messageHandlerDispatcher = new MessageDispatcher(factory); if (profile != null) { _configure(profile); } _immediateHandleRetryPolicy = Policy .Handle <Exception>(x => !(x is NonTransientException)) .RetryAsync(_maxImmediateRetryCount); }
public static IServiceRegistrar AddMessaging(this IServiceCollection services, Profile profile, EndpointBindings bindings, Action <MessagingConfiguration> config, params Assembly[] messageAndHandlerAssemblies) { MessagingService.InitializeTypes(messageAndHandlerAssemblies); if (bindings != null) { profile.AddBindings(bindings); } var messagingConfig = new MessagingConfiguration(); if (config != null) { config.Invoke(messagingConfig); } services.AddSingleton(messagingConfig); services.AddTransient <IServiceFactory>(x => new MicrosoftDependencyInjectionServiceFactory(x)); services.AddSingleton(profile); services.AddSingleton <MessagingService>(); services.AddTransient <IMessagingClient, MessagingClient>(); //setup user message handlers services.Scan(c => { c.FromAssemblies(messageAndHandlerAssemblies) .AddClasses(t => t.AssignableTo(typeof(IMessageHandler <>))) .AsImplementedInterfaces() .WithTransientLifetime() .AddClasses(t => t.AssignableTo(typeof(ICustomMessageHandler <>))) .AsImplementedInterfaces() .WithTransientLifetime() .AddClasses(t => t.AssignableTo(typeof(ISaga <,>))) .AsImplementedInterfaces() .AsSelf() .WithTransientLifetime() .AddClasses(t => t.AssignableTo(typeof(ISagaPersistence <,>))) .AsImplementedInterfaces() .WithTransientLifetime() ; }); return(new ServiceRegistrar(services)); }