예제 #1
0
        public static void Register(IComponentRegistry registry, IEventStoreConfiguration configuration)
        {
            Guard.AgainstNull(registry, nameof(registry));
            Guard.AgainstNull(configuration, nameof(configuration));

            registry.AttemptRegisterInstance(configuration);

            registry.RegistryBootstrap();

            registry.AttemptRegister <IEventMethodInvokerConfiguration, EventMethodInvokerConfiguration>();
            registry.AttemptRegister <IEventMethodInvoker, DefaultEventMethodInvoker>();
            registry.AttemptRegister <ISerializer, DefaultSerializer>();
            registry.AttemptRegister <IConcurrenyExceptionSpecification, DefaultConcurrenyExceptionSpecification>();

            registry.AttemptRegister <IProjectionEventProvider, ProjectionEventProvider>();

            registry.AttemptRegister <ITransactionScopeObserver, TransactionScopeObserver>();

            if (!registry.IsRegistered <ITransactionScopeFactory>())
            {
                var transactionScopeConfiguration =
                    configuration.TransactionScope ?? new TransactionScopeConfiguration();

                registry.AttemptRegisterInstance <ITransactionScopeFactory>(
                    new DefaultTransactionScopeFactory(transactionScopeConfiguration.Enabled,
                                                       transactionScopeConfiguration.IsolationLevel,
                                                       TimeSpan.FromSeconds(transactionScopeConfiguration.TimeoutSeconds)));
            }

            registry.AttemptRegister <IPipelineFactory, DefaultPipelineFactory>();

            var reflectionService = new ReflectionService();

            foreach (var type in reflectionService.GetTypesAssignableTo <IPipeline>(typeof(EventStore).Assembly))
            {
                if (type.IsInterface || type.IsAbstract || registry.IsRegistered(type))
                {
                    continue;
                }

                registry.Register(type, type, Lifestyle.Transient);
            }

            foreach (var type in reflectionService.GetTypesAssignableTo <IPipelineObserver>(typeof(EventStore).Assembly))
            {
                if (type.IsInterface || type.IsAbstract)
                {
                    continue;
                }

                var interfaceType = type.InterfaceMatching($"I{type.Name}");

                if (interfaceType != null)
                {
                    if (registry.IsRegistered(type))
                    {
                        continue;
                    }

                    registry.Register(interfaceType, type, Lifestyle.Singleton);
                }
                else
                {
                    throw new ApplicationException(string.Format(Resources.ObserverInterfaceMissingException, type.Name));
                }
            }

            registry.AttemptRegister <IEventStore, EventStore>();
            registry.AttemptRegister <IEventProcessor, EventProcessor>();
        }
예제 #2
0
        public static void Register(IComponentRegistry registry, IServiceBusConfiguration configuration)
        {
            Guard.AgainstNull(registry, nameof(registry));
            Guard.AgainstNull(configuration, nameof(configuration));

            registry.RegistryBootstrap();

            registry.AttemptRegisterInstance(configuration);

            registry.AttemptRegister <IServiceBusEvents, ServiceBusEvents>();
            registry.AttemptRegister <ISerializer, DefaultSerializer>();
            registry.AttemptRegister <IServiceBusPolicy, DefaultServiceBusPolicy>();
            registry.AttemptRegister <IMessageRouteProvider, DefaultMessageRouteProvider>();
            registry.AttemptRegister <IIdentityProvider, DefaultIdentityProvider>();
            registry.AttemptRegister <IMessageHandlerInvoker, DefaultMessageHandlerInvoker>();
            registry.AttemptRegister <IMessageHandlingAssessor, DefaultMessageHandlingAssessor>();
            registry.AttemptRegister <IUriResolver, DefaultUriResolver>();
            registry.AttemptRegister <IQueueManager, QueueManager>();
            registry.AttemptRegister <IWorkerAvailabilityManager, WorkerAvailabilityManager>();
            registry.AttemptRegister <ISubscriptionManager, NullSubscriptionManager>();
            registry.AttemptRegister <IIdempotenceService, NullIdempotenceService>();
            registry.AttemptRegister <ITransactionScopeObserver, TransactionScopeObserver>();
            registry.AttemptRegister <ICancellationTokenSource, DefaultCancellationTokenSource>();

            if (!registry.IsRegistered <ITransactionScopeFactory>())
            {
                var transactionScopeConfiguration = configuration.TransactionScope ??
                                                    new TransactionScopeConfiguration();

                registry.AttemptRegisterInstance <ITransactionScopeFactory>(
                    new DefaultTransactionScopeFactory(transactionScopeConfiguration.Enabled,
                                                       transactionScopeConfiguration.IsolationLevel,
                                                       TimeSpan.FromSeconds(transactionScopeConfiguration.TimeoutSeconds)));
            }

            registry.AttemptRegister <IPipelineFactory, DefaultPipelineFactory>();
            registry.AttemptRegister <ITransportMessageFactory, DefaultTransportMessageFactory>();

            var reflectionService = new ReflectionService();

            foreach (var type in reflectionService.GetTypesAssignableTo <IPipeline>(typeof(ServiceBus).Assembly))
            {
                if (type.IsInterface || type.IsAbstract || registry.IsRegistered(type))
                {
                    continue;
                }

                registry.Register(type, type, Lifestyle.Transient);
            }

            foreach (var type in reflectionService.GetTypesAssignableTo <IPipelineObserver>(typeof(ServiceBus).Assembly))
            {
                if (type.IsInterface || type.IsAbstract)
                {
                    continue;
                }

                var interfaceType = type.InterfaceMatching($"I{type.Name}");

                if (interfaceType != null)
                {
                    if (registry.IsRegistered(type))
                    {
                        continue;
                    }

                    registry.Register(interfaceType, type, Lifestyle.Singleton);
                }
                else
                {
                    throw new EsbConfigurationException(string.Format(Resources.ObserverInterfaceMissingException, type.Name));
                }
            }

            if (configuration.RegisterHandlers)
            {
                foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
                {
                    foreach (var type in reflectionService.GetTypesAssignableTo(MessageHandlerType, assembly))
                    {
                        foreach (var @interface in type.GetInterfaces())
                        {
                            if ([email protected](MessageHandlerType))
                            {
                                continue;
                            }

                            var genericType = MessageHandlerType.MakeGenericType(@interface.GetGenericArguments()[0]);

                            if (!registry.IsRegistered(genericType))
                            {
                                registry.Register(genericType, type, Lifestyle.Transient);
                            }
                        }
                    }
                }
            }

            var queueFactoryType = typeof(IQueueFactory);
            var queueFactoryImplementationTypes = new HashSet <Type>();

            void AddQueueFactoryImplementationType(Type type)
            {
                queueFactoryImplementationTypes.Add(type);
            }

            if (configuration.ScanForQueueFactories)
            {
                foreach (var type in new ReflectionService().GetTypesAssignableTo <IQueueFactory>())
                {
                    AddQueueFactoryImplementationType(type);
                }
            }

            foreach (var type in configuration.QueueFactoryTypes)
            {
                AddQueueFactoryImplementationType(type);
            }

            registry.RegisterCollection(queueFactoryType, queueFactoryImplementationTypes, Lifestyle.Singleton);

            registry.AttemptRegister <IServiceBus, ServiceBus>();
        }