Skip to content

Commit

Permalink
IContainerAdapter isn't needed
Browse files Browse the repository at this point in the history
  • Loading branch information
Hawxy committed Feb 1, 2019
1 parent 86501ed commit e1d141f
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 54 deletions.
Expand Up @@ -15,14 +15,14 @@ public class NetCoreServiceProviderActivationContext : IActivationContext
{
public IHandlerActivator CreateActivator(Action<IHandlerRegistry> handlerConfig, out IActivatedContainer container)
{
var services = new ServiceCollection().AddSingleton(p => new NetCoreServiceProviderContainerAdapter(p));
var services = new ServiceCollection().AddSingleton(p => new DependencyInjectionHandlerActivator(p));
handlerConfig.Invoke(new HandlerRegistry(services));

var provider = services.BuildServiceProvider();

container = new ActivatedContainer(provider);

return provider.GetRequiredService<NetCoreServiceProviderContainerAdapter>();
return provider.GetRequiredService<DependencyInjectionHandlerActivator>();
}

public IBus CreateBus(Action<IHandlerRegistry> handlerConfig, Func<RebusConfigurer, RebusConfigurer> configureBus, out IActivatedContainer container)
Expand Down
Expand Up @@ -4,7 +4,6 @@
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Rebus.Activation;
using Rebus.Bus;
using Rebus.Extensions;
using Rebus.Handlers;
using Rebus.Transport;
Expand All @@ -13,20 +12,18 @@
namespace Rebus.ServiceProvider
{
/// <summary>
/// Implementation of <see cref="IContainerAdapter"/> that is backed by a ServiceProvider
/// Implementation of <see cref="IHandlerActivator"/> that is backed by a ServiceProvider
/// </summary>
/// <seealso cref="IContainerAdapter" />
public class NetCoreServiceProviderContainerAdapter : IContainerAdapter, IDisposable
/// <seealso cref="IHandlerActivator" />
public class DependencyInjectionHandlerActivator : IHandlerActivator
{
readonly IServiceProvider _provider;

IBus _bus;

/// <summary>
/// Initializes a new instance of the <see cref="NetCoreServiceProviderContainerAdapter"/> class.
/// Initializes a new instance of the <see cref="DependencyInjectionHandlerActivator"/> class.
/// </summary>
/// <param name="provider">The service provider used to yield handler instances.</param>
public NetCoreServiceProviderContainerAdapter(IServiceProvider provider)
public DependencyInjectionHandlerActivator(IServiceProvider provider)
{
_provider = provider ?? throw new ArgumentNullException(nameof(provider));
}
Expand All @@ -45,22 +42,7 @@ public Task<IEnumerable<IHandleMessages<TMessage>>> GetHandlers<TMessage>(TMessa

return Task.FromResult((IEnumerable<IHandleMessages<TMessage>>)resolvedHandlerInstances.ToArray());
}

/// <summary>
/// Sets the bus instance associated with this <see cref="T:Rebus.Activation.IContainerAdapter" />.
/// </summary>
/// <param name="bus"></param>
/// <exception cref="ArgumentNullException"></exception>
public void SetBus(IBus bus)
{
if (_bus != null)
{
throw new InvalidOperationException("Cannot set the bus instance more than once on the container adapter.");
}

_bus = bus ?? throw new ArgumentNullException(nameof(bus));
}


List<IHandleMessages<TMessage>> GetMessageHandlersForMessage<TMessage>(IServiceScope scope)
{
var handledMessageTypes = typeof(TMessage).GetBaseTypes()
Expand All @@ -76,31 +58,5 @@ List<IHandleMessages<TMessage>> GetMessageHandlersForMessage<TMessage>(IServiceS
.Cast<IHandleMessages<TMessage>>()
.ToList();
}

bool _disposed;

/// <summary>
/// Disposes of the bus.
/// </summary>
/// <param name="disposing"></param>
protected virtual void Dispose(bool disposing)
{
if (_disposed) return;

if (disposing)
{
_bus?.Dispose();
}

_disposed = true;
}

/// <summary>
/// Disposes of the bus.
/// </summary>
public void Dispose()
{
Dispose(true);
}
}
}
4 changes: 2 additions & 2 deletions Rebus.ServiceProvider/ServiceCollectionExtensions.Bus.cs
Expand Up @@ -43,10 +43,10 @@ public static IServiceCollection AddRebus(this IServiceCollection services, Func
services.AddTransient(s => s.GetService<IBus>().Advanced.SyncBus);

// Register the Rebus Bus instance, to be created when it is first requested.
services.AddSingleton(provider => new NetCoreServiceProviderContainerAdapter(provider));
services.AddSingleton(provider => new DependencyInjectionHandlerActivator(provider));
services.AddSingleton(provider =>
{
var configurer = Configure.With(provider.GetRequiredService<NetCoreServiceProviderContainerAdapter>());
var configurer = Configure.With(provider.GetRequiredService<DependencyInjectionHandlerActivator>());
configureRebus(configurer, provider);
return configurer.Start();
Expand Down

0 comments on commit e1d141f

Please sign in to comment.