ninject.web.mvc

Share a Kernel between WebAPI and MVC

為{幸葍}努か 提交于 2019-11-30 08:50:30
问题 I've got a simple MVC4 site which uses ASP.NET webAPI and also MVC pages. I'd like to use Ninject DI for both controller types but I'm wondering how this can be done? I've got Ninject DI working for WebAPI, but now not sure how to share the same Kernel elegantly. Should I be using some sort of Kernel singleton which both Dependency Resolvers can refer to? Anyone had any experience with this? Cheers. 回答1: You should use the same IKernel instance for a single application-level composition root,

Ninject w/ ASMX web service in a MVC3/Ninject 3 environment

纵饮孤独 提交于 2019-11-29 14:47:28
I'm looking for the best way to incorporate a classic asmx web service into my MVC3 environment. I'd like the kernel/container to be shared between the two. In the past with ASP.NET web forms I was able to use the following: [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.Web.Script.Services.ScriptService] public class Service : WebServiceBase { [Inject] public IContextProvider ContextProvider { get; set; } ... This used the older Ninject.Web to shared the kernel using the KernelContainer that was shared between WebServiceBase, PageBase, etc. etc. Now I've got a MVC3

Share a Kernel between WebAPI and MVC

六眼飞鱼酱① 提交于 2019-11-29 08:12:53
I've got a simple MVC4 site which uses ASP.NET webAPI and also MVC pages. I'd like to use Ninject DI for both controller types but I'm wondering how this can be done? I've got Ninject DI working for WebAPI, but now not sure how to share the same Kernel elegantly. Should I be using some sort of Kernel singleton which both Dependency Resolvers can refer to? Anyone had any experience with this? Cheers. You should use the same IKernel instance for a single application-level composition root, may be WebApi or MVC controllers. If you are using Ninject.MVC3 package: When the kernel is initialized in

How to configure Ninject so that it creates a single instance per Controller

筅森魡賤 提交于 2019-11-28 13:58:12
I'm trying to use Ninject for an MVC5 application and I want to know if there's any way I can configure it so that instantiate a single object per Controller (I'm not sure if it the same as per request), use that instance in any Action of the controller and once the Action has finished release all the resources that the object utilized. The class that I want Ninject to inject in my controllers implements the IDisposable interface. So far I have tried this: private static void RegisterServices(IKernel kernel) { kernel.Bind<IUnitOfWork>().To<SqlUnitOfWork>(); } But the Dispose method never gets

Batch registering all implementations of a generic interface with Ninject

a 夏天 提交于 2019-11-28 12:52:42
i have the following interfaces injected in Castle Windsor. how do i do the same in Ninject? container.Register( AllTypes.FromAssemblyNamed("Apps.Web") .BasedOn(typeof(ICommandHandler<>)) .WithService.FirstInterface()); i've tried: this.Bind(x => x.FromAssembliesMatching("Apps.Web.dll") .Select(y => y.Namespace.EndsWith("Handlers")) .BindSingleInterface()); but getting Object reference not set to an instance of an object error. You can use Ninject's convention binding extensons (install it from NuGet ) to do this. Something like the following should work kernel.Bind(x => x

Ninject Conditional Self bind to change scope (For Task-scheduler) not working properly?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-28 08:44:16
问题 Within MVC Web Application DbContext binding work properly with InRequestScope() kernel.Bind<DbContext>().ToSelf().InRequestScope(); kernel.Bind<IUnitOfWork<DbContext>>().To<UnitOfWork<DbContext>>(); But from a Task Scheduler call DbContext in InRequestScope() unable to update Db Table ( without any error ), until I change Binding to InSingletonScope() OR InThreadScope() Question: So is their any way change scope to InSingletonScope() / InThreadScope() for a Task Scheduler Call. ? // For Task

Ninject w/ ASMX web service in a MVC3/Ninject 3 environment

帅比萌擦擦* 提交于 2019-11-28 08:41:39
问题 I'm looking for the best way to incorporate a classic asmx web service into my MVC3 environment. I'd like the kernel/container to be shared between the two. In the past with ASP.NET web forms I was able to use the following: [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.Web.Script.Services.ScriptService] public class Service : WebServiceBase { [Inject] public IContextProvider ContextProvider { get; set; } ... This used the older Ninject.Web to shared the kernel using

NinjectDependencyResolver fails binding ModelValidatorProvider

落爺英雄遲暮 提交于 2019-11-28 06:49:53
I'm developing an ASP.NET Web Api 2.2 with C#, .NET Framework 4.5.1. After updating my Web.Api to Ninject 3.2.0 I get this error: Error activating ModelValidatorProvider using binding from ModelValidatorProvider to NinjectDefaultModelValidatorProvider A cyclical dependency was detected between the constructors of two services. Activation path: 3) Injection of dependency ModelValidatorProvider into parameter defaultModelValidatorProviders of constructor of type DefaultModelValidatorProviders 2) Injection of dependency DefaultModelValidatorProviders into parameter defaultModelValidatorProviders

Can a Ninject binding be based on a URL/route value?

江枫思渺然 提交于 2019-11-28 00:23:37
I have a single controller that I want to use for CRUD operations on two different entities which implement the same interface. I'd like for Ninject to give it a different repository based on a query string value in the URL (or maybe a different URL, routed to the same controller). Is this possible? How can I do it? That's usually a design smell but you could define the binding like this: kernel.Bind<IRepo>().ToMethod(ctx => { var a = HttpContext.Current.Request["a"]; if (a == "b") { return new RepoA(); } return new RepoB(); }).InRequestScope(); The following worked for me, Getting A Specific

Batch registering all implementations of a generic interface with Ninject

风格不统一 提交于 2019-11-27 07:23:52
问题 i have the following interfaces injected in Castle Windsor. how do i do the same in Ninject? container.Register( AllTypes.FromAssemblyNamed("Apps.Web") .BasedOn(typeof(ICommandHandler<>)) .WithService.FirstInterface()); i've tried: this.Bind(x => x.FromAssembliesMatching("Apps.Web.dll") .Select(y => y.Namespace.EndsWith("Handlers")) .BindSingleInterface()); but getting Object reference not set to an instance of an object error. 回答1: You can use Ninject's convention binding extensons (install