Question

In this question I used SignalR.EventAggregatorProxy as an Event Aggregation proxy for SignalR.

So problem I have it only for non Ninject users, so basically all constraints are ignored (and no any errors or something).

I tried to change MVC4 example to use SimpleInjector or without using IoC at all and no success - basic logic works but constraints simply ignored.

Try 1:

So very basic try was just reusing same instances of SignalR.EventAggregatorProxy.EventAggregation.IEventAggregator and IEventAggregator

public class App
{
    private static Lazy<SignalR.EventAggregatorProxy.EventAggregation.IEventAggregator> _eventAggregator;
    private static Lazy<IEventAggregator> _eventAggregatorProxy;

    public static SignalR.EventAggregatorProxy.EventAggregation.IEventAggregator EventAggregatorOrAnotherProxyWhatever
    {
        get
        {
            if (_eventAggregator == null)
                _eventAggregator = new Lazy<SignalR.EventAggregatorProxy.EventAggregation.IEventAggregator>(() => new EventAggregatorProxy(EventAggregatorProxy));

            return _eventAggregator.Value;
        }
    }

    public static IEventAggregator EventAggregatorProxy
    {
        get
        {
            if (_eventAggregatorProxy == null)
                _eventAggregatorProxy = new Lazy<IEventAggregator>(() => new EventAggregator());

            return _eventAggregatorProxy.Value;
        }
    }
}

And in Application_Start: GlobalHost.DependencyResolver.Register(typeof(SignalR.EventAggregatorProxy.EventAggregation.IEventAggregator), () => App.EventAggregatorOrAnotherProxyWhatever);

Try 2:

So there I tried with SimpleInjector. I created my own dependency resolver:

public class SignalRSimpleInjectorDependencyResolver : DefaultDependencyResolver
{
    private readonly Container _container;

    public SignalRSimpleInjectorDependencyResolver(Container container) { _container = container; }

    public override object GetService(Type serviceType)
    {
        if (serviceType.IsAssignableFrom(typeof (Caliburn.Micro.IEventAggregator)) ||
            serviceType.IsAssignableFrom(typeof (SignalR.EventAggregatorProxy.EventAggregation.IEventAggregator)))
            return _container.GetInstance(serviceType);

        return base.GetService(serviceType);
    }

    public override IEnumerable<object> GetServices(Type serviceType)
    {
        IEnumerable<object> enumerable = base.GetServices(serviceType);
        if (serviceType.IsAssignableFrom(typeof (Caliburn.Micro.IEventAggregator)) ||
            serviceType.IsAssignableFrom(typeof (SignalR.EventAggregatorProxy.EventAggregation.IEventAggregator)))
            enumerable = enumerable.Concat(_container.GetAllInstances(serviceType));

        return enumerable;
    }
}

And in Application_Start I registered it like so:

        var container = new Container();

        container.RegisterSingle<Caliburn.Micro.IEventAggregator, Caliburn.Micro.EventAggregator>();
        container.RegisterSingle<SignalR.EventAggregatorProxy.EventAggregation.IEventAggregator, EventAggregatorProxy>();

        GlobalHost.DependencyResolver = new SignalRSimpleInjectorDependencyResolver(container);
        DependencyResolver.SetResolver(new SimpleInjectorDependencyResolver(container));

Also I tried to use default resolver for SignalR like so:

GlobalHost.DependencyResolver.Register(typeof(SignalR.EventAggregatorProxy.EventAggregation.IEventAggregator), container.GetInstance<SignalR.EventAggregatorProxy.EventAggregation.IEventAggregator>);

Nothing works. Can you please point we what I am doing wrong there is it isn't a bug?

Was it helpful?

Solution

The library will ask the Dependency resolver for the constraint handler instance. Ninject can create instances of unregistered types if they are concrete types. The built in Dependency resolver is missing this.

So you need todo this manually, like

 var constraint = new Lazy<ConstrainedEventConstraintHandler>(() => new ConstrainedEventConstraintHandler());
            GlobalHost.DependencyResolver.Register(typeof(ConstrainedEventConstraintHandler), () => constraint.Value);

See this branch for full demo https://github.com/AndersMalmgren/SignalR.EventAggregatorProxy/tree/NoNinjectDemo

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top