I'm using XSockets to communicate between an ASP.NET website and some server. When I receive a callback from the server via XSockets I would like to notify the frontend of the ASP.NET website using Response()
.
My main Default.aspx.cs
page looks like so:
// I want to access all these variables in the callback method
public HttpContext ctx;
public HttpSessionState session;
public int _theNumber;
public static CommHandler _commHandler
private void Page_Load(object sender, System.EventArgs e)
{
_theNumber = 4; // Random number
ctx = HttpContext.Current;
_commHandler = new CommHandler( ...
aPromptCallback: PromptCallback
...) //The callback, amongst othr parameters
session = HttpContext.Current.Session; //Try to access this in callback
HttpContext.Current.Session["SomeClass"] = _someObject;
}
The CommHandler
basically sends data to some server using XSockets and expects (asynchronously) a reply with some data.
In the same Default.aspx.cs
I have a function for a button click defined as below:
protected void Deploy(object sender, EventArgs e)
{
_commHandler.SendData()
}
The CommHandler makes use of the XSockets Send()
method to send the data. When the CommHandler receives data a callback inside it is triggered which calls the PromptCallback()
Now the PromptCallback()
is defined like so in the same Default.aspx.cs
file:
protected void PromptCallback(string aStr)
{
var v = _theNumber; // This works as expected, the _theNumber is 4
var j = (SomeClass)session["SomeClass"] // I can still access the session..
ctx.Response.Write(aStr); // This does not work, amazingly ctx is now null
//HttpContext.Current.Response.Write(aStr); // This does not work either
}
Why is it that the _theNumber
is still accessible at the callback, but the HttpContext is null by then?
(I get the idea that the lifetime of the callback may outlive the lifetime of the HttpContext
, but why is the session
/_theNumber
accessible still there?)
I've read similar questions (1,2) / articles (1,2) but am still uncertain on how to solve this.