ninject.web.mvc

How to manually instantiate objects using Ninject for MVC 3

笑着哭i 提交于 2019-12-06 00:13:00
How is it possible to use Ninject inside ASP.NET MVC 3 to instantiate objects manually? Something as "NinjectObject".Resolve<IMyService>(); Thank you & regards It is better to inject dependencies instead of resolving them. Service Locator is an anti-pattern. You could for example use the following: IMyService myService = DependencyResolver.Current.GetService<IMyService>(); But please do not use it. That's an anti-pattern. Dependency injection is the preferred way. You should have the constructor of the class that needs this dependency take an IMyService instead of having the class fetch this

Ninject for Web Site and Api - Sequence contains no elements

这一生的挚爱 提交于 2019-12-05 22:36:07
I have a single VS2010 solution with a Web.Api project and an MVC 3 project. Both the Web.APi and the MVC project have their own App_Start with NinjectWebCommon in and their own bindings declared in there. When trying to use the Api I always get the following message: Sequence contains no elements Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.InvalidOperationException: Sequence contains no elements Source Error: Line

Ninject, injecting Membership.Provider in RegisterServices of ninject initialization

Deadly 提交于 2019-12-05 21:27:23
Anybody know how to configure Membership.Provider in RegisterServices of ninject initialization code? In my code: /// <summary> /// Load your modules or register your services here! /// </summary> /// <param name="kernel">The kernel.</param> private static void RegisterServices(IKernel kernel) { // Put additional bindings here kernel.Bind<IDatabaseFactory>().To<DatabaseFactory>(); kernel.Bind<IUnitOfWork>().To<UnitOfWork>(); kernel.Bind<IUserRepository>().To<UserRepository>(); kernel.Bind<IRoleRepository>().To<RoleRepository>(); kernel.Bind<ISecurityService>().To<SecurityService>(); kernel

Parameterized Factories Using Ninject

风流意气都作罢 提交于 2019-12-04 15:59:23
问题 How to make Ninject to instantiate object based on variable on run time?. I am trying to inject the correct Repository in The Controller action - MVC 3 - based on parameter come from user input. If user input "BMW" it would bind ICarRepository to BMWRepository , and if he input "KIA" KiaRepository will be injected. [HttpPost] public ActionResult SearchResult(FormCollection values) { string carModel = values["model"]; ICarRepository myRepository = RepositoryFactory.getRepository(carModel); ...

Problem with Ninject and MVC3 Dependency injection action filter on Controller and Action

天涯浪子 提交于 2019-12-04 14:21:52
Recently I decided to remove a heap of action level filters in a controller and replace them with a single controller level filter. Now I'm getting this error message. Error activating LogActionFilter More than one matching bindings are available. Activation path: 1) Request for LogActionFilter Suggestions: 1) Ensure that you have defined a binding for LogActionFilter only once. I'm sure the error is related to action filter being bound twice, as that's what I've changed. However, when I view the documentation here I can see it specifies/does the same. So I'm really not sure what I'm doing

Cannot Inject Dependencies using Ninject into ASP.NET Web API Controller called from Angular Service

三世轮回 提交于 2019-12-04 14:03:27
I am using Ninject together with ASP.NET MVC 4. I am using repositories and want to do constructor injection to pass in the repository to one of the controllers. Here is my context object (EntityFramework) that implements my StatTracker interface: public class StatTrackerRepository : IStatTrackerRepository { private GolfStatTrackerEntities _ctx; public StatTrackerRepository(GolfStatTrackerEntities ctx) { _ctx = ctx; } public IQueryable<Facility> GetFacilites() { return _ctx.Facilities; } } This is my Repository interface: public interface IStatTrackerRepository { IQueryable<Facility>

No matching bindings are available, and the type is not self-bindable in Ninject

北战南征 提交于 2019-12-04 03:08:48
I am using Ninjec, Ninject.Web.MVC and Ninject.Web.Common When I start my mvc application I get this binding error: What do I wrong in my binding? Error activating DbConnection No matching bindings are available, and the type is not self-bindable. Activation path: 4) Injection of dependency DbConnection into parameter existingConnection of constructor of type DbContext 3) Injection of dependency DbContext into parameter dbContext of constructor of type GenericRepository{User} 2) Injection of dependency IGenericRepository{User} into parameter repo of constructor of type HomeController 1)

Parameterized Factories Using Ninject

匆匆过客 提交于 2019-12-03 09:10:35
How to make Ninject to instantiate object based on variable on run time?. I am trying to inject the correct Repository in The Controller action - MVC 3 - based on parameter come from user input. If user input "BMW" it would bind ICarRepository to BMWRepository , and if he input "KIA" KiaRepository will be injected. [HttpPost] public ActionResult SearchResult(FormCollection values) { string carModel = values["model"]; ICarRepository myRepository = RepositoryFactory.getRepository(carModel); ..... } This is known by switch/case noob instantiation or Parameterized Factories, and i know how to do

Ninject UnitOfWork confusion

百般思念 提交于 2019-12-02 08:46:09
I use Ninject all the time with my MVC 3 applications, but I'm trying to change the Pattern for my Data Objects to use UnitOfWork and I'm having trouble figuring out how to get Ninject to handle this properly. I know my implementation of classes work when they are constructed manually like this in my console application: IDatabaseFactory factory = new DatabaseFactory(); IUnitOfWork worker = new UnitOfWork(factory); IBlogCategoryDao dao = new BlogCategoryDao(factory); IBlogCategoryService service = new BlogCategoryService(dao); BlogCategory category = service.GetById(id); try { if (category !=

Ninject: entity object cannot be referenced by multiple instances of IEntityChangeTracker

我只是一个虾纸丫 提交于 2019-12-02 07:21:20
I am starting to use Ninject in my MVC5 code-first app. Here's my NinjectWebCommon.cs: private static IKernel CreateKernel() { var kernel = new StandardKernel(); try { kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel); kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>(); kernel.Bind<CMSContext>() .ToSelf() //.InSingletonScope(); .InRequestScope(); kernel.Bind<IExecutiveRepository>() .To<ExecutiveRepository>(); kernel.Bind<IExecutiveSectionRepository>() .To<ExecutiveSectionRepository>(); kernel.Bind<IExecutiveSectionMappingRepository>() .To