ninject

Ninject logger using NLog

…衆ロ難τιáo~ 提交于 2019-12-03 10:53:36
I've just started learning Ninject but have come across a problem with the logger. I've currently got a controller that has a service and logger injected into the constructor like so: public ToolsController(IToolsService toolsService, ILogger logger) { logger.Info("ToolsController Created"); this.toolsService = toolsService; this.logger = logger; } The problem is on the logger.Info line (for example) in the constructor which seems to use the wrong logger, so the logger name it prints out is incorrect. Tools.IGeocodeImporter: ToolsController Created Below is how it is setup to get the logger

Accessing ninject kernel in Application_Start

半城伤御伤魂 提交于 2019-12-03 10:20:00
I am using Ninject and the MVC3 extension installed with nuget. My kernel setup code is in the App_Start/NinjectMVC3.cs file. Everything works great in controllers, but I can't figure out how to (properly) bind interfaces in the Global.asax.cs MvcApplication code. I ended up using a hack (creating a public NinjectMVC3.GetKernel() method that returns bootstrap.kernel). However, that will be deprecated, and there must be a proper way to do this that I am not seeing. Here is my code: public class LogFilterAttribute : ActionFilterAttribute { private IReportingService ReportingService { get; set; }

Ninject.MVC5 not generating NinjectWebCommon.Cs

ⅰ亾dé卋堺 提交于 2019-12-03 10:02:25
I'm developing a MVC5 project on Visual Studio 2017 Version 15.4. I'm getting unexpected result here what I never faced before. I've installed Ninject.MVC5 package from nuget . It's installing nicely and not giving any error or warning. But problem is it's not generating NinjectWebCommon.cs file in App_Start folder. Is there any reason? 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 NinjectHttpApplication class you can derive

Ninject error in WebAPI 2.1 - Make sure that the controller has a parameterless public constructor

拜拜、爱过 提交于 2019-12-03 09:53:05
I have the following packages and their dependencies installed in my WebAPI project: Ninject.Web.WebApi Ninject.Web.WebApi.OwinHost I am running this purely as a web-api project. No MVC. When I run my application and send a POST to the AccountController's Register action I get the following error returned: { "message":"An error has occurred.", "exceptionMessage":"An error occurred when trying to create a controller of type 'AccountController'. Make sure that the controller has a parameterless public constructor.", "exceptionType":"System.InvalidOperationException", "stackTrace":" at System.Web

Ninject WithConstructorArgument : No matching bindings are available, and the type is not self-bindable

时光总嘲笑我的痴心妄想 提交于 2019-12-03 09:51:56
My understanding of WithConstructorArgument is probably erroneous, because the following is not working: I have a service, lets call it MyService, whose constructor is taking multiple objects, and a string parameter called testEmail. For this string parameter, I added the following Ninject binding: string testEmail = "test@example.com"; kernel.Bind<IMyService>().To<MyService>().WithConstructorArgument("testEmail", testEmail); However, when executing the following line of code, I get an exception: var myService = kernel.Get<MyService>(); Here is the exception I get: Error activating string No

Using Ninject with a Custom Role provider in an MVC3 app

纵然是瞬间 提交于 2019-12-03 09:43:08
问题 I'm trying to use a custom role provider in an MVC3 app. I've already got the membership provider working ok using Ninject but can't seem to get the role provider working. The Membership provider doesn't require a parameterless constructor but role provider does. Here's some code snippets: Web.config <membership> <providers> <clear/> <add name="MyMembershipProvider" type="MyApp.Models.NHibernateMembershipProvider" applicationName="myApp" /> </providers> </membership> <roleManager enabled=

How do I work with Ninject in an ASP.NET MVC Web App?

不想你离开。 提交于 2019-12-03 09:32:41
I've created a new MVC Web application and I have references to Ninject.dll, Ninject.Web.Common.dll and Ninject.Web.MVC.dll. Global.asax.cs: public class MvcApplication : NinjectHttpApplication { public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new HandleErrorAttribute()); } public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "Index", id = UrlParameter.Optional }); }

injecting viewmodel class without parameterless constructor in WPF with NInject

大兔子大兔子 提交于 2019-12-03 09:22:10
I'm using NInject to resolve the dependency for my first WPF application. Following are my code snippets. My App.xaml.cs goes like. public partial class App : Application { private IKernel container; protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); ConfigureContainer(); ComposeObjects(); } private void ComposeObjects() { Current.MainWindow = this.container.Get<MainWindow>(); } private void ConfigureContainer() { this.container = new StandardKernel(); container.Bind<ISystemEvents>().To<MySystemEvents>(); } } App.xaml goes like this. <Application x:Class="Tracker.App"

Appropriate Repository LifeCycle Scope w/ Ninject in MVC

人盡茶涼 提交于 2019-12-03 09:13:12
What is the appropriate LifeCycle Scope for a repository and the EF context when using Entity Framework 4 with Ninject in an MVC 3 application? I've been using the default of InTransientScope, but questioning whether it should be InRequestScope. public class MyController: Controller { private readonly IMyRepo _repo; public MyController(IMyRepo repo) { _repo = repo; } public ActionResult Index() { var results = _repo.GetStuff(); return View(results); } } Ninject Module: public class MyServices : NinjectModule { public overrride void Load() { Bind<IMyRepo>.To<MyRepo>(); Bind<MyContext>.ToSelf();

Parameterized Factories Using Ninject

匆匆过客 提交于 2019-12-03 09:10:35
How to make Ninject to instantiate object based on variable on run time?. I am trying to inject the correct Repository in The Controller action - MVC 3 - based on parameter come from user input. If user input "BMW" it would bind ICarRepository to BMWRepository , and if he input "KIA" KiaRepository will be injected. [HttpPost] public ActionResult SearchResult(FormCollection values) { string carModel = values["model"]; ICarRepository myRepository = RepositoryFactory.getRepository(carModel); ..... } This is known by switch/case noob instantiation or Parameterized Factories, and i know how to do