Skip to content

Commit

Permalink
fix disposal of IBus
Browse files Browse the repository at this point in the history
  • Loading branch information
mookid8000 committed May 31, 2017
1 parent eba0c0b commit d669a99
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 17 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@

* Update to Rebus 3

## 4.0.0-b03
## 4.0.0-b04

* Update to Rebus 4
* Add .NET Core support (netstandard1.6)
* Update deps to b10
* Update deps to b10
* Circumvent SimpleInjector's reluctance to dispose singletons that have not been resolved
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Rebus.Activation;
using Rebus.Bus;
using Rebus.Extensions;
Expand Down Expand Up @@ -41,7 +40,7 @@ public IHandlerActivator GetActivator()

var containerAdapter = new SimpleInjectorContainerAdapter(_container);

_disposables.Add(containerAdapter);
_disposables.Add(_container);

return containerAdapter;
}
Expand All @@ -65,7 +64,8 @@ public IBus GetBus()
static IEnumerable<Type> GetHandlerInterfaces(Type handlerType)
{
#if NETSTANDARD1_6
return handlerType.GetTypeInfo().GetInterfaces().Where(i => i.GetTypeInfo().IsGenericType && i.GetGenericTypeDefinition() == typeof(IHandleMessages<>));
return System.Reflection.IntrospectionExtensions.GetTypeInfo(handlerType)
.GetInterfaces().Where(i => System.Reflection.IntrospectionExtensions.GetTypeInfo(i).IsGenericType && i.GetGenericTypeDefinition() == typeof(IHandleMessages<>));
#else
return handlerType.GetInterfaces().Where(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IHandleMessages<>));
#endif
Expand Down
27 changes: 15 additions & 12 deletions Rebus.SimpleInjector/SimpleInjectorContainerAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ namespace Rebus.SimpleInjector
/// <summary>
/// Implementation of <see cref="IContainerAdapter"/> that uses Simple Injector to do its thing
/// </summary>
public class SimpleInjectorContainerAdapter : IContainerAdapter, IDisposable
public class SimpleInjectorContainerAdapter : IContainerAdapter
{
readonly Container _container;
IBus _bus;

/// <summary>
/// Constructs the container adapter
Expand Down Expand Up @@ -76,9 +75,8 @@ public void SetBus(IBus bus)
throw new InvalidOperationException($"Cannot register IBus in the container because it has already been registered. If you want to host multiple Rebus instances in a single process, please use separate container instances for them.");
}

_container.RegisterSingleton(bus);
_bus = bus;

_container.RegisterSingleton(() => bus);

_container.Register(() =>
{
if (_container.IsVerifying)
Expand All @@ -105,6 +103,11 @@ public void SetBus(IBus bus)
throw new InvalidOperationException("Attempted to inject the current message context from MessageContext.Current, but it was null! Did you attempt to resolve IMessageContext from outside of a Rebus message handler?");
});

// cheat and activate the IBus singleton behind the scenes, thus ensuring that the container will dispose it when it is time
var registration = _container.GetRegistration(typeof(IBus));

registration.GetInstance();
}

class FakeSyncBus : ISyncBus
Expand Down Expand Up @@ -167,12 +170,12 @@ class FakeMessageContext : IMessageContext
public Dictionary<string, string> Headers { get; }
}

/// <summary>
/// Disposes the bus
/// </summary>
public void Dispose()
{
_bus?.Dispose();
}
///// <summary>
///// Disposes the bus
///// </summary>
//public void Dispose()
//{
// _bus?.Dispose();
//}
}
}

0 comments on commit d669a99

Please sign in to comment.