Skip to content

Commit

Permalink
Switch to properties
Browse files Browse the repository at this point in the history
  • Loading branch information
danielmarbach committed Aug 6, 2020
1 parent dd51d94 commit 37fc2b2
Showing 1 changed file with 13 additions and 19 deletions.
32 changes: 13 additions & 19 deletions src/NServiceBus.Extensions.Hosting/HostBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
namespace NServiceBus
{
using System;
using System.Linq;
using Extensions.Hosting;
using Logging;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -23,33 +22,28 @@ public static IHostBuilder UseNServiceBus(this IHostBuilder hostBuilder, Func<Ho

hostBuilder.ConfigureServices((ctx, serviceCollection) =>
{
var endpointConfiguration = endpointConfigurationBuilder(ctx);
var startableEndpoint = EndpointWithExternallyManagedContainer.Create(endpointConfiguration, new ServiceCollectionAdapter(serviceCollection));
if (serviceCollection.Any(x =>
x.ServiceType == typeof(IHostedService) &&
x.ImplementationFactory != null &&
x.ImplementationFactory.Method.ReturnType == typeof(NServiceBusHostedService) &&
x.Lifetime == ServiceLifetime.Singleton))
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.");
}
serviceCollection.AddSingleton(_ => startableEndpoint.MessageSession.Value);
serviceCollection.AddSingleton<IHostedService, NServiceBusHostedService>(Factory);
ctx.Properties.Add(HostBuilderExtensionInUse, null);
NServiceBusHostedService Factory(IServiceProvider serviceProvider)
{
return new NServiceBusHostedService(
startableEndpoint,
serviceProvider,
serviceProvider.GetRequiredService<ILoggerFactory>(),
deferredLoggerFactory);
}
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,
serviceProvider.GetRequiredService<ILoggerFactory>(),
deferredLoggerFactory));
});

return hostBuilder;
}

const string HostBuilderExtensionInUse = "NServiceBus.Extension.Hosting.UseNServiceBus";
}
}

0 comments on commit 37fc2b2

Please sign in to comment.