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

  1. LocalProxy is being used
  2. 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:

  1. Create a new scoped service called “LocalProxyState” which has a bool property “NewScopeExists”.
  2. See about making an abstract base class called ApplicationContextManagerFacade which implements IContextManager. This can then be used for 3 below.
  3. Make each IContextManager implementation where IsStatefulRuntime=true be a class that inherits from ApplicationContextManagerFacade, and which is a facade wrapper with an ActiveContextManager just like the current Csla.AspNetCore.ApplicationContextManager class.
  4. Each of these facade context managers needs to use the LocalProxyState in their CTOR to know if they should create the ActiveContextManager as a ApplicationContextManagerAsyncLocal

SECOND:

  1. Immediately after LocalProxy creates the new scope, it will fetch a fresh instance of this service var lpScopState = provider.GetRequiredService<LocalProxyState>() then it sets lpScopeState.NewScopeExists= true
  2. LocalProxy should then use DI to get it’s IContextManager and 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

Most upvoted comments

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:

  • Child objects in [UpdateChild] method
  • Command objects in [Execute] method where the ApplicationContext.LogicalExecutionLocation is not being properly set to “Server”. Trying to see if I can pinpoint what is going on.

I think Microsoft is really messing with us here, by providing an “invalid” HttpContext. I’m not 100% sure we should be building a workaround for what seems to be a bug in aspnetcore itself. Specifically, if they are saying that they’ll provide us with an HttpContext, and then telling us not to use the very instance they created and provided - then they shouldn’t be providing us with an instance that is invalid.

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)