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

本文关键字:ninject 设置 依赖 关系 WebAPI 何使用 | 更新日期: 2023-09-27 18:23:50

我已经使用Nuget安装了Ninject.Web.WebAPI包,但我不知道如何绑定我的依赖项。

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

任何人都可以帮我把我的捆绑包放在哪里。

欢呼

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

创建一个类来修复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            
    }        
}

希望这能有所帮助。