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]