Dependency Injection in JSR-303 Constraint Validator with Spring fails

I have fought the same problem in Spring Boot environment and I found out that Hibernate internal implementation got in instead of the configured Spring’s one. When the application started, debugger caught a line with the Spring’s factory but later in runtime there was Hibernate’s one. After some debugging, I came to the conclusion that … Read more

Get a service in a IServiceCollection extension

At the time you are calling services.AddSomething(), the service provider has not been built from the service collection yet. So you cannot instantiate a service at that time. Fortunately, there is a way to configure services while using dependency injection. When you do services.AddSomething(options => …) what usually happens is that a certain amount of … Read more

How do I handle classes with static methods with Ninject?

If you’re building a singleton or something of that nature and trying to inject dependencies, typically you instead write your code as a normal class, without trying to put in lots of (probably incorrect) code managing the singleton and instead register the object InSingletonScope (v2 – you didnt mention your Ninject version). Each time you … Read more

Conditional dependency resolver on run-time (.net Core)

There are several options here, but the two that to me are the most obvious are using a factory or the adapter pattern. 1.1 Use a factory public class OrderService { private readonly IPaymentGatewayFactory _factory; public void DoOrder(bool isFoo) { IPaymentGateway service = _factory.Create(isFoo); this._service.Pay(); } } Where the factory can be: public class PaymentGatewayFactory … Read more

Ninject Binding Attribute to Filter with Constructor Arguments

I have figured it out (thanks to Remo’s directions and documentation). Use the appropriate .WithConstructorArgument extension whether you are binding to a Controller or Action filter. For example binding my action filter looks like this: kernel.BindFilter<AuthorizationFilter>(FilterScope.Action, 0) .WhenActionMethodHas<RequireRolesAttribute>() .WithConstructorArgumentFromActionAttribute<RequireRolesAttribute>(“requiredRoles”, o => o.RequiredRoles); Once I understood the Func<> signature, it all became clear. The best way … Read more

How to integrate IoC Membership provider with ASP.NET MVC

The simplest way to get this to work is to use ASP.NET’s standard mechanism of <membership> in web.config. You just let it use the default constructor but you override Initialize() and pull the dependencies there. Use this as reference. Personally, due to things like this, I prefer to avoid the provider model altogether so I … 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

How to inject a reference to a specific IHostedService implementation?

Turns out there’s an easy way to do this (thanks for the pointer, Steven). If you need to be able to inject / get a reference to some service, go ahead and register the service normally (without worrying about any IHostedService stuff): services.AddSingleton<ServiceBusListener>(); Now we can register a separate hosted service whose only responsibility is … Read more

Resolving HttpControllerContext with Castle Windsor

Just for the sake of completeness, an answer I got from Krzysztof Koźmic (the current maintainer of Castle Windsor) on Twitter indicated that the method outlined in the question is, indeed, the correct way of achieving this particular goal. (However, I can’t link to that tweet, since Krzysztof’s twitter account is protected (tweets are not … Read more