IdentityServer4 icon indicating copy to clipboard operation
IdentityServer4 copied to clipboard

JwtRequestValidator ctor causes SimpleInjector validation error

Open protonyx opened this issue 7 years ago • 8 comments

We use SimpleInjector in tandem with the built-in Microsoft DI (https://simpleinjector.readthedocs.io/en/latest/aspnetintegration.html) to provide more advanced (and opinionated) DI scenarios. During our startup flow, we call a Verify method on the SimpleInjector container which walks through the dependency tree and ensures that all dependencies are constructable.

When SimpleInjector gets to JwtRequestValidator, an exception is thrown because contextAccessor.HttpContext is null, which is expected behavior when you are calling this method outside the scope of a request.

This issue is not seen when using the default Microsoft DI container, because it does not try to construct all classes known to the container. This capability of SimpleInjector allows us to fail fast and detect DI configuration-related issues on startup.

This is not a high priority issue, since it has limited impact to an optional feature of a non-standard DI container.

protonyx avatar Jul 15 '19 21:07 protonyx

Is SimpleInjector clever enough to deal with ArgumentNullExceptions?

leastprivilege avatar Jul 23 '19 15:07 leastprivilege

I did some testing, and it looks like it is not. SimpleInjector assumes that all registered classes are constructable. HttpContextAccessor is constructable without providing an HttpContext (https://github.com/aspnet/HttpAbstractions/blob/master/src/Microsoft.AspNetCore.Http/HttpContextAccessor.cs), so a call to HttpContextAccessor.HttpContext without setting the context will return null.

protonyx avatar Jul 24 '19 02:07 protonyx

So where are we on this issue?

brockallen avatar Jul 29 '19 01:07 brockallen

I'm using Autofac in tandem with Microsoft DI and I'm getting the same error in my integration tests for controller instantiation. Each of my controllers that inject IIdentityServerInteractionService cannot now be tested. Any ideas?

m17kea avatar Aug 01 '19 20:08 m17kea

I am experiencing the same issue when using Simple Injector and updating to 2.5.0, so it is kinda keeping us from updating from 2.4.0. Maybe the HttpContext access could be moved away from the constructor?

ChristianKuhre avatar Aug 05 '19 11:08 ChristianKuhre

Will be fixed here

https://github.com/IdentityServer/IdentityServer4/pull/3517

leastprivilege avatar Aug 05 '19 15:08 leastprivilege

Thank you, that change fixes our issue.

protonyx avatar Aug 06 '19 03:08 protonyx

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

lock[bot] avatar Jan 10 '20 22:01 lock[bot]