Reputation: 85845
I upgraded from ninject 2.0 to 2.2 and nothing works anymore.
When I use nuget it makes this
[assembly: WebActivator.PreApplicationStartMethod(typeof(MvcApplication3.App_Start.NinjectMVC3), "Start")]
[assembly: WebActivator.ApplicationShutdownMethodAttribute(typeof(MvcApplication3.App_Start.NinjectMVC3), "Stop")]
namespace MvcApplication3.App_Start
{
using System.Reflection;
using Microsoft.Web.Infrastructure.DynamicModuleHelper;
using Ninject;
using Ninject.Web.Mvc;
public static class NinjectMVC3
{
private static readonly Bootstrapper bootstrapper = new Bootstrapper();
/// <summary>
/// Starts the application
/// </summary>
public static void Start()
{
DynamicModuleUtility.RegisterModule(typeof(OnePerRequestModule));
DynamicModuleUtility.RegisterModule(typeof(HttpApplicationInitializationModule));
bootstrapper.Initialize(CreateKernel);
}
/// <summary>
/// Stops the application.
/// </summary>
public static void Stop()
{
bootstrapper.ShutDown();
}
/// <summary>
/// Creates the kernel that will manage your application.
/// </summary>
/// <returns>The created kernel.</returns>
private static IKernel CreateKernel()
{
var kernel = new StandardKernel();
RegisterServices(kernel);
return kernel;
}
/// <summary>
/// Load your modules or register your services here!
/// </summary>
/// <param name="kernel">The kernel.</param>
private static void RegisterServices(IKernel kernel)
{
}
}
}
I use this
/// <summary>
/// Application_Start
/// </summary>
protected void Application_Start()
{
// Hook our DI stuff when application starts
IKernel kernel = SetupDependencyInjection();
}
public IKernel SetupDependencyInjection()
{
IKernel kernel = CreateKernel();
// Tell ASP.NET MVC 3 to use our Ninject DI Container
DependencyResolver.SetResolver(new NinjectDependencyResolver(kernel));
return kernel;
}
protected IKernel CreateKernel()
{
var modules = new INinjectModule[]
{
new NhibernateModule(),
new ServiceModule(),
new RepoModule()
};
public class NinjectDependencyResolver : IDependencyResolver
{
private readonly IResolutionRoot resolutionRoot;
public NinjectDependencyResolver(IResolutionRoot kernel)
{
resolutionRoot = kernel;
}
public object GetService(Type serviceType)
{
return resolutionRoot.TryGet(serviceType);
}
public IEnumerable<object> GetServices(Type serviceType)
{
return resolutionRoot.GetAll(serviceType);
}
}
So when I try to use my way(what worked before the changes) when I load it up now I get some no parameterless controller.
When I use their I get
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.
Upvotes: 0
Views: 472
Reputation: 32725
I hope you know that there is a documentation at https://github.com/ninject/ninject.web.mvc/wiki/Setting-up-an-MVC3-application where this question is explained.
Upvotes: 1
Reputation: 89
Move your module array into the
var modules = new INinjectModule[]
{
new NhibernateModule(),
new ServiceModule(),
new RepoModule()
};
into the RegisterServices and add
kernel.Load(modules);
Upvotes: 1
Reputation: 1039298
Those are two different approaches into configuring the kernel. The approach you were using requires modifying Global.asax
. The NuGet package uses this new feature of ASP.NET 4 which allows dynamic modules to be registered at application startup. Because the authors of the NuGet package didn't want to mess up with Global.asax
as there might be some other existing code, they preferred to use this separate file for configuring the kernel.
The two approaches are not compatible and you should never use them both in the same application. The new version also already contains a NinjectDependencyResolver
so you no longer need to write or set any custom DependencyResolver.SetResolver
.
All you need to do is use the RegisterServices
static method in the bootstrapper class to configure your kernel:
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<ISomeControllerDependency>().To<SomeConcreteImpl>();
}
And if you had some NInject modules that you wanted to load:
private static void RegisterServices(IKernel kernel)
{
kernel.Load(
new NhibernateModule(),
new ServiceModule(),
new RepoModule()
);
}
That's it. Don't forget to remove any NInject trace from your Global.asax
to avoid any conflicts.
I guess the reason your code didn't work with the first approach is because you didn't load the modules in the RegisterServices
method.
Upvotes: 0