csla: DI still giving wrong IContextManager within new scope in LocalProxy (IsStatefulRuntime=true)
@rockfordlhotka @TheCakeMonster All testing good in preview 6 except one last issue - it’s baaaack 😦
AFAIK - this issue only shows up when
- LocalProxy is being used
- A new scope is in play (e.g. for ApplicationContextManager instances with
IsStatefulRuntime=true)
As you know, in LocalProxy, if in a new scope, we now manually create ApplicationContextManagerAsyncLocal.
But since DI doesn’t create it, DI doesn’t yet have a scoped instance of IContextManager to give others. So the next time (while in that scope) any code uses DI to get an IContextManager, a new instance is given for whatever IContextManager type is registered. (In BSS this ends up being a new instance of ApplicationContextManagerHttpContext)
BUG: So you end up with LocalProxy having an ApplicationContextManagerAsyncLocal while my code gets a separate ApplicationContextManagerHttpContext
Two different IContextManagers on logical server side is a problem. We need to devise a way where, during the new scope in LocalProxy, only one is used and it is always ApplicationContextManagerAsyncLocal.
Back to the chicken-egg scenario:
Maybe you’ll think of a better way, because this is rough, but I think it will work. After brainstorming on this for hours, it’s the only way I can think of that respects DI:
FIRST:
- Create a new scoped service called “LocalProxyState” which has a bool property “NewScopeExists”.
- See about making an abstract base class called
ApplicationContextManagerFacadewhich implementsIContextManager. This can then be used for 3 below. - Make each
IContextManagerimplementation whereIsStatefulRuntime=truebe a class that inherits fromApplicationContextManagerFacade, and which is a facade wrapper with anActiveContextManagerjust like the currentCsla.AspNetCore.ApplicationContextManagerclass. - Each of these facade context managers needs to use the
LocalProxyStatein their CTOR to know if they should create theActiveContextManageras aApplicationContextManagerAsyncLocal
SECOND:
- Immediately after LocalProxy creates the new scope, it will fetch a fresh instance of this service
var lpScopState = provider.GetRequiredService<LocalProxyState>()then it setslpScopeState.NewScopeExists= true - LocalProxy should then use DI to get it’s
IContextManagerand everyone gets one happy scoped instance
If you think it’s a good idea, I could try it in a PR?
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 25 (24 by maintainers)
Commits related to this issue
- #2798 Ensure scoped ApplicationContext uses correct context manager — committed to rockfordlhotka/csla by rockfordlhotka 2 years ago
- #2798 ApplicationContext selects context manager based on IsValid — committed to rockfordlhotka/csla by rockfordlhotka 2 years ago
- #2798 Allow a null HttpContext value — committed to rockfordlhotka/csla by rockfordlhotka 2 years ago
- #2798 fix appcontext tests for blazor; — committed to swegele/csla by swegele 2 years ago
- Merge pull request #2 from swegele/2798-localproxy-fixtests #2798 some tweaks to appcontext tests for blazor; — committed to rockfordlhotka/csla by rockfordlhotka 2 years ago
- #2808 #2798 Update cloning of criteria/object to use scoped provider — committed to rockfordlhotka/csla by rockfordlhotka 2 years ago
- #2798 #2808 Set DI provider/context for LocalProxy scope and return values — committed to rockfordlhotka/csla by rockfordlhotka 2 years ago
- #2798 #2808 Set/reset ApplicationContext in object graph and result — committed to rockfordlhotka/csla by rockfordlhotka 2 years ago
- #2798 #2808 Improve how LocalProxy iterates through the object graph — committed to rockfordlhotka/csla by rockfordlhotka 2 years ago
- #2798 #2808 Implement IManageProperties in ManageObjectBase — committed to rockfordlhotka/csla by rockfordlhotka 2 years ago
- #2798 #2808 Handle unloaded lazy loading property case — committed to rockfordlhotka/csla by rockfordlhotka 2 years ago
- #2798 #2808 Fix condition check — committed to rockfordlhotka/csla by rockfordlhotka 2 years ago
Actually, pause on that (if you were getting ready to hit the red button).
I’m running my tests now and seeing some weird behavior with:
[UpdateChild]method[Execute]method where theApplicationContext.LogicalExecutionLocationis not being properly set to “Server”. Trying to see if I can pinpoint what is going on.PREACH IT!! I’m seeing the fallout of their weird decision play out over and over online in articles/examples/guidance and in 3rd party component developers too. It’s dangerous that they provided it as NOT NULL. Either it shouldn’t be available or it should be null. The way it is just taunts newbs to try it and do bad things.
I just implemented my SSO engine in Blazor. I do complicated things and cannot use their middle-ware completely. I noticed that the federated ClaimsPrincipal from AzureAD is present and perfect in the User property on the Razor side (_Host.cshtml) Then immediately when you check it in Blazor (app.razor) the User property is unauthenticated. Obviously their middle-ware does some magic to get it across. But my point is HttpContext is truly untrustworthy in the circuit just like they say. So to your point…WHY LEAVE A LOADED GUN LYING AROUND! (eyeroll)