-
Notifications
You must be signed in to change notification settings - Fork 432
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using AddHttpClient<T> creates multiple instances of Scoped services in the same Scope #6432
Comments
I think this is a duplicate of #5098 |
Did some more research on this and found out this triggers when However it does not trigger when This basically stops the This can create weird and hard to find bugs since this doesn't trigger any errors or warnings. |
THE FIX IS HEREAdd the following setting |
Hi @madushans, Apologies for the delayed response, Were you able to find a solution here. Kindly let us know if you could resolve it can we close this issue? |
The feature switch didn't quite work for me. I may have not had the right version of the host at the time. I decided to make my own scope dependency wrapping the client and added that instead.
All code depending on http client now depends on above and get the client from the property. This solved my immediate issue. Waiting for the feature switch work to be rhe default option to remove the above from my code. Do understand that, that may break existing code of people who inadvertently relied on the "wrong" behavior. |
I doubt anyone relied on it working incorrectly. If they did, there will be a correct way to achieve their weird requirements :) |
I checked internally. Closing this issue as this issue is being addressed and worked upon on a similar scenario in #5098, Please find the space for further updates - azure-functions-host/issues/5098 |
Is your question related to a specific version? If so, please specify:
Does not seems to be specific.
Below are the versions I use
Microsoft.Extensions.Http (tested with 3.1.4 and 3.1.6)
Microsoft.NET.Sdk.Functions (tested with 3.0.7 and 3.0.9)
Microsoft.Azure.Functions.Extensions 1.0.0
What language does your question apply to? (e.g. C#, JavaScript, Java, All)
C#
Question
Summary
Basically I have an issue with Scoped services only when using Azure Functions and
AddHttpClient<T>
.How to Reproduce
I have the following dependency structure in my functions app.
All my services are registered as Scoped.
However the
SharedService
seems to get created twice in the same function execution. Once for theParentService
and first dependent service, and once again for the second dependent service, but only when I useAddHttpClient<T>
and only in a Functions app.I log the
HashCode
of theSharedService
instance injected to each place for debugging.DI setup
Services
And I have a function as below
Now, since I registered
ScopedService
asScoped
, I expected only one instance but I get the following output.Parent and Dependent1 always gets the same instance of SharedService, and Dependent2 gets a different instance. If I add a 3rd or 4th dependent service to parent, they all get the same instance injected to Dependent2.
UPDATE2: Only one of the dependents depending on the MyHttpService triggers this issue. If none of the dependent services depend on MyHttpService this behaves expectedly.
This only happens when I use
AddHttpClient<T>()
. I triedAddHttpClient()
(non generic) and also not having it at all, which changes this behavior to reuse mySharedService
correctly in both cases.I currently want to use the
AddHttpClient<T>()
since I want to customize theHttpClient
injected with custom headers .etc. as explained in the guidance here.It appears this only happens in Azure Functions
I have tried this on an ASP.NET Core WebAPI app (File > New > Project > ASP.NET Core Web Application with defaults) and injecting ParentService to the WeatherForecastController that comes with the VS template which does not have this behavior.
I have also tried this on a simple Console App as follows and it also does not have this behavior.
Would like to know why this happens and any way to avoid it so the SharedService is shared between parent and its other dependent services.
UPDATE: Dependency Tree was incorrect. Updated. Code samples unchanged.
The text was updated successfully, but these errors were encountered: