Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions src/NServiceBus.AcceptanceTesting/Support/EndpointBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,23 @@ public EndpointBehavior(IEndpointConfigurationFactory endpointBuilder, int insta

var serviceKey = collectionAdapter.ServiceKey;

collectionAdapter.Inner.AddNServiceBusEndpoint(config, serviceKey);
// we don't want to expose the lifecycle in Core, but some super advanced scenarios require
// stopping the endpoint and this backdoor allows that without having to expose the lifecycle in Core
collectionAdapter.AddKeyedSingleton<Func<CancellationToken, Task>>("Stopper", (provider, key) =>
// Prevent concurrent access to AddNServiceBusEndpoint, which enumerates and modifies the service collection
configureSemaphore.Wait();
try
{
var endpointLifecycle = provider.GetRequiredKeyedService<IEndpointLifecycle>(serviceKey);
return async token => await endpointLifecycle.Stop(token).ConfigureAwait(false);
});
collectionAdapter.Inner.AddNServiceBusEndpoint(config, serviceKey);
// we don't want to expose the lifecycle in Core, but some super advanced scenarios require
// stopping the endpoint and this backdoor allows that without having to expose the lifecycle in Core
collectionAdapter.AddKeyedSingleton<Func<CancellationToken, Task>>("Stopper", (provider, key) =>
{
var endpointLifecycle = provider.GetRequiredKeyedService<IEndpointLifecycle>(serviceKey);
return async token => await endpointLifecycle.Stop(token).ConfigureAwait(false);
});
}
finally
{
configureSemaphore.Release();
}

return Task.FromResult(new StartableEndpointInstance(serviceKey));
}, static (startableEndpoint, provider, cancellationToken) => startableEndpoint.Start(provider, cancellationToken));
Expand Down Expand Up @@ -107,4 +116,6 @@ public async Task<ComponentRunner> CreateRunner(RunDescriptor run)

Func<IServiceCollection, EndpointConfiguration, Task<object>> createInstanceCallback;
Func<object, IServiceProvider, CancellationToken, Task<Func<CancellationToken, Task>>> startInstanceCallback;

static readonly SemaphoreSlim configureSemaphore = new SemaphoreSlim(1, 1);
}
Loading