Constructor injection with Quartz.NET and Simple Injector

According to this blog post, you would need to implement a custom IJobFactory, like this: public class SimpleInjectorJobFactory : IJobFactory { private readonly Container container; private readonly Dictionary<Type, InstanceProducer> jobProducers; public SimpleInjectorJobFactory( Container container, params Assembly[] assemblies) { this.container = container; // By creating producers, jobs can be decorated. var transient = Lifestyle.Transient; this.jobProducers = … Read more

Simple Injector: Register ILogger by using ILoggerFactory.CreateLogger()

Use the following registrations: container.RegisterInstance<ILoggerFactory>(loggerFactory); container.RegisterSingleton(typeof(ILogger<>), typeof(Logger<>)); Or, in case you are integrating Simple Injector into a generic host or ASP.NET Core application, make use of the .AddLogging() extension method to even inject a non-generic ILogger into your application components, as demonstrates in this ASP.NET Core Startup class: public class Startup { … public void … Read more

log4net with DI Simple Injector

There’s nothing special about Simple Injector when it comes to configuring log4net. For instance, you can register ILog as follows: container.RegisterSingleton<ILog>(LogManager.GetLogger(typeof(object))); Obviously this registered a single logger for the complete application. In case you want to inject a different logger per class, you will have to define your own adapter implementation: public sealed class Log4NetAdapter<T> … Read more

Register IAuthenticationManager with Simple Injector

As TrailMax already mentioned, the exception you got probably got raised during the call to container.Verify(). At application start-up time there is no HttpContext, hence the exception. Although the removal of the call to container.Verify() will ‘solve’ the problem, I would advise against doing this and I will suggest a better solution below. NightOwl888 references … Read more

Using Simple Injector with Unit Of Work & Repository Pattern in Windows Form

The problem you have is the difference in lifestyles between your service, repository, unitofwork and dbcontext. Because the MemberRepository has a Singleton lifestyle, Simple Injector will create one instance which will be reused for the duration of the application, which could be days, even weeks or months with a WinForms application. The direct consequence from … Read more

Avoiding all DI antipatterns for types requiring asynchronous initialization

This is a long answer. There’s a summary at the end. Scroll down to the summary if you’re in a hurry. The problem you have, and the application you’re building, is a-typical. It’s a-typical for two reasons: you need (or rather want) asynchronous start-up initialization, and Your application framework (azure functions) supports asynchronous start-up initialization … Read more

Simple Injector unable to inject dependencies in Web API controllers

TLTR: the problem is caused by the implicit way Web API handles resolving controller types; register your Web API controllers explicitly and you’ll see where the problem is. Here is a step by step what is happening under the covers: The System.Web.Http.DefaultHttpControllerActivator calls into the SimpleInjectorWebApiDependencyResolver and requests the creation of an API controller. SimpleInjectorWebApiDependencyResolver … Read more