-
Notifications
You must be signed in to change notification settings - Fork 451
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
Problems with scoped lifetime services (AddScoped) #5098
Comments
I find it hard to believe this is by design. If it is so, documentation should state that very clearly. |
The problem is not durable function specific. Same happens with normal |
I made more experiments and it seem that the DI problem occurs only when I have used |
Hi @heikkilamarko I am also facing the same issue, when this issue happens last week I have pinned to older runtime version, then it started working, now again breaking. I agree it's not an issue with durable function. Even I am using the same DI pattern for HttpClients, Do we have any workaround for httpclient register or waiting for team to provide global fix |
@dhanapalschandran Same thing on my side. Could it be breaking again due to #5096? |
@heikkilamarko can you please share repro code? Is this happening with factory registrations only? Some more details/repro will ensure we're looking exactly at the issue you're running into. @unt1tled / @dhanapalschandran if you can share the code you're having issues with, we can validate with your scenarios as well. Thanks! |
Hi. See below for a repro code (see the comment block in the function code):
|
@fabiocav Hope you are having a nice day :) We are also hopeful that this issue will be fixed soon. We have spent a lot of time writing code to wire up our services and we really love the way scoped services work in ASP.NET Core. Let me know if there is anything I can do to help get this moving! |
We have experienced the same problem, but I also noticed this: This is a pretty big problem for us as well, any progress on this issue? Some additional information: |
@buyckkdelaware during load testing I have found multiple instances of same service type within the same request scope, it occurs frequently for me, at least 1 in 100 requests. I don't know if the weight of the type is relevant, but my Detecting these duplicates is quite easy, using an I haven't yet "handled" it, but I'll probably just throw an exception and fail the request thread using hashcodes as the detection basis. Without handling it, my code sometimes doesn't know that it has failed, it just misses data. |
Are there any workarounds for this issue? |
@darrenhull the workarounds will require some code refactoring at the moment. This is being assigned to a sprint for investigation, so we hope to have an update on this issue soon. |
Hi @fabiocav, Do you know if there is an update on this issue yet? Sorry to keep asking but It’s a pretty big blocking issue for my company’s migration which we are pushing further and further away without being able to suggest when we can resume it. Thanks |
Hi @fabiocav, I see this has been triaged and had the p2 tag. What exactly does the p2 mean, guessing priority2? Can you give us an update please? Thanks |
@darrenhull yes, this is a priority label. The issue has been triaged, but not yet assigned. We'll have the milestone updated once this is assigned to a sprint. |
@fabiocav: this really sounds like a .net core bug/issue. can you confirm? i'm running into something similar using a vanilla asp.net core service. |
@fabiocav : Not to blame anyone, but how can a basic feature like Scoped or not being labeled as a p2 ? This seems like a rather basic functionality ? |
This appears to be a problem on at least .NET Core 2.1, but is fixed in 3.x. I've raised it here: dotnet/runtime#34586 |
@fabiocav any update please? |
Any updates on this one? We registered our DbContext as scoped, but we noticed, if we start multiple http requests, the context gets shared between the different requests, which leads to threading issues: { Any feedback on how to fix this would be greatly appreciated. |
@Schuer84 can you please open a separate issue with those details so we can take a closer look at your scenario? We have no evidence that with the changes made behind the flag still have issues with scoped services, so it would be good to rule other things out. Thanks! |
Alright some new info on this in our functions we are also using HttpClients with DI with AddHttpClient. After enabling EnableEnhancedScopes we are seeing fewer errors related to object disposed exceptions from DryIoc related to our DbContexts when locally debugging. As soon as we deploy our Function to Azure and run tests on it everything falls apart and almost every function trigger fails.
The function Version is V3 we're running .net core 3.1 and ef core 3.1 version. This is ridiculous that we can't use proper DI and HttpClient injection with functions runtime and packages that are in production. You need to provide at least a workaround otherwise there's no point in using azure functions whatsoever. Without the Feature flag we can at least get our functions running and only encountering the issue rarely. |
@Gevil The workaround we found when using HttpClients is by adding an extra abstraction layer and don't inject an HttpClient (or HttpClientFactory) directly. This is now running for about 1 year in production without any issues. Example:
|
@thomasvdb so what do you inject into the Function then? In the example you are injecting an |
Our strategy so far dealing with this in our case (given this is your vanilla ef.core n-tier app with db store, this may be applicable for other folks) was that due to separate db context (and its associated changed entities), data was not being saved as save changes was invoked on the primary db context ,not the secondary we have moved all functions that talk to Http (fortunately that is few integration points in our apps) to a separate fn app... these are woken by commands and do the http dance and persist whatever they retrieve etc into db store, for many of the data services we have added an optional param to send a repository object to interact with DB - so as to skip the one injected by DI (as that is tied to a separate db context due to this bug) this way we gain additional runtime isolation - not to frown upon - all tied to the same fn app plan so the overhead is not huge - and the code change associated is easy to dissolve once this is resolved (and we could have done feature flag) |
@eluchsinger Indeed, I inject the |
@fabiocav any update on making this feature flag default? i haven't seen/heard anything. our team has been tracking this issue for a long time and we have to add this feature flag to each app we make so would be nice that it gets moved to default. |
in case anyone else is looking for the answer, i found it here: #7669 seems that it will only be removed in v4, not v3 |
I've just experienced this issue when using middlewares (.NET 6, isolated), I have got an auditing scope defined in the middleware before the function call where I resolve a (scoped) EF DB context here and would like to call SaveChanges on this DB context at the end of the request pipeline, but it seems this DB context is different to the one that is resolved in the function, so doing save changes at the end does nothing as the changes never got tracked on this DB context. Please let me know if this is a bug or expected behavior for the host and if expected how can I achieve the above goal. Thanks! |
@soloham -- can you open an issue in the https://github.com/Azure/azure-functions-dotnet-worker/issues repo? Please include some example code to show us how things are set up in your scenario. This issue is related to the in-proc DI setup, which is different from that in the isolated worker. |
I ran into a similar issue in 3.1, I had a ScopedServiceProvider and when HttpClientFactory.CreateClient was called inside it, it caused the next time I resolved a interface some of the instances to be lost. After looking at the source code for I did get it to work setting SupressHandlerScope to true. Not sure what side effects this has. |
I confirm that the issue persists even in version 4 of the Azure Functions runtime. Worst of all, I don't use the IHttpClientFactory interface and yet AddScoped always generates new instances on each constructor injection. |
Found this thread after banging by head against he wall for 3 days. We had a transient class A which depended on a scoped dbcontext and another scoped service B with httpclient added through httpclient nuget package and dbcontext I could reproduce an issue where the first time service A was resolved through a scoped service provider, two instances of dbcontext were created. Service A and Service B receives separate instances of db context. Service B modified entities through dbcontext and save changes was called on service A. Because there were two instances of dbcontext and each service wrongly had its own instance - nothing was saved to the database. Subsequent resolutions of service A did not produce the same behavior and both service A and service B received the same instance of dbcontext. Enabling the feature flag mentioned earlier helped, but I am yet to test it on production. |
He everybody. I see this issue has been opened for almost 3 years. I'm using all the most recent versions and I'm continue facing the same issue. |
@jorge-duarte -- Without seeing your exact repro, I believe this is working as expected. Each function invocation is its own scope, which mean it gets its own instance of scoped services. Similar to an http request in ASP.NET. Could you also open a separate issue and tag me and @fabiocav in it so we can take a look at your exact scenario? |
I also have Scoped lifetime problem with Isolated runtime. It looks like there are two scoped lifetimes for one http request. Here is my code: Startup.cs
Function1.cs
When i call "http://localhost:7071/api/Function1", then it will log this out:
Its expected that there should be 1 scoped lifetime in one http request, where the "Middleware" and "Function" scopes are both shared. So this is a bug too, correct? Im using:
It happens both with .NET 7 rc 2 and .NET 6 with the latest versions of the nuget packages and also the preview packages. WorkaroundMy workout right now, is to instantiate all services in my middleware through the FunctionContext, and then it will work
Then when i call the api, it will log out:
Edit: |
@Assassinbeast Your issues seems to be in the isolated model. Could you please open a new issue here: https://github.com/Azure/azure-functions-dotnet-worker/issues |
Closing this issue as based on the recent reports, the default behavior in 4.0 (or using the If you continue to experience similar problems, please open a separate issue with the details so we can take a closer look. Thank you! |
Hi,
We are using durable functions with dependency injection. In our
Startup.cs
file, we register our dependencies as follows:In our activity function, we are using normal constructor injection to get
IMyService
instances.The problem is that even if we are using
AddScoped
for registering the service, during the activity function run, each class that asks for the service, gets a different instance ofIMyService
. This breaks our app logic, becauseIMyService
users won't see each other's changes.As a workaround to the earlier Azure Functions runtime DI issues, we had the following pinning in place
FUNCTIONS_EXTENSION_VERSION = 2.0.12673.0
. The pinned version is not supported anymore by Azure, so our function DI is now broken again.The text was updated successfully, but these errors were encountered: