ninject

ASP.NET MVC, Ninject, single instance per request for multiple constructors

随声附和 提交于 2019-11-29 06:11:46
问题 Im trying to implement an unit of work pattern by passing an unit of work instance into my repositories. Relevant code from Global.asax. public class SiteModule : NinjectModule { public override void Load() { Bind<IUnitOfWork>().To<SqlUnitOfWork>() .InRequestScope() .WithConstructorArgument("connectionString", ConfigurationManager.ConnectionStrings["Entities"].ConnectionString); Bind<IProductRepository>().To<ProductRepository>(); Bind<ICategoryRepository>().To<CategoryRepository>(); } }

Ninject-ing a dependency in Global.asax

拈花ヽ惹草 提交于 2019-11-29 04:15:32
问题 I'm starting a web application with MVC3 and Ninject. There is one dependency that I also need in the Global.asax file that needs to be a singleton. I thought it should be like this: public class MvcApplication : NinjectHttpApplication { IUserAuthentication _auth; public MvcApplication() { base.AuthenticateRequest += new EventHandler(MvcApplication_AuthenticateRequest); } protected override IKernel CreateKernel() { var _kernel = new StandardKernel(new SecurityModule()); _auth = _kernel.Get

Ninject passing in constructor values

对着背影说爱祢 提交于 2019-11-29 03:40:38
With Ninject, how do you configure the kernel so I can define what constructor values are passing into the instantiation of an object? I have the following configured in a module: Bind<IService1>() .To<Service1Impl>() .InSingletonScope() .Named("LIVE"); Bind<IService2>() .To<Service2Impl>() .InSingletonScope() .Named("LIVE") .WithConstructorArgument( "service1", Kernel.Get<IService1>("LIVE")); Service2Impl takes a constructor parameter of IService1 but I want this to come from the container. I also want to have named bindings as my code will be targeting different versions at runtime. This

Ninject default contextual binding

不问归期 提交于 2019-11-29 03:07:31
I have an interface with a few different concrete implementations. I am trying to give Ninject a default to use and only use the other implementation if a name matches. For instance, I have the following bindings. Bind<ISomething>().To<DefaultSomething>() Bind<ISomething>().To<OtherSomething>().Named("55abd8b8-097f-4e1c-8d32-95cc97910604"); What I would like is if the Named section doesn't match, to use the DefaultSomething implementation. When I pass in the explicitly bound guid, it works fine. When I pass in any other guid I get the "No matching bindings are available" exception. Bind

Inject Array of Interfaces in Ninject

青春壹個敷衍的年華 提交于 2019-11-29 01:34:34
Consider the following code. public interface IFoo { } public class Bar { public Bar(IFoo[] foos) { } } public class MyModule : NinjectModule { public override void Load() { Bind<IFoo[]>().ToConstant(new IFoo[0]); // ToConstant() is just an example } } public class Program { private static void Main(string[] args) { var kernel = new StandardKernel(new MyModule()); var bar = kernel.Get<Bar>(); } } When I try to run the program I get the following exception. Error activating IFoo No matching bindings are available, and the type is not self-bindable. Activation path: 2) Injection of dependency

Ninject - how and when to inject

耗尽温柔 提交于 2019-11-29 00:57:18
问题 I'm a newbie when it comes to DI and ninject and I'm struggling a bit about when the actual injection should happen and how to start the binding. I'm using it already in my web application and it working fine there, but now I want to use injection in a class library. Say I have a class like this: public class TestClass { [Inject] public IRoleRepository RoleRepository { get; set; } [Inject] public ISiteRepository SiteRepository { get; set; } [Inject] public IUserRepository UserRepository { get

Error “More than one matching bindings are available” when using Ninject.Web.Mvc 2.0 and ASP.NET MVC 1.0

天涯浪子 提交于 2019-11-28 23:47:12
问题 Recently I've switched to Ninject 2.0 release and started getting the following error: Error occured: Error activating SomeController More than one matching bindings are available. Activation path: 1) Request for SomeController Suggestions: 1) Ensure that you have defined a binding for SomeController only once. However, I'm unable to find certain reproduction path. Sometimes it occurs, sometimes it does not. I'm using NinjectHttpApplication for automatic controllers injection. Controllers are

MVC 4 Web Api Controller does not have a default constructor?

怎甘沉沦 提交于 2019-11-28 23:17:19
Here is the trace: <Error> <Message>An error has occurred.</Message> <ExceptionMessage> Type 'ProjectName.Web.Api.Controllers.ContinentsController' does not have a default constructor </ExceptionMessage> <ExceptionType>System.ArgumentException</ExceptionType> <StackTrace> at System.Linq.Expressions.Expression.New(Type type) at System.Web.Http.Internal.TypeActivator.Create[TBase](Type instanceType) at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage request, Type controllerType, Func`1& activator) at System.Web.Http.Dispatcher

Where to locate Ninject modules in a multi-tier application

情到浓时终转凉″ 提交于 2019-11-28 22:56:53
问题 My application includes a number of back-end assemblies (including an Entity Framework data repository layer) that are shared by a number of front-end assemblies (including a Windows service and an MVC3 web application). My understanding of the Ninject binding process is that each assembly that contains injectable types should also contain an Ninject module that defines the default bindings for these types. The set of defined modules would then be loaded into the Ninject Kernel of the

Web API2 NinjectWebCommon.cs do not appear

拈花ヽ惹草 提交于 2019-11-28 21:29:49
I am doing an Empty Web API in Visual Studio 2013 Framework 4.5. Obviously NinjectWebCommon.cs do not appear. I installed via Nuget, Ninject, Ninject.Web.Common, Ninject.MVC5, Ninject.Web.Common.WebHost, Ninject.Web.WebApi, Ninject.web.WebApi.WebHost but NinjectWebCommon.cs still does not appear. What else do I need to install? Can I add that file manually? thanks It looks like the most recent Ninject.Web.Common.WebHost 3.3.0 NuGet package no longer includes the NinjectWebCommon.cs. Older versions, such as 3.2.0 do include this file. Ninject.Web.Common.WebHost 3.3.0 provides a