Why does my application take two sessionIDs? - asp.net

In a web application, I need the SessionID for some reason, so, I save it in the database. the application does two redirects before it starts, and I found that it acquires two SessionIDs. The second one remains.
Why this behavior ? Any idea? How can I prevent that to save in the DB one record.
Sorry because I can't post code, it's compound with another logic.

There's a million possible reasons for losing / re-starting your session. Without your code, its difficult to offer advice.
One thing you can try on your own is to use the Session_End event in your global.asax file, as long as you're using inProc session. Put some code in there to have it tell you when the session ends, so you can track-down the problem / what's causing it to end in your application.
Another think you can look at is your method of redirecting. Make sure you're preserving the session, like this: Response.Redirect("~/default.aspx", false).

Another possibility is that you might not be putting anything into your first session. By default, you'll get a new sessionID on each postback unless you put something in the session.

Related

SignalR and SqlDependency refreshment issue - ASP.NET

I have a table in MSSQL database, and I have an ASPX page, I need to push all new rows to the page in a descending order. I found this awesome tutorial which is using SignalR and SqlDependency and it shows only the last row descarding the previous rows which have been added when I'm online, it does that because it has a span element to show data and every time it overwrites this span, so I modified the JavaScript code to append the new data and it works fine.
The problem now is when I refreshed the page for the first time, I'll get the new rows twice, and if I refreshed the page again I'll get the new rows triple .. and so on.
The only solution is to close the application and reopen it again, it looks like reset the IIS.
So, what can I do to avoid duplicating data in the online show?
It is not a SignalR issue, that happens because the mentioned tutorial has a series of mistakes, the most evident being the fact that it continuously creates SqlDependency instances but then it trashes them without never unsubscribing from the OnChange event. You should start by adding something like this:
SqlDependency dependency = sender as SqlDependency;
dependency.OnChange -= dependency_OnChange;
before calling SendNotifications inside your event handler. Check this for some inspiration.
UPDATE (previous answer not fully accurate but kept in place for context)
The main problem here is that this technique creates a sort of auto-regenerating infinite sequence of SqlDependencies from inside instances of Web Forms pages, which makes them unreachable as soon as you page has finished rendering. This means, once your page lifecycle is complete and the page is rendered, the chain of dependencies stays alive and keeps on working even if the page instance which created has finished its cycle. The event handler also keeps the page instance alive even if unreachable, causing a memory leak.
The only way you can control this is actually to generate these chains somewhere else, for example within a static type you can call passing some unique identifier (maybe a combination of page name and username? that depends on your logic). At the first call it will do what currently happens in your current code, but as soon as you do another call with the same parameters it will do nothing, hence the previously created chain will go on being the only one notifying, with no duplicate calls.
It's just a suggestion, there would be many possible solutions, but you need to understand the original problem and the fact that it is practically impossible to remove those chains of auto-regenerating dependencies if you don't find a way to keep track of them and create them only when necessary. I hope that part is clear.
PS: this behavior is very similar to what you get sometimes with event handlers getting leaked and keeping alive objects which should be killed, this is what fooled me with the previous answer. It is in a way a similar problem (leaked objects), but with a totally different cause. The tutorial you follow does not clarify that and brings you to this situation where phantom code keeps on executing and memory is lost.
I got it, although I don't like this way absolutely, I have declared a static member in the Global.asax file and in the Page_Load event I checked its value, if it was true don't run a new instance of SqlDependency, otherwise run it.
if (!Global.PageIsFired)
{
Global.PageIsFired = true;
SqlDependency.Stop(ConfigurationManager.ConnectionStrings["db"].ConnectionString);
SqlDependency.Start(ConfigurationManager.ConnectionStrings["db"].ConnectionString);
SendNotifications();
}
Dear #Wasp,
Your last update helped me a lot to understand the problem, so thank you so much for your time and prompt support.
Dear #dyatchenko,
Thanks a lot for your comments, it was very useful too.

Asp.Net MVC weird error when filling a really big form

On a new website, I've an huge formular(meaning really big, needs at least 15-20min to finish it), that configure the whole website for one client for the next year.
It's distributed between several tabs(it's a wizard). Every time we go to the next tab, it makes a regular(non ajax) call to the server that generate the next "page". The previous informations are stored in the session(an object with a custom binder).
Everything was working fine until we test it today with all real data. Real data needs reflexion, work to find correct elements, ... And it takes times.
The problem we got is that the View receive a Model partialy empty. The session duration is set to 1440 minutes(and in IIS too). For now what I know is that I get a NullException the first time I try to access the Model into my view.
I'm checking the controller since something like 1 hour, but it's just impossible it gives a null model. If I put all those data very fast, I don't have any problem(but it's random data).
For now I did only manage to reproduce this problem on the IIS server, and I'm checking elmah logs to debug it, so it's not so easy to reproduce it.
Have you just any idea about how should I debug this? I'm a little lost here
I think you should assume session does not offer reliable persistence. I am not sure about details but I guess it will start freeing some elements when it exceeds its memory limit.
You will be safer if you use database to store that information or you could introduce your own implementation for persisting state.
in addition to ans provided by #Ufuk
you can easily send an ajax request every 1 minute which would actually do nothing but by doing this the session wont get expired and site will continue to run in extended periods
The problem was that the sessions wasn't having enough space I think. I resolved temporary my problem by restarting the application pool. Still searching a solution that will not implies to changes all this code. Maybe with other mode of session states, but I need to make my models serializable.

Check once per user session - asp.net

I would like to make a call to the underlying backend just one time when user first initiates a user session. While the session is up, the same call shall never be made again. Do I just create a singleton tied to the particular session or is there a better way to do this?
You can place the method call in the global.asax Session_Start event.
List of available events is listed here.
You can always use the Begin_Session Session_Start method in the global.asax. This will fire only once and only at the beginning of the session. It will fire before any authentication occurs, though.
Edit: Begin_Session? Sorry. Code bleed in my brain. I've corrected the actual function name above. The bleed was for a proprietary project I had active in my noggin when I was reading the question.

asp.net session dissappers

I have a strange problem:
I have a site which has an administration system.
In the system there is a way to edit my inputs. To make this easier, I can search for their ids to find them.
The problem is when the page posts back the session variable I hold the "logged in" value in, becomes nothing.. and i get kicked out. Why is that happening? Session variables should hold at least 20 minutes if anything else is stated?
EDIT: It works for a coworker, but not for me.. Also, it only does it on certain inputs.
EDIT2: Turns out i get an exception. but it doesnt say what, only says "property evaluation failed". i get it on this code row:
Response.Redirect("./admin.aspx?search=" + u.FirstOrDefault.ProductID.ToString, False)
And sience it goes through this statement, its not because it is an objectreference not set to an instance of an object
If u.Any Then
If you experience an exception your session might be restarted.
Create a global.asax and set breakpoints to the Application_Error, Session_End and Session_Start events to track down the issue
Edit based on your update:
Make sure that u.Count == 1, because FirstOrDefault will throw an exception, if the count is > 1.
Have a look at this link http://support.microsoft.com/kb/312629/EN-US/ which should cover your original problem
http://forums.asp.net/t/1296202.aspx/1
If you have Web Garden enabled (multiple worker processes for an application pool) this can explain such behavior - been there.
Either have one worker process for the pool, or switch to Database driven Session.
Do you have cookies enabled? If not, this would explain this behavior. Especially as you state it works for the co-worker, you can deduce it's a client-side issue.

Classic ASP, application variables, refreshing

I have an application variable which is populated onstart (in this case it is an array). Ideally I need to rebuild this array every 3 hours, what is the best way of going about this?
Thanks, R.
Save the time you last refreshed the variable contents.
On every request, check the current time against the saved time. If there's a three hour difference, lock and refresh the variable.
As long as there are no requests, the variable also needs no refreshing.
If your application variable must remain "in process" with the rest of the site's code, the way suggested by Tomalak may be your only way of achieving this.
However, if it's possible that the application variable could effectively reside "out of process" of the website's ASP code (although still accessible by it), you may be able to utilise a different (and perhaps slightly better) approach.
Please see "ASP 101: Getting Scripts to Run on a Schedule" for the details.
Tomalak's method is effectively Method 1 in the article, whilst Method's 2 & 3 offer different ways of achieving what is effectively something happening on a schedule, and avoid the potentially redundant checking with every HTTP request.

Resources