3
I’m using Ninjectwebcommom to work with Inject. I installed the package via Nuget and automatically it creates a class named "Ninjectwebcommon" in the App_start folder, as the documentation itself says. I need to know why it’s not working properly, follow some code snippets:
NinjectWebCommon.cs
public static class NinjectWebCommon
{
private static readonly Bootstrapper bootstrapper = new Bootstrapper();
/// <summary>
/// Starts the application
/// </summary>
public static void Start()
{
DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
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();
try
{
kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
RegisterServices(kernel);
return kernel;
}
catch
{
kernel.Dispose();
throw;
}
}
/// <summary>
/// Load your modules or register your services here!
/// </summary>
/// <param name="kernel">The kernel.</param>
private static void RegisterServices(IKernel kernel)
{
// kernel.Load(AppDomain.CurrentDomain.GetAssemblies());
kernel.Bind<IFooService>().To<FooService>();
}
}
Controller
public class FooController : Controller
{
private readonly IFooService fooService;
public FooController(IFooService fooService)
{
this.fooService = fooService;
}
public ActionResult Index()
{
return View(this.fooService.All());
}
}
This section generates the following error
Error Activating Ifooservice No matching bindings are available, and the type is not self-bindable. Activation path:
2) Injection of dependency Ifooservice into Parameter fooService of constructor of type Foocontroller
1) Request for Foocontroller
Suggestions:
1) Ensure that you have defined a Binding for Ifooservice. 2) If the Binding was defined in a module, ensure that the module has been Loaded into the kernel.
3) Ensure you have not accidentally created more than one kernel.
4) If you are using constructor Arguments, ensure that the Parameter name Matches the constructors Parameter name. 5) If you are using Automatic module loading, ensure the search path and Filters are correct.
Use Ioc to resolve instances, but it Works only in my Homecontroller, if I change to Another controller using exactly the same code (with the Ioc), it generates the error Again. Follows the code using the Ioc.
Follow the stretch using Ioc:
private readonly IFooService fooService;
public HomeController()
{
this.fooService = IoC.Instance.Resolve<IFooService>();
}
public ActionResult Index()
{
ViewBag.MyFoos = this.fooService.All();
return View();
}
generates this error
No matching bindings are available, and the type is not self-bindable.
Error Activating Ifooservice No matching bindings are available, and the type is not self-bindable.
Activation path: 1) Request for Ifooservice
Suggestions:
1) Ensure that you have defined a Binding for Ifooservice.
2) If the Binding was defined in a module, ensure that the module has been Loaded into the kernel.
3) Ensure you have not accidentally created more than one kernel.
4) If you are using constructor Arguments, ensure that the Parameter name Matches the constructors Parameter name.
5) If you are using Automatic module loading, ensure the search path and Filters are correct.
What are the dependencies of
FooService
? They’re being injected as well?– brazilianldsjaguar
And up there in Ninjectwebcommon.Cs, has the web Activator? looks like this:
[assembly: WebActivatorEx.PreApplicationStartMethod...]
– brazilianldsjaguar
Try to replace private readonly Ifooservice fooService; for public Ifooservice Fooservice {get; set;} and remove the constructor parameter from the Foocontroller class. The line fooService = fooService is not required as Ninject itself will initialize the Property with dependency injection.
– Ulysses Alves
@Ulyssesalves made the modifications and did not work. get; set; leaves the null interface.
– Luiz Negrini
And that mistake Error Activating Ifooservice No matching bindings are available, and the type is not self-bindable. still occurs after this change?
– Ulysses Alves
@Ulyssesalves no, but with service null, the problem remains...
– Luiz Negrini
The code you used with comments in English you took from some article? If yes, could you give us the link so we can analyze better?
– Ulysses Alves
@Ulyssesalves is part of Nuget, it automatically installs this class inside the project, follows the link of the documentation: https://github.com/ninject/Ninject.Web.Mvc/wiki/Setting-up-an-MVC3-application
– Luiz Negrini
@brazilianldsjaguar yes for the second question, the first I do not know answer you for sure, but I believe that from the used class, he should solve the others and he has.
– Luiz Negrini
@Luiznegrini So, you inherited your Mvcapplication class from Ninjecthttpapplication, as shown on the page? public class Mvcapplication : Ninjecthttpapplication
– Ulysses Alves
Let’s go continue this discussion in chat.
– Ulysses Alves