Exemplo n.º 1
0
        /// <summary>
        /// Creates a new startable endpoint based on the provided configuration that uses an externally managed container implementing the Microsoft dependency injection abstraction.
        /// </summary>
        /// <param name="endpointConfiguration">The endpoint configuration.</param>
        /// <param name="serviceCollection">Service collection.</param>
        public static IStartableEndpointWithExternallyManagedContainer Create(EndpointConfiguration endpointConfiguration, IServiceCollection serviceCollection)
        {
            Guard.AgainstNull(nameof(endpointConfiguration), endpointConfiguration);
            Guard.AgainstNull(nameof(serviceCollection), serviceCollection);

            return(EndpointWithExternallyManagedContainer.Create(endpointConfiguration, new ServiceCollectionAdapter(serviceCollection)));
        }
        /// <summary>
        /// Configures the host to start an NServiceBus endpoint.
        /// </summary>
        public static IHostBuilder UseNServiceBus(this IHostBuilder hostBuilder, Func <HostBuilderContext, EndpointConfiguration> endpointConfigurationBuilder)
        {
            var deferredLoggerFactory = new DeferredLoggerFactory();

            LogManager.UseFactory(deferredLoggerFactory);

            hostBuilder.ConfigureServices((ctx, serviceCollection) =>
            {
                if (ctx.Properties.ContainsKey(HostBuilderExtensionInUse))
                {
                    throw new InvalidOperationException(
                        "`UseNServiceBus` can only be used once on the same host instance because subsequent calls would override each other. For multi-endpoint hosting scenarios consult our documentation page.");
                }

                ctx.Properties.Add(HostBuilderExtensionInUse, null);

                var endpointConfiguration = endpointConfigurationBuilder(ctx);
                var startableEndpoint     = EndpointWithExternallyManagedContainer.Create(endpointConfiguration, serviceCollection);

                serviceCollection.AddSingleton(_ => new HostAwareMessageSession(startableEndpoint.MessageSession));
                serviceCollection.AddSingleton <IMessageSession>(serviceProvider => serviceProvider.GetService <HostAwareMessageSession>());
                serviceCollection.AddSingleton <IHostedService>(serviceProvider => new NServiceBusHostedService(
                                                                    startableEndpoint,
                                                                    serviceProvider,
                                                                    serviceProvider.GetRequiredService <ILoggerFactory>(),
                                                                    deferredLoggerFactory,
                                                                    serviceProvider.GetRequiredService <HostAwareMessageSession>()));
            });

            return(hostBuilder);
        }
        /// <summary>
        /// Configures the host to start an NServiceBus endpoint.
        /// </summary>
        public static IHostBuilder UseNServiceBus(this IHostBuilder hostBuilder, Func <HostBuilderContext, EndpointConfiguration> endpointConfigurationBuilder)
        {
            hostBuilder.ConfigureServices((ctx, serviceCollection) =>
            {
                var endpointConfiguration = endpointConfigurationBuilder(ctx);
                var startableEndpoint     = EndpointWithExternallyManagedContainer.Create(endpointConfiguration, new ServiceCollectionAdapter(serviceCollection));

                serviceCollection.AddSingleton(_ => startableEndpoint.MessageSession.Value);
                serviceCollection.AddSingleton <IHostedService>(serviceProvider => new NServiceBusHostedService(startableEndpoint, serviceProvider));
            });

            return(hostBuilder);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Adds NServiceBus, IMessageSession, and related services to the IServiceCollection.
        /// </summary>
        /// <remarks>
        /// Use this extension method with WebHost only.
        /// </remarks>
        public static IServiceCollection AddNServiceBus(this IServiceCollection services, EndpointConfiguration configuration)
        {
            var startableEndpoint = EndpointWithExternallyManagedContainer.Create(configuration, new ServiceCollectionAdapter(services));
            var hostedService     = new NServiceBusHostedService(startableEndpoint);
            var webHostCompatibleMessageSession = new WebHostCompatibleMessageSession(hostedService);

            services.AddSingleton <IMessageSession>(webHostCompatibleMessageSession);
            services.AddSingleton <IHostedService>(serviceProvider =>
            {
                hostedService.UseServiceProvider(serviceProvider);
                return(hostedService);
            });

            return(services);
        }
        internal static IStartableEndpointWithExternallyManagedContainer Configure(
            EndpointConfiguration endpointConfiguration,
            IServiceCollection serviceCollection,
            string appDirectory = null)
        {
            var scanner = endpointConfiguration.AssemblyScanner();

            if (appDirectory != null)
            {
                scanner.AdditionalAssemblyScanningPath = appDirectory;
            }

            scanner.ExcludeAssemblies(InProcessFunctionEndpoint.AssembliesToExcludeFromScanning);

            return(EndpointWithExternallyManagedContainer.Create(
                       endpointConfiguration,
                       serviceCollection));
        }
Exemplo n.º 6
0
        /// <summary>
        /// Configures the host to start an NServiceBus endpoint.
        /// </summary>
        public static IHostBuilder UseNServiceBus(
            this IHostBuilder hostBuilder,
            Func <HostBuilderContext, EndpointConfiguration> endpointConfigurationBuilder,
            Action <IServiceCollection, IConfiguration> configureServicesForHandlers = null)
        {
            hostBuilder.ConfigureServices((ctx, serviceCollection) =>
            {
                var newServiceCollection = new ServiceCollection();
                configureServicesForHandlers?.Invoke(newServiceCollection, ctx.Configuration);

                var endpointConfiguration = endpointConfigurationBuilder(ctx);
                var startableEndpoint     = EndpointWithExternallyManagedContainer.Create(endpointConfiguration, new ServiceCollectionAdapter(newServiceCollection));

                newServiceCollection.AddSingleton(_ => startableEndpoint.MessageSession.Value);
                serviceCollection.AddSingleton(_ => startableEndpoint.MessageSession.Value);
                serviceCollection.AddTransient <IHostedService>(_ => new NServiceBusHostedService(startableEndpoint, newServiceCollection.BuildServiceProvider()));
            });

            return(hostBuilder);
        }