[SOLVED] HttpContext.Current.Session is null

Answered Closed
1
0

Hello,

I uses a third party application framework, in which HttpContext.Current.Session is used to store system data like authentication, system variables ….

However in Wisej,  HttpContext.Current.Session is always null. Please advise how to resolve this issue?

Thank You.

 

  • You must to post comments
Best Answer
0
0

Hello Tung,

The issue is that the asynchronous handler in Wisej doesn’t require the AspNet session – it can handle simultaneous requests really really fast.

You can add this class to your app to require the AspNet session to be created:

namespace Wisej.Core
{
  public class HttpHandlerRequiresSession : Wisej.Core.HttpHandler, System.Web.SessionState.IRequiresSessionState
  {
    // no code required.
  }
}

Then modify your Web.config file like this:

  <add name="wisej" verb="*" path="*.wx" type="Wisej.Core.HttpHandlerRequiresSession, [Assembly Name]"/>

Now all Wisej HTTP requests can use HttpContext.Current.Session.

Notice that I bolded HTTP. That’s because the traditional AspNet session cannot possibly work with WebSocket requests. But you can solve this in two alternative ways:

  1. Disable WebSocket: add “enableWebSocket”: false to your Default.json. See https://docs.wisej.com/docs/concepts/configuration
  2.  Keep WebSocket enabled and in your Program.Main() add this:
           Application.Session.AspNetSession = HttpContext.Current.Session;

It saves the AspNet session with the Wisej session, which works with WebSocket.

Solution 1 works with the AspNet code that  you mentioned. Solution 2 won’t work since HttpContext.Current.Session for those library will always be null on a WebSocket call.

HTH

Best,

Luca

  • Luca (ITG)
    I forgot to add that you should be careful with AspNet sessions since they lock – a second request will block and wait until a previous request is done using the session. You can solve that using the IReadOnlySessionState instead of IRquiresSessionState. But a read-only session is read-only… That’s ASP.NET/MVC.
  • Tung Ngo
    Hello Luca, Thanks for your instruction on how to enable HttpContext.Current.Session. Is there any performance penalty of disabling Websocket ? BR,
  • Luca (ITG)
    There is a small performance penalty and you lose the ability to push updates to the client from background tasks or long running requests. If you need to push updates to the client, you can add a timer component to the page (window, form) that needs to be updated (update a progress bar, for example), set the timer to fire every few seconds and Wisej will be able to “push” the updates using http. Additionally, http has few hundred bytes of overhead for each request, while WebSocket doesn’t. In any case, running Wisej just with http is still a lot faster than ASP.NET/MVC.
  • Tung Ngo
    I’m exciting about capability of push data from server to Client. I have read that Signal R can do this. But not expect that Wisej supports this feature as well.
  • Luca (ITG)
    Wisej is built for WebSocket see https://wisej.com/docs/html/RealTimeWebApplications.htm. We don’t use SignalR – it’s a relative large library mostly built when WebSocket was a new technology not available consistently and it provided a uniform interface, plus long polling for when WebSocket is not available. Wisej uses the WebSocket feature directly, it also compresses large packets, and falls back to plain HTTP without long polling.
  • You must to post comments
0
0

Hi Chris,

I added an example.

You have to change the namespace.

With the new web.config entry you overwritte the default handler for wx.

I choose the second approach from Luca’s post and store the HttpContext.Current.Session in Application.Session.AspNetSession.

Best,

Jens

Attachment
  • You must to post comments
0
0

Hi , can you provide some more info on adding the namespace, where should I put it.

When I add it I get lots of errors from all the forms in the application as various events are missing, i guess its intefering with the normal wisej namespace.

Also

Where do you put this line in the webconfig , the handlers I am guessing , but does it replace the normal wisej handler ?

 <add name="wisej" verb="*" path="*.wx" type="Wisej.Core.HttpHandlerRequiresSession, [Assembly Name]"/>

Assembly name I presume is the fully qualified name of your project assembly ?

I am working in vb.net
  • You must to post comments
0
0

Hi Tung.

Just out of curiosity, is it possible to know the name of this third party application framework you mention?

  • You must to post comments
0
0

Hello Tung,

I think you can use Application.Session to dinamically define session variables:

string Name;
int Id;

Name = “Jack”;
Id = 125;

Application.Session.UserId = Id;
Application.Session.UserName = Name;

then use:

Application.Session.UserId
Application.Session.UserName

in your application forms or code.

Mariano

  • Tung Ngo
    Thanks Mariano, This approach does not work in my case, because the code, which uses HttpContext.Current.Session is in a third party dll.
  • You must to post comments
Showing 5 results