Skip to content

Holes in my dependency injection knowledge: IMultiTenantContextAccessor as a Singleton #661

Closed Answered by AndrewTriesToCode
SmagPie asked this question in Q&A
Discussion options

You must be logged in to vote

Hi, you ask a very good question.

The answer, currently, is that the accessor implementation uses an AsyncLocal instance.

AsyncLocals have some special behavior whenever an async/await happens so that each new continuation gets its own copy of the instance from it's parent. So in the middleware pipeline pretty much immediately triggers an async/await so there is no conflict for tenants.

Some downsides exist though. Performance is impacted. Also any code in the async/await stack "higher" than the multitenant middleware will not see the tenants on the return trip through the pipeline.

I've also toyed with an implementation that instead uses a scoped DI variable or the HttpContext property bag.

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by SmagPie
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants