How to expire session using jquery/javascript in my mvc applicatoin - session

In my application i m processing xml file differences. Some time when processing xml file differences the response is being hanged due to which some time i refresh the browser so that the loader will remove and processing will start again.
I want to expire session when such type of hanging problem occur? OR what other I do for removing such type of bug? Please suggest.
Please note that, i m using MVC 3.0 using fluent nhibernate.

What I would do is start a timer in java-script. when you call your function to process the xml differences start the timer and when it fires after X amount of milliseconds call an ajax function to your mvc controller where you expire your session via .net or set it to null. from their you can either redirect to a different view or the same one starting the process over or you can return a var to the javascript triggering the function to just be called again without a page refresh if that will work

Related

How can I set the view timeout?

In our JSF2 project on JBoss 7.1.1, we define a session timeout in the web.xml and it works just fine.
However, sometimes we're getting view expiration, leading to errors like this one even if the session is still alive:
javax.faces.application.ViewExpiredException: viewId:/... - View /... could
not be restored.
Where can we set the view timeout, like we did for sessions? Or is the view expiration caused by something else?
Another cause of ViewExpiredException is that too many logical views are been created in the session. The default limit is JSF implementation specific and every synchronous GET request on a particular view basically creates a new view. So, for example, when you use Mojarra (which has a default limit of 15) and start a browser session and open the same view in 16 different tabs and then submit a form in the 1st one, then you may get this exception as well. The limit is configureable with a JSF implementation specific context parameter, which is com.sun.faces.numberOfLogicalViews for Mojarra and org.apache.myfaces.NUMBER_OF_VIEWS_IN_SESSION in MyFaces (defaults to 20).
This is however a very rare real world problem. If your webapp is really designed to be used this way (e.g. a social/community site which invites to being opened in multiple tabs, such as discussion forum or Q&A), then you might consider using client side state saving instead.
See also:
javax.faces.application.ViewExpiredException: View could not be restored
com.sun.faces.numberOfViewsInSession vs com.sun.faces.numberOfLogicalViews

ASP.Net MVC: Store data in OnActionExecuting and retrieve it in OnActionExecuted in a controller

I want to add some code to log the time spent in each action call in a controller. I saw a suggestion of creating a Stopwatch inside the OnActionExecuting method of the controller and stoping it OnActionExecuted, which seems fine to me.
What I want to know is where do I have to add the started Stopwatch object so it can be read back once OnActionExecuted is called.
I was thinking on adding it to the Session, but I'm guessing this might have issues if there are simultaneous requests from the same session.
What is the best place to store this data?
Thanks
You can use HttpContext.Current.Items for objects that are related to a single request.
https://web.archive.org/web/20201202215202/https://www.4guysfromrolla.com/articles/060904-1.aspx

Is there a better way than using session variables to access an object anywhere in an application?

I am working with ASP.NET MVC 3.0. I have a page with an action link that makes appear a window in which you can adjust a certain value. Once the new value is sent to the database, an extern application deals with the value and send the result back to that database. I want the action link to be disabled while the extern application is doing her job. The page I am working on is refreshing automatically with some AJAX calls. The date when the extern application finished her last adjustment on a value is kept in the database. I first thought I could use session variables to store the date time of when the action link was pressed (because I need it through all the application) and then enable the action link when the adjust time is greater than the time when the action link was pressed, but I heard it was bad practices. Does someone have another solution?
Since you are already using the database - query the database to check the current status. If your application is restarted - a session value would be lost unless you are using a state server (ie sql server) to manage state- unless you don't care if its lost upon restart. You can use session and save yourself database calls - but the database is a bit cleaner and doesn't suffer from the same issue. If you do end up using the session, don't spread that session value all over your code, simply have a single method that reads or sets it (same with the db solution as well)

Run a JS method before / after every AJAX call

I'm using AJAX.NET including update panels, web service calls, client controls etc.
In may page I include 3rd party javascript file that makes keep-alive call for limited preconfigured amount of time (say 30 min). this script is my session keeper.
every time the user have an interaction with the browser - i want to run a method in this script to reset the counter for new 30 min.
Is there a place that is prior / later to all my ajax calls where i can put this method call instead doing that everywhere dozens of times?
Thanks.
I'm not sure if there is a way to catch every ajax call, but the handler below will fire after every asynchronous postback(eg updatepanel)
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(endRequestHandler)
More info here: http://msdn.microsoft.com/en-us/library/bb383810.aspx
As Chaotic_one mentioned, jQuery does have the concept of global events that fire before/after every ajax call, but I think that only refers to calls made using jQuery.
Well, you have to bind your AJAXing function to every browser event. Here is more about JS events. If you use jQuery, events will become much simplier.
But what I didn't get is why just don't make use of .NET Session class?

Best way to run a long DB query in classic ASP?

I have a couple of queries for a web site that take a long time to run due to the data model and the amount of data held in the tables. So far I've been running them manually against the database to avoid any timeout issues etc.. however the site owner has asked for these to be made available on the site so he can get the query results.
I had thought of doing this via a .NET web service and having the classic ASP page call this asynchronously. The web page would just initiate the process and before redirecting the user to another screen. The web service would then run the query and email the user the results in a CSV.
However, I can't seem to get this to work. The service runs ok if I invoke it through the screen in IE but calling it through an Ajax call in ASP seems to be an issue - no error is generated but neither is the CSV file created.
I've enclosed the classic ASP code below. The service only has one method with a parameter of the name email which is of the type string. Can anyone see anything wrong with it? Also, this the best way to be doing this or should I be thinking of another approach?
CODE
<%
message = "http://wwww.example.com/service/query.asmx/GetResults?email=test"
set req = server.createobject("MSXML2.XMLHTTP")
With req
.open "GET", message, False
.setRequestHeader "Content-Type", "text/xml"
.send
End With
works = req.responseText
response.redirect "http://www.bbc.co.uk"
%>
The idea of asynchronously requesting the work and arranging for its later delivery seems very reasonable to me. I don't speak ASP well enough to know what's wring with your attempt, but is that really an asnch call you have there? Would the seb service also suffer from an HTTP connection timeout?
My approach would have been for an Ajax request to place a request on a queue and return, no need for a redirect, you're still on the page where the user makes the request, your JavaScript could just acknowledge that the request was sent. Alternatively, your more traditional "submit a page, stash the request, display another page" appraoch can work, but the the stashing is just to put the request on a queue.
An advantage of the queueing approach is that by controlling the number of daemons we can get controlled parallelism in servicing the requests - avoid overloading the DB. Also the queues can persist and allow a leisurely delivery of the responses.
I assume that MS queues then let you have a daemon processing the reuquest and delivering the responses. Clearly email works, but strikes me as a tad unfriendly. With Ajax style interfaces it would be quite easy to invisibly poll for the status of requests and obtain the results when they are ready, or even to use Comet-style push delivery of the responses.
The problem here, as djna noted, is that you are not calling a callback function.
Due to the asynchronously aspect of Ajax, you have set up a callback function that will be executed when the Ajax call ends.
Long story short:
Call the webservice from a javascript function, preferably using JQuery to avoid cross browser incompatibilities
Code:
<div id="results">Processing query. Please wait</div>
<script type="text/javascript">
$(document).ready(function(){
$("#results").load("http://wwww.mywebsite.com/service/query.asmx/GetResults?email=test&Rnd=" + Math.random().toString());
});
</script>

Resources