views:

383

answers:

2

For testing I have 1 isolated page - no masters, controls, …. My sessions are lost after about 30 seconds. I’ve tried setting timeout on the page itself, in web.config, both, and neither. Tried forms authentication with timeout and windows authentication. Recycle the AppPool after changes.

I can response.write from the Session_Start , but I never get any response.writes from the Session_End.

Some things I’ve tried:

<sessionState mode="InProc"
  stateConnectionString="tcpip=127.0.0.1:42424"
  sqlConnectionString="data source=127.0.0.1;"
   cookieless="false"
   timeout="20" />

<sessionState mode="InProc" cookieless="false" timeout="20"/>

<sessionState mode="InProc" timeout="20"/>

<sessionState timeout="20"/>

No luck.

My runtime is set to:

<httpRuntime useFullyQualifiedRedirectUrl="true" 
maxRequestLength="204800" 
requestLengthDiskThreshold="204800" 
executionTimeout="600" />

I don’t know what this would be relevant, but I can’t think of anything else to post!

Thanks!

A: 

How do you know your Session is being lost? Do you have cookies enabled in your browser?

EDIT:
Here is a much, much simpler test page:

<body>
    <form id="form1" runat="server">
    <div>
        Session value is: <%= Session["testvalue"] %><br />
        <asp:TextBox ID="txtText" runat="server"></asp:TextBox>
        <asp:Button ID="btnSet" runat="server" Text="Set" OnClick="btnSet_Click" /><br />
        <asp:Button ID="btnRefresh" runat="server" Text="Refresh" />
    </div>
    </form>
</body>

And the code behind:

public partial class SessionTest : System.Web.UI.Page
{
    protected void btnSet_Click(object sender, EventArgs e)
    {
        Session["testvalue"] = txtText.Text;
    }
}

Another possibility is that you are losing your sessions due to App domain restarts. Add some sort of logging output to Application_Start.

Bryan
I am using a simple test. On the page, I set the session variable on a button click. On the Page_load, I print the contents of the session variable. It prints fine for a few refreshes. If I wait about 30 seconds and refresh, I get nothing.
shxo
The cookies work; the session does not: protected void Page_Load(object sender, EventArgs e) { Literal1.Text = ":: " + HttpContext.Current.Request.Cookies["test"].Value + " "; Literal1.Text += "<br><br>:: " + (string)HttpContext.Current.Session["test"] + " "; } protected void Button1_Click(object sender, EventArgs e) { HttpContext.Current.Session["test"] = Convert.ToString( DateTime.Now); HttpContext.Current.Response.Cookies["test"].Value = "TEST"; HttpContext.Current.Response.Cookies["test"].Expires = DateTime.Now.AddMinutes(20);}
shxo
Try checking the Session cookies ASP.NET generates, not create your own. You can do this with Fiddler, Firebug, or just checking the "Cookie" header while debugging your ASP.NET page.
C. Dragon 76
Yes.. let's simplify your test page. Get rid of all the Cookies stuff. This could be a ViewState issue also, since you are checking the session by adding text to Literal1.Text... and this value is stored in ViewState.
Bryan
shxo
Update: App RestartingMy Application_Start is logging a new application. My App is restarting. I am going to red the links provided by Joshua.Any other info on preventing App restarts?Thanks!
shxo
+1  A: 

If you're doing inproc sessions (which the snippet says you are) and something keeps touching the virtual folder or anything below it be prepared to lose lots of sessions.

If this is the case, this is the fix:

'This is supposed to turn off the monitoring for directory deletes
'See https://connect.microsoft.com/VisualStudio/feedback/Workaround.aspx?FeedbackID=240686
'This incurrs the penelty of an IISRESET or manually restarting the containing AppPool after every upgrade.
Dim pi As PropertyInfo
Dim o As Object
Dim m As MethodInfo
pi = GetType(System.Web.HttpRuntime).GetProperty("FileChangesMonitor", BindingFlags.NonPublic Or BindingFlags.Public Or BindingFlags.Static)
o = pi.GetValue(Nothing, Nothing)
m = o.GetType().GetMethod("Stop", BindingFlags.Instance Or BindingFlags.NonPublic)
m.Invoke(o, New Object() {})
Joshua
Thanks Joshua. My App is indeed restarting. I'll have a look at your links. Thanks
shxo
I'm on a shared remote and my trust level wont allow this code. Any ideas?
shxo
I hope I'm not speaking too soon, but this solution appears to have worked. I say “appears” because the page errored when I attempted to run the code; however, the site is now holding sessions (fingers crossed)
shxo
You tried to run that on a shared site. Shudder. When doing code like this, you *must* have at least a dedicated AppPool because somebody else probably depends on this not being broken.
Joshua
Yes this is trouble waiting to happen. You need to figure out what is writing to your app folders and causing the reset. This is just a hack.
Bryan
@Bryan when I used this my own code was the writer. My code actually needed to manipulate *data* files within the wwwroot and not lose its sessions.
Joshua