I have a process that involves a few .aspx pages. Fill out some info in one, hang on to it, move to another page that has an iframe on it, upload some documents in the iframe via a web service to a server, hang on to the list of documents and the status of the upload and then, on a third page, do something else that involves saving all the data to a database.
Before moving from page 1 to page 2, I put some data in Session variables. On page 2, the data is retrieved and displayed, some more data is put into Session variables during the process of uploading the files from within the page in the iframe on page 2 then, on page 3 the data is retrieved from Session and written to the database.
On a test server this all works perfectly. On a live server, I keep getting (random) 'object not set to a reference' errors - which seem to be reporting that the session variables have disappeared.
My understanding is that, within .aspx pages ...
HttpContext.Current.Session["myvariable"]
Session["myvariable"]
are, effectively, the same thing. I am setting my session variable just using ...
Session["Variable1"] = "fred";
Any ideas why (randomly, sometimes the process works fine on the live server) I seem to be losing my Session variables?
This is a web site as opposed to a web application. Developed in VS2010 using Framework 4.0
There can be various reasons why you are loosing the session.
Some of them are:
Your sessionState timeout has expired
You update your web.config or other file type that causes your Application Domain to recycle like files in folder App_Data
Your Application Pool gets recycled
You update your site with a lot of files just by doing copy and paste physically, and ASP.NET will automatically recycles the session.
If you are not sure of the reason you can do event logging why application pool is getting recycled. May be you will come to know about the reason and depending upon that you can take preventive measures.
For Logging you can write following code block on Application_End
public void Application_End()
{
HttpRuntime runtime = (HttpRuntime)typeof(System.Web.HttpRuntime).InvokeMember("_theRuntime", BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.GetField, null, null, null, CultureInfo.InvariantCulture);
if (runtime == null)
return;
string shutDownMessage = (string)runtime.GetType().InvokeMember("_shutDownMessage", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetField, null, runtime, null, CultureInfo.InvariantCulture);
string shutDownStack = (string)runtime.GetType().InvokeMember("_shutDownStack", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetField, null, runtime, null, CultureInfo.InvariantCulture);
//Do Logging as per your need and implementation
//Logging.Write(TraceEventType.Error, shutDownMessage, shutDownStack);
}
The is a good posiblity you save non serializable objects in you session variables and keep your session out of proc on your production server (load balancing?). Check if you objects are seriablizable and if not make them serializable.
What happens is your live server is recycling its application pools, which basically resets the memory used for applications. Normally a timeout can be set, but if the application pool recycles, this will reset your session.
The solution is to use SQL server for your session state.
Try this: http://support.microsoft.com/kb/317604
Supplemental Link: http://www.codeproject.com/Articles/104082/Configuring-ASP-session-state-on-SQL-server
If you are hosting with a larger public host, they have probably already prepped their SQL to handle session states, and you should be able to just make the change in your web.config file to use SQL session state.
Ciao
There is one more condition where sessions can lose its value.
You can use Fiddler tool to trace out this problem.
The most condition can be found when you some element like source not found in solution. At that moment server will try to reload that unfounded or lost object by restarting the project. Restarting the project will result in resetting all session objects.
Thanks.
Related
I have sample webapp deployed to Azure. The app cached a variable using MemoryCacheEntryOptions to store a value (from database) which expire in 5 minutes.
However after 5 minutes via Chrome debugging tool, I still can query the cache, the cache value expected to be empty or whatever the new value which currently stored updated in the database.
I even tried to clear cache in the web browser, but cache seem still retain the previous value.
However when I restart the web site, and open the web app again the cache value is no longer exist.
Would any setting in Azure might affect the cache expiry?
private readonly MemoryCacheEntryOptions _cacheEntryOptions;
protected CacheService(IMemoryCache memoryCache)
{
_ memoryCache = memoryCache;
_cacheEntryOptions = new MemoryCacheEntryOptions
{
AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(300)
};
}
Debugging the behavior of a web application is notoriously hard, as all you got to control it is the Browser - and you never get exclusive access.
Even if you did not refresh the page, any number of things might have queried the server. The culprits start around "any search engines webcrawler" and end around "somewhat aggressive security tools" (because some viruses might use web servers). You could try a way shorter timeout. But ideally you want to have both the Server and the client you access it with run in separate virtual machines, which are only connected via the Hypervisor. That way you can be certain nobody is interfering.
On my localhost server Session["Culture"] variable always store the proper value.
But on my external server, the same session variable (Session["Culture"]) sometimes lose value and become null. Why it is happen? And how to resolve it?
Part of a global.asax.cs file:
protected void Application_AcquireRequestState(object sender, EventArgs e)
{
if (HttpContext.Current.Session != null)
{
CultureInfo ci = (CultureInfo)this.Session["Culture"];
if(ci == null){
//breakpoint
}
}
}
EDIT:
It is obvious that session cannot be shared between server A and server B. My problem is rather different - in a nutshell- the same application, but:
1) on server A (localhost) Session["Culture"] works properly and it always store some information.
2) on server B (external) Session["Culture"] work nice but always after random time lose value and become null
In-process session state is subject to being cleared any time, as sessions are ended when the appdomain is recycled.
You can mitigate this some by using an external session storage mechanism, such as the state service, or a database provider. You can also build your own session state provider.
However; you should still treat session state as only somewhat less volatile than cache, if you want it to be robust; always check for null, and reset the values if so.
If sessions are expiring that quickly on one server, I would look into why that might be. Something may be causing recycles improperly.
Addition: I see you are using multiple servers; in-process session state also cannot handle this. The above advice can help there, too.
I create several temporary files in my asp.net app, and I need to delete them when the user is done using them.
I tried
void Session_End(object sender, EventArgs e)
{
System.IO.File.Delete(#"C:\Temp\test.doc");
}
but it's not working.
Any ideas?
you must grant permissions to iisuser at this folder
1) Set mode variable on web.config
<sessionState
mode="InProc"
stateConnectionString="tcpip=127.0.0.1:42424"
sqlConnectionString="data source=127.0.0.1;Trusted_Connection=yes"
cookieless="false"
timeout="20"
/>
2) Start a Session variable.
3) Put some code on Session_End and then check if that code is executed when the user click [X] on the browser.
Taken from the following
http://www.tek-tips.com/viewthread.cfm?qid=742667
Hope this helps
There is no good reliable (works 100% of the time) way to determine when a user leaves a web application. While conventional means will work when all is running as expected, there are edge cases that can happen that will orphan data, e.g.
The user's computer freezes or crashes...
The server freezes or crashes...
The user's internet connection fails...
The server's internet connection fails...
You get the idea. For this reason, I personally prefer to do all processing in memory instead of writing temporary files to the web server's hdd. That way the .Net garbage collector will handle cleaning up any orphaned data.
If, however, you are processing data in sufficiently large quantities to make keeping it in memory not a viable option, set up a Windows Service or scheduled task to perform cleanup once enough time has passed to ensure that the files are stale. Continue to attempt cleanup at runtime, but the alternate method should allow you to handle any data which becomes orphaned over time.
Some quick details:
I'm running ASP.NET 4.0.30319.0 on IIS6. I've been hosting a website that contains a ReportViewer for quite some time now (~3 years). In April, I upgraded to the 4.0 runtime, and things ran smoothly for a couple of months.
Now, I'm suddenly starting to see quite a Session Timeout exceptions occur on the pages hosting the ReportViewer. The Event Viewer has many many of these events logged, and it's rather hit or miss when it comes to actually being able to view the page. Hitting the page once, you can see the generated report. Refresh, and the error occurs. Refresh again and it returns...
I've scoured many forums trying to figure out the issue - most seem to recommend changing SQL server settings (which I'm not using), changing the AsyncRendering="False", changing the Application Pool settings, or the timeout. I'm reluctant to change any of these, as it has worked only a week ago, without this issue.
Short of a Windows Update, or someone making a change to the server without my knowledge, I'm out of ideas...
Update
I've tried increasing the maximum virtual memory, in the app pool, which didn't work.
I have almost the same problem, after upgrading to .NET 4.0 and Report Viewer 2010. I did both upgrades at the same time, now I'm not sure who's to blame. In my case, refresh does work, but the users keep the page open during the night, then click on refresh the next morning, when the session is already lost. Our app pool recycles every night.
I believe the report viewer is supposed to keep the session alive, but it doesn't. There's no request of any kind from the report viewer. It then loses it's stored state when the session ends, either by session expiration or app recycling. I'm also using InProc, I tried to change it, but the report viewer did not work with State Server. I will try again at a later time, to move away from InProc.
See my similar question.
I haven't put it into production yet, but I gave the aspx pages with the reports a custom page to derive from, and I will check there if the session has actually timed out. It basically reloads the report page, instead of doing postback where it expects the session.
if (Context.Session != null)
{
//Tested and the IsNewSession is more advanced then simply checking if
// a cookie is present, it does take into account a session timeout, because
// I tested a timeout and it did show as a new session
if (Session.IsNewSession)
{
// If it says it is a new session, but an existing cookie exists, then it must
// have timed out (can't use the cookie collection because even on first
// request it already contains the cookie (request and response
// seem to share the collection)
string cookieHeader = Request.Headers["Cookie"];
if ((null != cookieHeader) && (cookieHeader.IndexOf("ASP.NET_SessionId") >= 0))
{
Response.Redirect(Request.Url.ToString());
}
}
}
I have a simple web app built in asp.net webforms c#
where and how would be the best way to save info from the code behind?
(and also retrieve that info)
eg all i want to save is a dateTime. and a flag set to True or False.
and be able to access them in the code behind.
Im not using a db for this web app.
Edit: and can't really use session variables for this purpose.
thanks
If you have to save data for an arbitrary period of time, then you need to store it in a database.
If you need to read the saved variable after a lengthy period of time, you should store it in a local file or a database.
You can create a file using a FileStream object and then write your value to the file.
To get a path where your application has sufficient rights to write a file, use Server.MapPath.
Note : If possible, and if the data you store should not be available to users, you should configure your IIS WebSite to forbid him to serve this file to clients.
Use Application variable like Application["thedate"] = date; and you can get back as date = Application["thedate"].
The date will be saved untill app pool restarts (which also happens when IIS or system restarts).
For a more longer time, save in an xml file on the disk (Use XMLReader and XMLWriter for this purpose).
If this is per user info you could use either browser cookie or viewstate.
Use the Session object:
Session["thedate"] = date;
DateTime date = (DateTime)Session["thedate"];