You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently we are migrating legacy app to Asp.Net Core 2.2 as the first step, so we are using LightInject.Microsoft.AspNetCore.Hosting.
There are several interceptors that access current scope, for example transaction interceptor that automatically starts DB transaction, within interceptor we want to get scoped instance of EF context: Container.GetInstance<IContext>(), it requires current scope to work. I've tried the following:
The reason that LightInject disables the "current scope" is because the default DI provider does not support this either. Instead it relies on passing the scope rather than storing the current scope in an AsyncLocal<T>. The "Current scope" feature is only kept for backward compatibility and should be avoided in Asp.Net Core apps. Since I don't know exactly what you are trying to do here it might be simpler if you could come up with a simple repro app and maybe I can show you how to do this in another way 👍
On a side note. If you are migrating your apps you should really go for net5.0 or net6.0. netcoreapp2.2 is no longer supported by Microsoft and netcoreapp3.1 will be EOL within this year :)
@seesharper, I wish I could migrate to net6.0, but it is almost impossible to update all our dependencies at once within limited timeframe (as always), so I started with the easiest possible way.
I'm not sure if it is worth creating sample repo at this stage, I'll try to describe my problem better first.
We use LightInject interception package a lot, for example:
// Transactional attribute is a marker for async interceptor[Transactional]publicvirtualTaskSaveAsync(Entityentity){// this is just for illustration purposes, code executed inside transactionawaitContext.SaveAsync();}
Async interceptor simply calls Context.Database.BeginTransaction() and Context.Database.EndTransaction(), but this makes inteceptor to depend on scoped instance of Context. The problem is that inteceptor itself is not created immmediately (lazy), it is initialized only when corresponding method (SaveAsync in this example) is invoked, and it obviously can not work without some scope. I tried using ProxyDefinition(type, useLazyTarget: false) to make interceptors eager(?), but it does not help for some reason, still complains that there is no current scope. I think interception package could capture proxy scope and use it later to initialize interceptor instances, that would solve this problem I guess.
By now I've implemented custom ScopeManager that uses AsyncLocal, it looks promising, but it would be better to avoid such custom code.
Hi again @kemsky . I understand your concern about moving all the way to net6, but I would at least recommend that you move to netcoreapp3.1. Then you can go live with a runtime that is supported by Microsoft :)
Regarding the scope problem you're having. It is not that I don't want to help you, but I have spent so much time in the past trying to guess what the actual scenario is :) . I don't have any experience with Entity Framework either so too many unknowns :) I would really like to help you out, but that would require a simple repro of the problem. In this case I would guess a simple WebAPI app that uses. LightInject, LightInject.Interception and Entity Framework?
Activity
kemsky commentedon Jan 14, 2022
@seesharper, could you please give an advice?
seesharper commentedon Jan 14, 2022
Hi @kemsky
The reason that LightInject disables the "current scope" is because the default DI provider does not support this either. Instead it relies on passing the scope rather than storing the current scope in an
AsyncLocal<T>. The "Current scope" feature is only kept for backward compatibility and should be avoided in Asp.Net Core apps. Since I don't know exactly what you are trying to do here it might be simpler if you could come up with a simple repro app and maybe I can show you how to do this in another way 👍On a side note. If you are migrating your apps you should really go for
net5.0ornet6.0.netcoreapp2.2is no longer supported by Microsoft andnetcoreapp3.1will be EOL within this year :)kemsky commentedon Jan 14, 2022
@seesharper, I wish I could migrate to net6.0, but it is almost impossible to update all our dependencies at once within limited timeframe (as always), so I started with the easiest possible way.
I'm not sure if it is worth creating sample repo at this stage, I'll try to describe my problem better first.
We use LightInject interception package a lot, for example:
Async interceptor simply calls
Context.Database.BeginTransaction()andContext.Database.EndTransaction(), but this makes inteceptor to depend on scoped instance ofContext. The problem is that inteceptor itself is not created immmediately (lazy), it is initialized only when corresponding method (SaveAsyncin this example) is invoked, and it obviously can not work without some scope. I tried usingProxyDefinition(type, useLazyTarget: false)to make interceptors eager(?), but it does not help for some reason, still complains that there is no current scope. I think interception package could capture proxy scope and use it later to initialize interceptor instances, that would solve this problem I guess.By now I've implemented custom
ScopeManagerthat usesAsyncLocal, it looks promising, but it would be better to avoid such custom code.Thanks in advance.
seesharper commentedon Jan 15, 2022
Hi again @kemsky . I understand your concern about moving all the way to net6, but I would at least recommend that you move to netcoreapp3.1. Then you can go live with a runtime that is supported by Microsoft :)
Regarding the scope problem you're having. It is not that I don't want to help you, but I have spent so much time in the past trying to guess what the actual scenario is :) . I don't have any experience with Entity Framework either so too many unknowns :) I would really like to help you out, but that would require a simple repro of the problem. In this case I would guess a simple WebAPI app that uses. LightInject, LightInject.Interception and Entity Framework?
kemsky commentedon Jan 18, 2022
@seesharper, alright, this is example project: https://github.com/kemsky/lightinject-aspnet-core
kemsky commentedon Jan 31, 2022
@seesharper, hi, did you have a chance to look at it?