ninject

Ninject 3 InRequestScope not returning the same instance for the same request

折月煮酒 提交于 2019-12-04 02:59:18
问题 Recently, I upgraded one of my MVC3 projects from Ninject 2 to Ninject 3. After a couple of minutes trying to find why InRequestScope was not anymore available, I found that this is now an extension of Ninject.Web.Common. Now, when I try to run the application, Ninject works like if all types binded with a scope InRequest would be InTransientScope; a new instance was created each time. In my class that inherits from NinjectModule, I have a simple bind like that: Bind<ViewModel.Activity>()

How to inject an object into a WCF validator class

旧城冷巷雨未停 提交于 2019-12-04 02:53:04
Following up on using dependency injection for WCF services , is there any way of using DI for WCF validators , so that one could do this: public class DIValidator : UserNamePasswordValidator { private readonly IService service; [Inject] public DIValidator(IService service) { this.service = service; } public override void Validate(string userName, string password) { service.Login(userName, password); } } EDIT - I tried to apply Dzmitry's advice to my custom behaviour extension, since my validator is defined in app.config. Sadly I get a MethodMissingException, since wcf wants my validator to

Ninject Bind When Ancestor Of Type T

狂风中的少年 提交于 2019-12-04 01:22:40
I've got a dependency chain that looks roughly like this: public class CarSalesBatchJob { public CarSalesBatchJob(IFileProvider fileProvider) { ... } } public class MotorcycleSalesBatchJob { public MotorcycleSalesBatchJob(IFileProvider fileProvider) { ... } } public class FtpFileProvider : IFileProvider { public FtpFileProvider(IFtpSettings settings) { ... } } public class CarSalesFtpSettings : IFtpSettings { ... } public class MotorcycleSalesFtpSettings : IFtpSettings { ... } Up until now, I've been using conventions based bindings, but that isn't good enough anymore because I've got more

Using Nininject MVC with class libraries

杀马特。学长 韩版系。学妹 提交于 2019-12-03 23:37:40
问题 I'm quite new to IoC frameworks so please excuse the terminology. So what I have is a MVC project with the Nininject MVC references. I have other class libarys in my project e.g. Domain layer, I would like to be able to use the Ninject framework in there but all of my bindings are in the NinjectWebCommon.cs under the App_Start folder in the MVC project: private static void RegisterServices(IKernel kernel) { kernel.Bind<IHardwareService>().To<WindowsHardwareService>(); kernel.Bind<IStatusApi>(

MVC3 Action Filter Using Database (EF 4.1 DBContext, Ninject)

大憨熊 提交于 2019-12-03 22:44:37
问题 I'm trying to setup an 'Authorization' Filter on an Action, creating my own ActionFilterAttribute where I do a database lookup to determine if a user has access to a certain resource. On my class inheriting from ActionFilterAttribute, I have created an Injected(Ninject) property to hold the service that I am using for the database access. I have a parameterless constructor so that I can use this as an attribute on my actions. In the 'OnActionExecuting' Method, I am able to gain access to the

Ninject - binding constructors with arguments / Entity Framework connection string

人走茶凉 提交于 2019-12-03 22:35:55
Please forgive my ignorance, but I am very new to IOC and NinJect. I have searched for high and low for easily understandable solutions but so far they have eluded me. So far I have the following and all works as expected: private class StandardModule : NinjectModule { public override void Load() { Bind<ILog>().To<NLogLogger>(); // Use NLog Bind<IMyEntityFrameWorkRepository().To<MyEntityFrameWorkRepository>(); } } MyEntityFrameWorkRepository then creates its own EF DbContext via a connection string declared in app/web.config: public class MyDbContext : DbContext { public MyDbContext() : base(

Ninject and DataContext disposal

。_饼干妹妹 提交于 2019-12-03 22:20:38
I'm using Ninject to retrieve my DataContext from the kernel and I was wondering if Ninject automatically disposes the DataContext, or how he handles the dispose() behaviour. From own experiences I know disposing the datacontext is pretty important and that whenever you create a direct object of the DataContext (as in: new DataContext()) you should use a using() block. My question thus is: When im retrieving my DataContext from the kernel, should I still have to use a using() block? Or does Ninject fix this for me? I am investigating this for my colleague Bas. I was looking in the Ninject 2

Multiple database contexts when using repository pattern

ε祈祈猫儿з 提交于 2019-12-03 22:13:01
I am a bit lost right now... I've never seen this much divergent information regarding solution to the problem. But let us start from the beginning. I am using ASP.NET MVC with Repositories injected to Controllers, thanks to the Ninject. I have 2 simple Entities: Admin with a list of created blog entries and Entries with one virtual Admin field. Admin: public class Admin { [Key, ScaffoldColumn(false)] public int Id { get; set; } [Required(ErrorMessage = "Zły login.")] [StringLength(20), MinLength(3)] [RegularExpression(@"^[a-zA-Z0-9]*$", ErrorMessage = "Special characters are not allowed.")]

How do I get a new object using Ninject as in this example

旧巷老猫 提交于 2019-12-03 21:44:33
I am in need of adding an item of type IVehicle which is injected at runtime from constructor to a for loop. IVehicle vehicle; for (int i=0;i<=someValue;i++) { list.insert(i,vehicle); //some processing to assign values } now because Ivehicle is already injected by this time, my list has same value despite the value on view different and coming through the controller. How can I new up this object everytime EDIT The best way to new up this object everytime I found was to request new from the kernel that was injecting it. I am using Ninject as said earlier. All I did was use a create a variable

Ninject Binding, Interface to Interface

余生颓废 提交于 2019-12-03 21:11:12
I want to do something along the lines of this: kernel.Bind<IBootTaskA>().To<BootTaskA>().InSingletonScope(); kernel.Bind<IBootTaskB>().To<BootTaskB>().InSingletonScope(); kernel.Bind<IBootTask>().To<IBootTaskA>(); kernel.Bind<IBootTask>().To<IBootTaskB>(); So i can do this: public class Boot { public Boot(IBootTask[] bootTasks) { foreach(var task in bootTasks){task.Execute();} } } but i cant seem to bind an interface to an interface, anyone know a way around this? Heres how you do it. public class Service : IServiceA, IServiceB {} this.Bind<Service>().ToSelf().InSingletonScope(); kernel