Note that there are some explanatory texts on larger screens.

plurals
  1. PONinject Get Instance from Kernel
    text
    copied!<p>I am new to Ninject and I am new to stackoverflow too.</p> <p>I am using it with the ninject.web.mvc extension, I was able to initialize it correctly like this:</p> <pre><code>public class MvcApplication : NinjectHttpApplication { protected override void OnApplicationStarted() { RegisterRoutes(RouteTable.Routes); } protected override IKernel CreateKernel() { var kernel = new StandardKernel(); kernel.Load(AssemblyLocator.GetBinFolderAssemblies()); return kernel; } } </code></pre> <p>And here is my class assemlylocator that scans all the assemblies in the bin folder, searching for all the Ninject modules in the assembly.</p> <pre><code>public static class AssemblyLocator { private static readonly ReadOnlyCollection AllAssemblies = null; private static readonly ReadOnlyCollection BinFolderAssemblies = null; static AssemblyLocator() { AllAssemblies = new ReadOnlyCollection&lt;Assembly&gt;( BuildManager.GetReferencedAssemblies().Cast&lt;Assembly&gt;().ToList()); IList&lt;Assembly&gt; binFolderAssemblies = new List&lt;Assembly&gt;(); string binFolder = HttpRuntime.AppDomainAppPath + "bin\\"; IList&lt;string&gt; dllFiles = Directory.GetFiles(binFolder, "*.dll", SearchOption.TopDirectoryOnly).ToList(); foreach (string dllFile in dllFiles) { AssemblyName assemblyName = AssemblyName.GetAssemblyName(dllFile); Assembly locatedAssembly = AllAssemblies.FirstOrDefault(a =&gt; AssemblyName.ReferenceMatchesDefinition(a.GetName(), assemblyName)); if (locatedAssembly != null) { binFolderAssemblies.Add(locatedAssembly); } } BinFolderAssemblies = new ReadOnlyCollection&lt;Assembly&gt; (binFolderAssemblies); } public static ReadOnlyCollection&lt;Assembly&gt; GetAssemblies() { return AllAssemblies; } public static ReadOnlyCollection&lt;Assembly&gt; GetBinFolderAssemblies() { return BinFolderAssemblies; } } </code></pre> <p>Everything works fine in my controller:</p> <pre><code>public class ReteController : Controller { // // GET: /Rete/ private readonly IReteService _service; public ReteController(IReteService _service) { if (_service == null) { throw new ArgumentNullException("IReteService"); } this._service = _service; } public ActionResult Index() { return View(_service.getReti()); } </code></pre> <p>Until here almost everything was easy to learn, now my problem is that if I need to create a new instance of an object that was bind in the NinjectModule From Ninject I don't know how to acces the kernel from heare.</p> <pre><code>//this is jus a ex. public ActionResult NewRete() { IRete xItem = Kernel.get(); xItem.name= "hope"; return View(xItem); } </code></pre> <p>the problem is that i am not able to find the kernel from my controller. I need to inject it too in the constructor??</p> <p>I hope somebody can help me. Thank a lot for the big help that you guys give me all the days.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload