public static IServiceCollection AddMicrowaveWebApi(
            this IServiceCollection services,
            Action <MicrowaveWebApiConfiguration> addConfiguration)
        {
            var microwaveConfiguration = new MicrowaveWebApiConfiguration();

            addConfiguration.Invoke(microwaveConfiguration);

            services.AddMicrowaveMvcExtensions();

            services.AddSingleton(microwaveConfiguration);
            services.AddSingleton(microwaveConfiguration.ServiceLocations);
            services.AddSingleton(microwaveConfiguration.MicrowaveHttpClientFactory);
            services.AddSingleton(new DiscoveryConfiguration {
                ServiceName = microwaveConfiguration.ServiceName
            });

            services.AddTransient <IDiscoveryHandler, DiscoveryHandler>();
            services.AddTransient <DiscoveryPoller>();
            services.AddSingleton <IHostedService, MicrowaveBackgroundService <DiscoveryPoller> >();
            services.AddSingleton(new PollingInterval <DiscoveryPoller>("*/1 * * * *"));

            services.AddTransient <IServiceDiscoveryRepository, DiscoveryRepository>();

            services.AddTransient <DomainEventController>();
            services.AddTransient <DiscoveryController>();
            services.AddTransient <IDiscoveryClientFactory, DiscoveryClientFactory>();

            services.AddTransient <IDomainEventFactory, DomainEventFactory>();
            services.AddTransient <IDomainEventClientFactory, DomainEventClientFactory>();

            var eventRegistration = new EventRegistration();

            var assemblies = GetAllAssemblies();

            AddPublishedEventCollection(services, assemblies, microwaveConfiguration);
            AddEventAndReadModelSubscriptions(services, assemblies);

            foreach (var assembly in assemblies)
            {
                services.AddDomainEventRegistration(assembly, eventRegistration);
            }

            services.AddSingleton(eventRegistration);

            return(services);
        }
        private static void AddPublishedEventCollection(IServiceCollection services,
                                                        IEnumerable <Assembly> domainEventAssemblies, MicrowaveWebApiConfiguration microwaveWebApiConfiguration)
        {
            var publishedEvents = new List <EventSchema>();

            foreach (var assembly in domainEventAssemblies)
            {
                var eventsForPublish = GetEventsForPublish(assembly);
                var notAddedYet      = eventsForPublish.Where(e => publishedEvents.All(w => w.Name != e.Name));
                publishedEvents.AddRange(notAddedYet);
            }

            var publishedEventCollection = EventsPublishedByService.Reachable(
                new ServiceEndPoint(null, microwaveWebApiConfiguration.ServiceName),
                publishedEvents);

            services.AddSingleton(publishedEventCollection);
        }