Skip to content

Instantly share code, notes, and snippets.

@hmdhk
Created November 4, 2015 10:39
Show Gist options
  • Save hmdhk/639b1dfab51e0b6df6ec to your computer and use it in GitHub Desktop.
Save hmdhk/639b1dfab51e0b6df6ec to your computer and use it in GitHub Desktop.
A ninject dependency resolver for SignalR and Asp.net Web api
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