Hi guys.
I have implemented Luca’s advice for Session Statics in our solution, which works great. ( https://wisej.com/support/question/shared-variables-and-methods )
However, I have noticed that if I make reference to any of its static variables from inside an Async Method, the StaticsInstance for the referenced “key” returns “null” although it had been already initialized. This way all previously set variables return null and after the Async runs, they are all null for the entire application.
Does this make sense or am I doing a wrong use of this technique?
Thanks in advance.
Ivan
(Wisej 2.2.44.0 – C# – SQLServer)
The ThreadBegin and ThreadEnd events are not fired by regular threads (it’s a wisej event fired by wisej threads). Regular threads don’t fire any event. Set StaticsInstance = null before accessing so it’s restored from the session. Or use the Application.Session directly.
Hi Ivan, the socket callback is coming from a different thread and it’s out of context. The task in Application.StartTask() preserves the context because Wisej installs its synchronization manager, but a socket event is a different thread. Try this:
Application.RunInContext(this, () => { your code });
or, if you also need to update the browser:
Application.Update(this, () => { your code });
Hi Luca.
Thanks for the reply!
Unfortunately it didn’t appear to make a difference, even using RunInContext all static values are still returning “null” inside the ASync method. Maybe I will have to change my Socket connection workflow.
This is my “Statics”:
public static class OSEStaticsHelper
{
static OSEStaticsHelper()
{
Wisej.Web.Application.ThreadBegin += OSEWjStaticsHelper_ThreadCleanUp;
Wisej.Web.Application.ThreadEnd += OSEWjStaticsHelper_ThreadCleanUp;
}
[ThreadStatic]
private static StaticsWrapper StaticsInstance;
private static void OSEWjStaticsHelper_ThreadCleanUp(object sender, EventArgs e)
{
StaticsInstance = null;
}
public static StaticsWrapper Statics
{
get
{
if (StaticsInstance == null)
{
string key = typeof(StaticsWrapper).FullName;
StaticsInstance = Wisej.Web.Application.Session.key;
if (StaticsInstance == null)
{
StaticsInstance = new StaticsWrapper();
Wisej.Web.Application.Session.key = StaticsInstance;
}
}
return StaticsInstance;
}
}
public class StaticsWrapper
{
public string ApplicationKey;
...
}
}
Please login first to submit.