HttpContext.Current.Session is not saved in the Shared method.

I use some WebMethods in System.Web.UI.Page to handle some ajax requests, and I'm trying to save an object in a session to save information in multiple ajax requests and even after leaving the page and returning.

I access the session through HttpContext.Current.Session("foo") , and if it's nothing, I set it through HttpContext.Current.Session("foo") = New Bar() .

The problem that I encountered is that after it is installed, the next time I remove this method, it was reset. I tried to change some other session variables that are set elsewhere, and they are also returned. This tells me that I am getting a copy of the session state, not a link that I can update, and cause the request to be saved from the request. I double-checked that the EnableSession property for WebMethod is True, but changes to the session state are not saved.

Method Declaration:

 <System.Web.Services.WebMethod(EnableSession:=True)> Public Shared Function foo(ByVal woNum As String, ByVal workCenter as String) As ToolingModel Return ToolingModel.getSessionTooling(woNum, workCenter) End Function 

getSessionTooling:

 Public Shared Function getSessionTooling(woNum As String, workCenter As String) As ToolingModel Dim tooling As ToolingModel = HttpContext.Current.Session(TOOLING_MODEL_SESSION_KEY) If tooling Is Nothing Then tooling = New ToolingModel(woNum, workCenter) HttpContext.Current.Session(TOOLING_MODEL_SESSION_KEY) = tooling ElseIf tooling.woNum <> woNum OrElse tooling.workCenter <> workCenter Then tooling.woNum = woNum tooling.workCenter = workCenter tooling.assets.Clear() End If Return tooling End Function 

How can I make my changes apply to the session persistent state by accessing it from a shared web method?

Edit: found my problem, week too late. Line 1.aspx:

 <%@ Page Language="VB" AutoEventWireup="false" CodeFile="foo.aspx.vb" Inherits="foo" MaintainScrollPositionOnPostback="true" MasterPageFile="~/mstrFoot.master" EnableSessionState="ReadOnly" %> 

A read-only session was established on the page, which overrides the PageMethod EnableSession parameter. [Facepalm]

+5
source share
2 answers

IIS will use a multi-core processor by default. In a multiprocessor environment, the default InProc SessionState parameter in the web.config file randomly processes requests for different threads (individual threads per processor / kernel). More details here: https://msdn.microsoft.com/en-us/library/ms998549.aspx#scalenetchapt06_topic8

FYI, although I also did this myself in the past, saving session state between requests in the web API (SOAP / REST) ​​frowned, as it represents server overhead that is difficult to scale. Saving complex objects in session state also requires that every aspect of this object / properties / fields be serializable too, which can become very complex. Nonetheless...

Based on the information you provide, you need to configure your web application to use StateServer mode. And enable the ASP.NET public service on any machine so that multiple requests processed by separate threads support a session between processors. https://msdn.microsoft.com/en-us/library/ms178586.aspx

+1
source

If your session is not synchronized, the most likely reason is that ASP.NET generates a different session identifier between your web method requests and your calls (aspx?). You probably have access to the same session (if not timeouts and cookies) between two calls to web methods, but not through a page call and a web method.

Unable to get session in webmethod in asp.net

+1
source

Source: https://habr.com/ru/post/1261010/


All Articles