ASP.NET MVC 4 + Ninject MVC 3 = No parameterless constructor defined for this object

I know this is an old question but there don’t seem to be any real answers and I’ve worked around the problem so here is my solution: Create a custom controller factory: public class NinjectControllerFactory : DefaultControllerFactory { private IKernel ninjectKernel; public NinjectControllerFactory(IKernel kernel) { ninjectKernel = kernel; } protected override IController GetControllerInstance(RequestContext requestContext, Type … Read more

MVC5, Web API 2 and Ninject

The Ninject.Web.WebApi NuGet package has just been released. From now on the preferred solution is using that package. I couldn’t find any related documentation but after installing the package everything worked for me. Install-Package Ninject.Web.WebApi After installation both the normal MVC and the API controller instances are provided by Ninject. If you have Ninject.Web.Common already … Read more

Looking for a Ninject scope that behaves like InRequestScope

UPDATE: This approach works against NuGet current, but relies in an anomaly in the InCallscope implementation which has been fixed in the current Unstable NuGet packages. I’ll be tweaking this answer in a few days to reflect the best approach after some mulling over. NB the high level way of structuring stuff will stay pretty … Read more

Does Ninject support Func (auto generated factory)?

NB Ninject 3.0 and later has this fully supported using the Ninject.Extensions.Factory package, see the wiki:- https://github.com/ninject/ninject.extensions.factory/wiki EDIT: NB there is a Bind<T>().ToFactory() implementation in Ninject 2.3 (which is not a fully tests supported release but is available from the CodeBetter server) Ninject does not support this natively at the moment. We planned to add … Read more

An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key

Edit: Original answer used Find instead of Local.SingleOrDefault. It worked in combination with @Juan’s Save method but it could cause unnecessary queries to database and else part was probably never executed (executing the else part would cause exception because Find already queried the database and hadn’t found the entity so it could not be updated). … Read more

Creating an instance using Ninject with additional parameters in the constructor

The With.ConstructorArgument existed in 1.0 for this purpose. In 2.0, the syntax has changed slightly:- With.Parameters.ConstructorArgument with ninject 2.0 See Inject value into injected dependency for more details and examples of how to use the context, providers and arguments to pass stuff like this around more correctly. EDIT: As Steven has elected to pretend my … Read more

Dependency Injection with Ninject and Filter attribute for asp.net mvc

See this answer: Custom Authorization MVC 3 and Ninject IoC If you want to use constructor injection then you need to create an attribute and a filter. /// Marker attribute public class MyAuthorizeAttribute : FilterAttribute { } /// Filter public class MyAuthorizeFilter : IAuthorizationFilter { private readonly IUserService _userService; public MyAuthorizeFilter(IUserService userService) { _userService = … Read more

Validation: How to inject A Model State wrapper with Ninject?

The solution given by that article mixes validation logic with the service logic. These are two concerns and they should be separated. When your application grows you will quickly find out that validation logic gets complicated and gets duplicated throughout the service layer. I, therefore, like to suggest a different approach. First of all, it … Read more