-
-
Save zpisgod/8065987 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
using System.Reflection; | |
using System.Collections.Generic; | |
using Funq; | |
using Microsoft.AspNet.SignalR; | |
namespace Foo | |
{ | |
public class FunqDependencyResolver : DefaultDependencyResolver | |
{ | |
private readonly MethodInfo _tryResolveMethod; | |
private Container _container; | |
public FunqDependencyResolver(Container container) | |
{ | |
_container = container; | |
FunqDependencyResolver existingResolver = container.TryResolve<IDependencyResolver>() as FunqDependencyResolver; | |
if (existingResolver != null) | |
{ | |
existingResolver._container = container; | |
} | |
else | |
{ | |
container.Register<IDependencyResolver>(c => this); | |
} | |
container.RegisterAutoWired<BroadcastHub>();//NOTE: register auto wired to have funq IOC inject in it | |
_tryResolveMethod = container | |
.GetType() | |
.GetMethods() | |
.First(m => m.Name == "TryResolve" && m.IsGenericMethodDefinition && m.GetGenericArguments().Length == 1); | |
} | |
public override object GetService(Type serviceType) | |
{ | |
var instance = _tryResolveMethod.MakeGenericMethod(serviceType).Invoke(_container, null); | |
if (instance != null) | |
{ | |
return instance; | |
} | |
return base.GetService(serviceType); | |
} | |
public override IEnumerable<object> GetServices(Type serviceType) | |
{ | |
var instance = _tryResolveMethod.MakeGenericMethod(typeof(IEnumerable<>).MakeGenericType(serviceType)).Invoke(_container, null); | |
if (instance != null) | |
{ | |
return (IEnumerable<object>)instance; | |
} | |
return base.GetServices(serviceType) ?? Enumerable.Empty<object>(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment