我的项目有常规的Asp.Net MVC控制器和Web Api控制器。现在,Ninject使用Web Api,但Ninject不工作与常规Asp.MVC控制器。
我的常规MVC控制器实现;
public class GalleryController : BaseController { public GalleryController(IUow uow) { Uow = uow; } ........ }
与常规控制器一起使用时出错
An error occurred when trying to create a controller of type 'Web.Controllers.HomeController'. Make sure that the controller has a parameterless public constructor.]
但是,当我尝试与Web Api相同的代码,它的工作原理
public class GalleryController : BaseApiController { public GalleryController(IUow uow) { Uow = uow; } ...... }
我的界面保存差异存储库(工厂模式)
public interface IUow { // Save pending changes to the data store. void Commit(); //Repositoryries IRepository<Gallery> Gallery { get; } IMenuRepository Menus { get; } }
NinjectDependencyScope类;
public class NinjectDependencyScope : IDependencyScope { private IResolutionRoot resolver; internal NinjectDependencyScope(IResolutionRoot resolver) { Contract.Assert(resolver != null); this.resolver = resolver; } public void Dispose() { var disposable = resolver as IDisposable; if (disposable != null) disposable.Dispose(); resolver = null; } public object GetService(Type serviceType) { if (resolver == null) throw new ObjectDisposedException("this","This scope has already been disposed"); return resolver.TryGet(serviceType); } public IEnumerable<object> GetServices(Type serviceType) { if (resolver == null) throw new ObjectDisposedException("this","This scope has already been disposed"); return resolver.GetAll(serviceType); } }
NinjectDependencyResolver类;
public class NinjectDependencyResolver : NinjectDependencyScope,IDependencyResolver { private IKernel kernel; public NinjectDependencyResolver(IKernel kernel) : base(kernel) { this.kernel = kernel; } public IDependencyScope BeginScope() { return new NinjectDependencyScope(kernel.BeginBlock()); } }
Global.asax的Ninject配置;
public class IocConfig { public static void RegisterIoc(HttpConfiguration config) { var kernel = new StandardKernel(); // Ninject IoC //kernel.Load(Assembly.GetExecutingAssembly()); //only required for asp.net mvc (not for webapi) // These registrations are "per instance request". // See http://blog.bobcravens.com/2010/03/ninject-life-cycle-management-or-scoping/ kernel.Bind<RepositoryFactories>().To<RepositoryFactories>() .InSingletonScope(); kernel.Bind<IRepositoryProvider>().To<RepositoryProvider>(); kernel.Bind<IUow>().To<Uow>(); // Tell WebApi how to use our Ninject IoC config.DependencyResolver = new NinjectDependencyResolver(kernel); } }
Global.asax
protected void Application_Start() { // Tell WebApi to use our custom Ioc (Ninject) IocConfig.RegisterIoc(GlobalConfiguration.Configuration); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); GlobalConfig.CustomizeConfig(GlobalConfiguration.Configuration); AreaRegistration.RegisterAllAreas(); }
解决方法
> https://gist.github.com/odytrice/5821087(for MVC)
> https://gist.github.com/odytrice/5842010(适用于WebApi)
要为具体类型添加绑定,只需将它们放在MainModule的Load()方法中。您可以创建尽可能多的模块,以保持绑定的组织。但您还必须将它们添加到Modules属性中返回的数组。
> NinjectContainer.RegisterModules(NinjectModules.Modules)(for MVC)
> NinjectHttpContainer.RegisterModules(NinjectHttpModules.Modules)(适用于WebApi)
注意,您可以为MVC和WebApi注册使用相同的NinjectModules.Modules。我只是分开它为了清楚
更新:记住从您的项目中删除NinjectWebCommon.cs,因为它加载和引导一个新的内核在运行时,不幸的是只有为MVC。
更新:你也可以使用
> NinjectContainer.RegisterAssembly()(for MVC)
> NinjectHttpContainer.RegisterAssembly()(用于WebApi)
这将扫描所有模块的当前装配。这样,您可以将模块放置在项目的任何位置,它将被注册