The case of the ApplicationContext.ClientContext disappearance

The case of the ApplicationContext.ClientContext disappearance

Old forum URL: forums.lhotka.net/forums/t/12029.aspx


jteneglio posted on Monday, June 17, 2013

It seems that the content of ApplicationContext.ClientContext disappear .  During the login proess i put the user id in the clientContext, then i use my implementation of IAuthorizeDataPortal to recreate the user and get the stuff i need from it.   Now this is working fine for a couple of back and forth and than suddenly no more userID in the ClientContext, this can happen at any time, i there seems to have no pattern.

 

By reading the previous post, a recurring anwser is make sure you have the csla web in the folder, and i do.

 

i'm using Win8 with CSLA Core.4.5.30. on .net 4.5

JonnyBee replied on Monday, June 17, 2013

Which UI technology do you use? 

jteneglio replied on Tuesday, June 18, 2013

We are using xaml and,c#

JonnyBee replied on Tuesday, June 18, 2013

Hi,

For WPF you must make sure that Csla.Xaml.dll is present in your bin folder. 

jteneglio replied on Tuesday, June 18, 2013

We are using XAML C#  on  Windows 8  and we do have CSLA.xaml.dll in the .Net for Windows Store Apps 

jteneglio replied on Thursday, June 20, 2013

When i say windows 8 i mean Windows Store Apps and we do have the csla.xaml in the Windows Store App referecnces

RockfordLhotka replied on Friday, June 21, 2013

Is the ClientContext being lost on the client, or is it just not making it to the server via the data portal?

jteneglio replied on Friday, June 21, 2013

seems to have been lost on the client as well :-\

jteneglio replied on Sunday, June 30, 2013

it seems to be happening only or more during development then when running the application.

RockfordLhotka replied on Sunday, June 30, 2013

In WinRT the application context values are stored in a static fields, so they are global to the app. This does mean that changes to the values on background threads are global to the app. It also means that if the appdomain is reset somehow that the values would be lost. I don't know how _that_ would happen in WinRT though.

JonnyBee replied on Sunday, June 30, 2013

Hi,

I kinda suspect there is at least 2 other possible explanations as having applicationcontext in static variables breaks the DataPortal logic for SetContext  / ClearContext when running in local mode. 

1. Making 2 async DataPortal calls in parallel will make the second call start with ExecutionLocations.Server and then call  ClearContext when complete. 

2. Calling f.ex a new DataPortal.Fetch method from within another DataPortal.Fetch will make the second call start  with ExecutionLocations.Server and then call  ClearContext when complete.

RockfordLhotka replied on Sunday, June 30, 2013

You are probably right. And there's no easy solution because we have no access to TLS or ExecutionContext. As a result I do have an issue in the backlog on GitHub to develop my own equivalent to ExecutionContext for use in WinRT. I guess the priority of that issue should be elevated... 

JonnyBee replied on Sunday, June 30, 2013

Been digging into the code and it does not seem to be the issue. 

There is an extra check for context.IsRemote before the ApplicationContext.Clear() so I am not able to recreate the issue.

jteneglio replied on Thursday, May 21, 2015

after all these years Wink, i'm still having the issue, as you said JonnyBee it seem to be happening a lot more when i do a DataPortal.Fetch inside another DataPortal.Fetch but it is very strange, as it does not happen all the time and at the same place????

 

 

jteneglio replied on Friday, May 22, 2015

By digging a bit more it seem to be happening when there is more than one Dataportal call coming from the WinRT app at some point it looses the ApplicationContext.ClientContext in the backend ???? 

jteneglio replied on Wednesday, June 03, 2015

Digging more into this issue i realized that CSLA seems to be dropping the ClientContext when calling a DataPortal inside another DataPortal 

For Example:

Client Code

Csla.ApplicationContext.ClientContext[Key] = "SomeData"

Csla.ApplicationContext.ClientContext[Key2] = "SomeOtherData"

 

DataPortal.Fetch(some criteria)

 

Server Code 

DataPortal_Fetch(some criteria) 

{

    Csla.ApplicationContext.ClientContext  has 2 item

    Get Data from a database

     DataPortal.FetchChild<someChildObject>(some criteria));

}

 

SomChildObject() 

{

 private void Child_Fetch(somecriteria)

{

    Csla.ApplicationContext.ClientContext  is empty (Sometimes)

}

 

Anybody seen this before?

JonnyBee replied on Thursday, June 04, 2015

Hi,

Are you sure the ClientContext is till present after data access from database? 

If you have async or tasks in the data access that would explain how the code could be running on another thread and hence has lost the application context. 

DataPortal.FetchChild does NOT use task or async and does not interact with ApplicationContext. 

The semenatics of DataPortal.FetchChild is: 

 

Another culprit may be if you have async/tasks in one of the other methods that may be called. 

 

jteneglio replied on Thursday, June 04, 2015

Givin that how do I get it back? I need the clientcontext all the way down to the DataPortal.FetchChild

I do have Async Tasks in some methods and I do need the client context, can I do something to keep it?

RockfordLhotka replied on Thursday, June 04, 2015

The clientcontext is attached to the primary thread. If you somehow make a call that ends up running on another thread it is up to you to copy the clientcontext from the original thread to the new thread.

A typical async/await call won't actually jump threads btw - but if you do Task.Run or something along that line the result would be a new thread.

jteneglio replied on Thursday, June 04, 2015

I'm Only using Async/Await and by adding the CSLA code to MY project to see what's going on, I see that at the beginning of the CSLA stack the ClentContext is there but than a bit later it disappear for some reason.

I'll send some screen shot that shows what Im saying

JonnyBee replied on Thursday, June 04, 2015

Hi, 

Do you use the Csla.Threading.CslaTaskScheduler when you run tasks? 

jteneglio replied on Sunday, June 07, 2015

As you can see in the picture I do have the ClientContext.

Here is the steps

1. I use DataPortal_Fetch to get my Invoice

2. I Add a Part on it, doing this triggers an Async Business rule

2.1 That calls a DataPortal_Execute (a Command)

2.2 That command Load the part again (DataPortal_Fetch) but this time in edit mode so it can set some missign attributes

       When I'm Loading the properties of the last Fetch - The context is lost, 

 

In the picture included here you can see the CSLA call stack.

 

Am I doing something wrong?

 

jteneglio replied on Thursday, June 11, 2015

I'm using a lot of Async business Rules and it seems to be happening more in them!!!

RockfordLhotka replied on Thursday, June 11, 2015

All your code (1 through 2.2) is 100% server-side?

jteneglio replied on Thursday, June 11, 2015

1. Is from the UI , It call the server to get the list of Invoices

but the rest is all server side.

RockfordLhotka replied on Friday, June 12, 2015

So your Part object is a child of something like Invoice.PartList, but is also a root object in that it has a DataPortal_Fetch method (not just a Child_Fetch)?

RockfordLhotka replied on Friday, June 12, 2015

Also, is the async rule that is run in the Part object a per-type or per-property rule? What triggers this rule to run when the object is created?

jteneglio replied on Sunday, June 14, 2015

These rules are per-property rules.

We use  BusinessRules.CheckRules(PartTypeProperty);

So it loads a list immediately for the user to select, to set another property.

JonnyBee replied on Sunday, June 14, 2015

So you use use a rule to do lazy loading of a property? 

jteneglio replied on Sunday, June 14, 2015

The setting of a property load a list for the user to be able to set another property.

 

Property A is set -> triggers a rule to load the possible choices for Property B

 

Hope that clarifies

RockfordLhotka replied on Monday, June 15, 2015

I'm not sure I see how this scenario gets you to loading a child object as a root? What do you do, load it, pull the collection from it, and then discard it?

Why not just load the filtered collection directly?

RockfordLhotka replied on Monday, June 15, 2015

As I start to move the forum to GitHub, I'll move active threads there via link. Here's the link to continue this thread:

https://github.com/MarimerLLC/cslaforum/issues/3

 

jteneglio replied on Sunday, June 14, 2015

That's right, we use it as both a child and a Root object.

Copyright (c) Marimer LLC