如何使用带有WebAPI的ninject设置依赖项

时间:2014-09-22 13:07:27

标签: c# asp.net-mvc asp.net-web-api ninject

我使用Nuget安装了Ninject.Web.WebAPI包,但我无法弄清楚如何绑定我的依赖项。

我经常使用Ninject和我的MVC应用程序,当我安装Ninject包时,它会创建一个NinjectWebCommon.cs文件,我在其中设置我的绑定。此文件似乎不会为WebAPI项目生成。

任何人都可以帮我解决绑定的问题。

欢呼声

1 个答案:

答案 0 :(得分:1)

创建一个类来修复web api的NinjectDependencyResolver问题。

public class NinjectDependencyResolverHelper : IDependencyScope
    {
        IResolutionRoot resolver;
        public NinjectDependencyResolverHelper(IResolutionRoot resolver)
        {
            this.resolver = resolver;
        }
        public object GetService(Type serviceType)
        {
            if (resolver == null)
                throw new ObjectDisposedException("this", "This scope has been disposed");
            return resolver.TryGet(serviceType);
        }
        public System.Collections.Generic.IEnumerable<object> GetServices(Type serviceType)
        {
            if (resolver == null)
                throw new ObjectDisposedException("this", "This scope has been disposed");
            return resolver.GetAll(serviceType);
        }
        public void Dispose()
        {
            var disposable = resolver as IDisposable;
            if (disposable != null)
                disposable.Dispose();
            resolver = null;
        }
    }
    public class NinjectDependencyResolver : NinjectDependencyResolverHelper, IDependencyResolver
    {
        IKernel kernel;
        public NinjectDependencyResolver(IKernel kernel)
            : base(kernel)
        {
            this.kernel = kernel;
        }
        public IDependencyScope BeginScope()
        {
            return new NinjectDependencyResolverHelper(kernel.BeginBlock());
        }
    }

然后创建NinjectWebCommon类

public static class NinjectWebCommon 
{
    private static readonly Bootstrapper bootstrapper = new Bootstrapper();

    /// <summary>
    /// Starts the application
    /// </summary>
    public static void Start() 
    {
        DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
        DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
        bootstrapper.Initialize(CreateKernel);
    }

    /// <summary>
    /// Stops the application.
    /// </summary>
    public static void Stop()
    {
        bootstrapper.ShutDown();
    }

    /// <summary>
    /// Creates the kernel that will manage your application.
    /// </summary>
    /// <returns>The created kernel.</returns>
    private static IKernel CreateKernel()
    {
        var kernel = new StandardKernel();
        kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
        kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();

        GlobalConfiguration.Configuration.DependencyResolver = new NinjectDependencyResolver(kernel);

        RegisterServices(kernel);
        return kernel;
    }

    /// <summary>
    /// Load your modules or register your services here!
    /// </summary>
    /// <param name="kernel">The kernel.</param>
    private static void RegisterServices(IKernel kernel)
    {
        //Bind your stuff            
    }        
}

希望这有帮助。