Does the asp.net life cycle continue if I close the browser in the middle of processing?

I have an ASP.NET webpage that connects to multiple databases and uses multiple files. I don’t understand what will happen if the end user closes the web page before it finishes loading, i.e. Will the ASP.NET life cycle end or is the server still trying to create the page and return it to the client? I have a reasonable knowledge of the life cycle, but I can not find any documentation on this issue.

I am trying to find a potential memory leak. I am trying to establish whether all the code will be launched, i.e. whether the connection will be established, etc.

+7
source share
3 answers

The code will still work. The HttpRequest object has an IsClientConnected property that can indicate if the client is still connected if you perform operations such as streaming output in a loop.

+6
source

As soon as the request for the page is generated, it will proceed to the unloading on the life cycle. He does not know that the client does not exist until he sends the upload information.

A unique aspect of this is the Dynamic Compilation section. You can read here: http://msdn.microsoft.com/en-us/library/ms366723

For more information on the ASP.NET lifecycle, see here: http://msdn.microsoft.com/en-us/library/ms178472.aspx#general_page_lifecycle_stages

So basically the page is requested, ASP.NET uses dynamic compilation to basically create the page, and then tries to send the page to the client. All code will run in what you specify in the code, regardless of whether there is a client or not.

This is a very simplified answer, but these are the basics. Your code is compiled, the request generates a response, then a response is sent. It does not go to pieces unless you have explicitly indicated this.

Edit: Thanks to Chris Lively for recommending a rewording.

+3
source

You mentioned tracking potential memory leaks and the word “connection”. I am going to suggest that you mean a database connection.

You should ALWAYS wrap all your connections and commands into using . This ensures that the connection / command is deleted correctly, regardless of whether an error occurred, disconnecting the client, etc.

There are many examples here, but it comes down to something like:

 using (SqlConnection conn = new SqlConnection(connStr)) { using (SqlCommand cmd = new SqlCommand(conn)) { // do something here. } } 

If for some reason your code does not allow you to do it this way, then I suggest the next thing you do is restructure it, as you did it wrong. A common problem is that some people will create a connection object at the top of the page and then reuse it for the life of the page. This is guaranteed to lead to problems, including but not limited to: errors in the connection pool, memory loss, random requests, complete removal of the application ...

Don't worry about performance when establishing (and dropping) connections where you need them in your code. Windows uses a connection pool, which is lightning fast and will support connections for as long as necessary, even if your application signals about it.

Also note: you must use this template EVERY TIME using an unmanaged class. They always implement IDisposable .

+2
source

All Articles