예제 #1
0
        static IServiceCollection AddNServiceBus(this IServiceCollection services, EndpointConfiguration configuration)
        {
            services.AddSingleton(configuration);
            var holder = new SessionAndConfigurationHolder(configuration);

            services.AddSingleton(holder);

            services.AddHostedService <NServiceBusHost>();
            //   services.AddSingleton<NServiceBusHost>(new NServiceBusHost(holder));
            services.AddSingleton(provider =>
            {
                var management = provider.GetService <SessionAndConfigurationHolder>();
                if (management.MessageSession != null)
                {
                    return(management.MessageSession);
                }

                var timeout = TimeSpan.FromSeconds(30);

                if (!SpinWait.SpinUntil(() => management.MessageSession != null || management.StartupException != null,
                                        timeout))
                {
                    throw new TimeoutException($"Unable to resolve the message session within '{timeout.ToString()}'. If you are trying to resolve the session within hosted services it is encouraged to use `Lazy<IMessageSession>` instead of `IMessageSession` directly");
                }

                management.StartupException?.Throw();

                return(management.MessageSession);
            });
            services.AddSingleton(provider => new Lazy <IMessageSession>(provider.GetService <IMessageSession>));
            return(services);
        }
예제 #2
0
        static void AddRequiredInfrastructure(this IServiceCollection services, EndpointConfiguration configuration)
        {
            var holder = new SessionAndConfigurationHolder(configuration);

            services.AddSingleton(provider => holder.Session);
            services.AddSingleton(holder);
            services.AddHostedService <EndpointManagement>();
        }
예제 #3
0
        public static UpdateableServiceProvider AddNServiceBus(this IServiceCollection services, string endpointName)
        {
            UpdateableServiceProvider container     = null;
            EndpointConfiguration     configuration = new EndpointConfiguration(endpointName);

            configuration.UseSerialization <NewtonsoftSerializer>();
            configuration.EnableInstallers();
            configuration.DisableFeature <Heartbeats>();

            var transport = configuration.UseTransport <RabbitMQTransport>();

            configuration.UseTransport <RabbitMQTransport>();

            transport.UseDirectRoutingTopology();
            transport.ConnectionString("host=rabbitmq");
            var routing = transport.Routing();

            routing.RouteToEndpoint(typeof(Message).Assembly, "IoT.DeviceListener");

            services.AddSingleton(configuration);
            var sessionConfiguration = new SessionAndConfigurationHolder(configuration);

            services.AddSingleton(sessionConfiguration);

            configuration.UseContainer <ServicesBuilder>(customizations =>
            {
                customizations.ExistingServices(services);
                customizations.ServiceProviderFactory(sc =>
                {
                    container = new UpdateableServiceProvider(sc);
                    return(container);
                });
            });

            //container.AddHostedService<NServiceBusHost>();
            var endpointInstanse = Endpoint.Start(configuration).GetAwaiter().GetResult();

            sessionConfiguration.MessageSession = endpointInstanse;

            container.AddSingleton(provider =>
            {
                var management = provider.GetService <SessionAndConfigurationHolder>();

                if (management.MessageSession != null)
                {
                    return(management.MessageSession);
                }

                var timeout = TimeSpan.FromSeconds(30);

                if (!SpinWait.SpinUntil(() => management.MessageSession != null || management.StartupException != null,
                                        timeout))
                {
                    throw new TimeoutException($"Unable to resolve the message session within '{timeout.ToString()}'. If you are trying to resolve the session within hosted services it is encouraged to use `Lazy<IMessageSession>` instead of `IMessageSession` directly");
                }

                management.StartupException?.Throw();

                return(management.MessageSession);
            });
            container.AddSingleton(provider => new Lazy <IMessageSession>(provider.GetService <IMessageSession>));

            return(container);
        }
예제 #4
0
 public NServiceBusService(SessionAndConfigurationHolder holder)
 {
     this._logger = LogManager.GetLogger <NServiceBusService>();
     this.holder  = holder;
 }