ninject

Dependency injection and life time of IDisposable objects

眉间皱痕 提交于 2019-12-22 08:17:22
问题 I am trying to develop a library using dependency injection approach (with Ninject) and I am having some kind of confusion likely because of my incorrect design. In summary, my design approach is A parent object has a common object. A parent object uses some variable number of child objects. All child objects should use the very same common object instance with their parent object Here is a simple model of my problem domain. interface IParent : IDisposable { void Operation(); } interface

Post-initialization object creation with ninject

人盡茶涼 提交于 2019-12-22 07:19:02
问题 I'm new to Ninject (and DI in general). I understand how the kernel loads modules, and the code I've written thus far tends to have a single line: myKernel.Get<MyApp>() which constructs everything I need from the bindings in my module. If there's a requirement for new instances post initialization, these are taken care of by factories that I bind for initialization. Up to now, the factories have been free of any ninject dependencies, simply newing up objects on demand. Now I have reached a

How to handle async calls with Ninject InRequestScope?

左心房为你撑大大i 提交于 2019-12-22 05:52:35
问题 We are using Ninject in an ASP.NET Web Api application, and we bind our DbContext with InRequestScope . This works well with most of our requests, because they do all their work synchronously, so the context can be safely disposed after the request is completed. However, we have on request in which we do an asynchronous web service call, that has a continuation method passed as a callback, and that callback method needs to use the database context. However our request shouldn't wait for the

Ninject Dependency Injection with Asp.Net MVC3 or MVC4

五迷三道 提交于 2019-12-22 05:06:38
问题 I am using Ninject MVC3(version 3.0.0.0) for my ASP.Net MVC3 application installed using NuGet Package for Dependency Injection. Here is the Global.asax change: public class MvcApplication : NinjectHttpApplication { public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new HandleErrorAttribute()); } public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.IgnoreRoute("favicon.ico"); routes.IgnoreRoute

How do I inject Identity classes with Ninject?

落爺英雄遲暮 提交于 2019-12-22 04:51:20
问题 I'm trying to use UserManager in a class, but I'm getting this error: Error activating IUserStore{ApplicationUser} No matching bindings are available, and the type is not self-bindable. I'm using the default Startup.cs, which sets a single instance per request: app.CreatePerOwinContext(ApplicationDbContext.Create); app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create); I'm able to get the ApplicationDbContext instance, which I believe is getting injected by Owin (Is

Ninject property injection returns null

别来无恙 提交于 2019-12-22 04:27:31
问题 I've got a WinForms app with the following code: static void Main() { IKernel kernel = new StandardKernel(new MyModule()); TestInterface test = kernel.Get<TestInterface>(); } For the Module.Load() event: Bind<TestClass>().ToSelf().InSingletonScope(); Bind<TestInterface>().To<TestClass>(); At this point test in the Main() method is the proper object I'm expecting. In a form later on, I'm using property injection: [Inject] TestInterface test {get;set;} And once the form is loaded, trying to

Ninject and OnePerRequestModule

你。 提交于 2019-12-22 03:49:39
问题 I have recently tried out Ninject with the Ninject.Web.Mvc extension, and I've noticed something peculiar and, while not breaking, confusing. In the NinjectHttpApplication abstract class, there is a constructor defined as follows.. /// <summary> /// Initializes a new instance of the <see cref="NinjectHttpApplication"/> class. /// </summary> protected NinjectHttpApplication() { this.onePerRequestModule = new OnePerRequestModule(); this.onePerRequestModule.Init(this); } I have placed a debugger

Ninject.MockingKernel.Moq security exception

天大地大妈咪最大 提交于 2019-12-22 00:34:10
问题 I am using Ninject for my IoC container and I'm trying to write some unit tests. I found the Ninject Mocking Kernel so I thought I'd give it a go but I can't get the simplest test to pass. I am missing something and need a little help. Moq.4.0.10827.Final Ninject-2.2.0.0-release-net-4.0 Ninject.MockingKernel-2.2.0.0-release-net-4.0 My unit test... [TestMethod] public void Constructor_CanInitialize() { var kernel = new MoqMockingKernel(); var mock = kernel.Get<IDataRepository>(); <--Error here

Make sure that the controller has a parameterless public constructor using Ninject

夙愿已清 提交于 2019-12-21 20:24:11
问题 Here is the issue at hand: While calling my CustomerController through the URL , I get the following exception: ExceptionMessage: An error occurred when trying to create a controller of type 'CustomerController'. Make sure that the controller has a parameterless public constructor. I am using the following url's: http://localhost:55555/api/Customer/ http://localhost:55555/api/Customer/8 Please note: The /api/Customer/ call were working before I refactored the logic into a business class and

How can you inject an asp.net (mvc2) custom membership provider using Ninject?

北城以北 提交于 2019-12-21 18:58:08
问题 OK, so I've been working on this for hours. I've found a couple of posts here, but nothing that actually resolves the problem. So, let me try it again... I have an MVC2 app using Ninject and a custom membership provider. If I try and inject the provider using the ctor, I get an error: 'No parameterless constructor defined for this object.' public class MyMembershipProvider : MembershipProvider { IMyRepository _repository; public MyMembershipProvider(IMyRepository repository) { _repository =