Azure / Azure/azure-functions-host

Issue with IServiceProvider, Scope, Sub scope

Open
#5,312 12 comments 1 reaction 0 assignees View on GitHub
Dominant language
C#
Stars
2k
Forks
482
Avg merge
2d 12h
Merged PRs (30d)
38

Description

I've been waiting for #3399 to be resolve but when I ran my test I've got a strange result. It has to do with creating a sub scope using the function scoped **IServiceProvider**. In other IOC frameworks an instance that got created by the scope and tries to resolve **IServiceProvider** would get the creator scope reference. This is not the case in azure functions IOC.
More so between subsequent runs the HashCode of the function scope does not change however some cleanup appears to be done since the scoped services are not the same.

Possibly related to #4914

I have a created sample repository here: [Repo](https://github.com/MihaiStrejer/AzFuncScopeBug)
The code is fairly simple I'm going to list some details below:

**Objects lifetime:**
TestService, MedRequestHandler - transient
MediatR - scoped (as well as the ServiceFactory)
MyStateService - scoped

_Azure Function Scope_ > (DryIoc and the .Net container create new scopes in the FunctionEntry)
     FunctionEntry (Function1) >
         _CreateSubScope **and load a state**_ >
             Resolve TestService **that checks IServiceProvider** >
                 MediatR create and run request >
                     MedRequestHandler **that checks IServiceProvider**

Normally you would expect to see the scope and state created in _CreateSubScope_ to propagate down the line.

![Image result](https://raw.githubusercontent.com/MihaiStrejer/AzFuncScopeBug/master/Result.PNG)

And some final questions:

1) Up until now I've ran with a custom side container that behaves as expected (similar to the example **DryIocSideContainer**). Are there any potential issues with running a side container in functions, at least until the scope issue is fixed?

2) Does the scope created by azure functions handle IDisposables? Does it "behave" similarly to the default DryIoc container?

Contributor guide

Open the contributing guide

Research direction

Start with the linked AzFuncScopeBug sample, following the FunctionEntry flow through CreateSubScope and the DryIocSideContainer comparison. Reproduce the IServiceProvider and scoped-state observations across nested scopes and subsequent runs, then inspect how disposal is handled. Done means the expected scope identity and scoped-service behavior are explained or corrected, with the IDisposable behavior addressed.

Written by the indexing model from the issue text.

Assessment

Tech stack
azure, csharp
Domain
backend, cloud
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.