Closed CasperWSchmidt closed 2 years ago
If Simple Injector can't resolve IHttpClientFactory
, it means that it hasn't mean registered in MS.DI's ServiceCollection
. If it isn't physically part of the ServiceCollection
, Simple Injector will be unable to find it and resolve it through its auto-cross-wiring feature.
Through debugging the startup test, we concluded that IHttpClientFactory
should indeed be part of the service collection when SI is asked to resolve the service in question (and so is IHttpContextAccessor
IIRC but that is secondary for now).
I will ask my colleague to debug the test again and present documentation for this :)
So this is what the service collection looks like after our call to container.AddHttpClient
- our own extension method for SI to add our http client implementation to the container as well as adding the necessary framework stuff to the service collection:
It is worth noting that the registrations for IHttpClientFactory
and IHttpMessageHandlerFactory
both use a lambda to resolve the DefaultHttpClientFactory
from the service collection, which must be the reason that ImplementationType
is null in the picture.
After some more debugging, my colleague decided to follow the service collection integration guide closely and add his own service collection, calling AddSimpleInjector
, building the service provider and calling UseSimpleInjector
before passing the servicecollection on to our Startup method. Seeing this code (and reading the integration guide REALLY CLOSELY) I had an apiphany - The call to UseSimpleInjector
is triggered when the Azure Functions Runtime needs to resolve IMediator
when an Azure Function is triggered. This is what the unit tests are missing - we do not rely on the Azure Functions Runtime to serve an instance of IMediator
and so the call to UseSimpleInjector
to finish the setup is never done!
So the issue is with our test, not the library - sorry for disturbing you :)
Hi Casper,
I'm happy you figured this out. I was starting to expect some weird behavior in the framework. I'm the first to admit, though, that the Azure Functions integration for Simple Injector is a bit clunky and confusing. It tries to work around the limitations in Azure Functions to get the integration done. Hopefully this is something that Microsoft will improve in the future so the integration with Simple Injector can be simplified in the future.
Yeah I know, and I agree :) I remember we worked on the integration "together"
Describe the bug
According to the documentation (and code) AutoCrossWireFrameworkComponents should be true by default. However we experience the need to manually cross-wire stuff from MS.DI to SimpleInjector. As mentioned in the code in #936
IHttpContextAccessor
does not seem to be cross wired automatically. Recently we made some changes to our internal Http package (moving away from RestSharp and into usingIHttpClientFactory
). It now looks like this needs manual cross-wiring as well.Expected behavior
Unless setting
AutoCrossWireFrameworkComponents
tofalse
I would think that cross-wiring is handled automatically.Actual behavior
In our Azure Functions Integration package we have the following code:
trying to get any instance from the container using the updated internal Http package (called
HttpClient
) we get the following error:The mentioned class
HttpClient
in the exception is our internal adapter/helper that handles the connection toIHttpClientFactory
and contains some helper methods. All code that wants to do Http calls uses this class (through an interface) instead ofIHttpClientFactory
directly.Additional context
Add any other context about the problem here. e.g.: