Created
November 4, 2015 10:39
-
-
Save hmdhk/639b1dfab51e0b6df6ec to your computer and use it in GitHub Desktop.
A ninject dependency resolver for SignalR and Asp.net Web api
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.Collections.Generic; | |
using System.Linq; | |
using FluentNHibernate.Conventions; | |
using Ninject; | |
namespace Ninject | |
{ | |
public class NinjectDependencyResolver : Microsoft.AspNet.SignalR.DefaultDependencyResolver, | |
System.Web.Http.Dependencies.IDependencyResolver | |
{ | |
public readonly IKernel Kernel; | |
public NinjectDependencyResolver(string moduleFilePattern) | |
: base() | |
{ | |
Kernel = new StandardKernel(); | |
Kernel.Load(moduleFilePattern); | |
} | |
public override object GetService(Type serviceType) | |
{ | |
//todo: should clean up this part when signalr release fixed this issue [Consider splitting service location from DI](https://github.com/SignalR/SignalR/issues/289) | |
var service = Kernel.TryGet(serviceType) ?? base.GetService(serviceType); | |
return service; | |
} | |
public override IEnumerable<object> GetServices(Type serviceType) | |
{ | |
//todo: should clean up this part when signalr release fixed this issue [Consider splitting service location from DI](https://github.com/SignalR/SignalR/issues/289) | |
IEnumerable<object> services = Kernel.GetAll(serviceType).ToList(); | |
if (services.IsEmpty()) | |
{ | |
services = base.GetServices(serviceType) ?? services; | |
} | |
return services; | |
} | |
public System.Web.Http.Dependencies.IDependencyScope BeginScope() | |
{ | |
return this; | |
} | |
public void Dispose() | |
{ } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment