ASP.NET Application Pool Recycling Issue - asp.net

I have a Web Project setup that has both my WebForms and a WCF service. I am having an issue where every so often my application seems to recycle and i lose all singleton objects and session values. It does not appear to be a timeout issue, but maybe a memory leak of some kind. We can be using the app for a minute or so and then bam it just loses everything.
I have tried monitoring the directory for changes but found no changes to the filesystem at all. I also put a breakpoint in Application_Error and there are no errors being thrown.
I have been googling for two days trying to resolve this issue. The application is a direct duplication of a previous project that is working fine. The one thing I noticed that is different is my last project I used nHibernate for the backend and this project I switched to using Linq to SQL. I noticed that I wasn't handling the DataContext properly because I was diming an isntance of the DataContext inside my service calls and returning a value before ever disposing or setting the context back to nothing, so I figured it may not be closing. I tried instead of storing the datacontext in my "repository" class i stored it inside the operationcontext and explicitly dispose it on Application_EndRequest. That still hasn't resolved the issue.
Anyone have any suggestions or places I should look?
** UPDATE **: I believe i found my issue. I have objects that are using EntitySet and i am serializing those objects directly back using a serializable IList property with a linq query returning the list. When i tried to explicitly dispose of my datacontext before returning the data I am running into issues serializing those EntitySet's now because the datacontext is no longer active. I am going to try copying the data into a new blank object with regular Lists instead of EntitySet's and see if this will allow me to properly close my datacontext and resolve my issue.

Update is really a different question, but you really should not try and serialize stuff that comes off of an ORM -- lots of potential nightmares. Build yourself some DTOs.

In the IIS7 Console, select the specific application pool, and select "Recycling..." in the action pane to the left. It's a wizard that lets you define how the application pool recycles, and how it logs the recycle events to the event log (page 2 of the wizard).

Related

ASP.net Web API Singleton doesn't work

I'v used many ways to keep an object instance alive to share it's data between request.But All the methods even dependency injection doesn't work at all.
Finally I've realized that my App get recycled by every request and the reason was I wrote some log files in bin folder.So if you make any change in your bin directory, IIS will recycle your application.

Set HttpContext.Current.Application from Console Application

I'm in process of creating a Console Application, which needs to invode methods that heavily dependent on HttpContext. I could simulate the base HttpContext as well as the authentication, but I fail to set HttpContext.Current.Application. Is there any possibility to set the data in it?
There were couple of question on the same subject overe here, but couldn't get a solution from either, and thus ended up posting another one here.
My target is to get this working.
HttpContext.Current.Application("PageDefinitionCache") = pageDefinitions
Any suggestions, please?
The cache lives inside the ASP.NET worker process, you cannot access it directly from a console application. HttpContext.Current is null since you don't use the aspx.net worker process in a console application.
Consider declaring a global variable in the console application to simulate the behavior of the HttpContext.Current.Application.
you can not use HttpContext in Console application.
as mentioned by #matrxRapture you can resolve this issue by using global variables and if you are looking for current directory path for the application you can use Assembly.GetExecutingAssembly().Location

Keeping data in memory and persisting on Application_Disposed

I'm building a website (for personal use, low load) and instead of using an Access or MySQL database for data storage I'm thinking of having one XML file that I load and parse on Application_Start and then keep in memory (in static objects). The website then do reads and writes against these in-memory objects and I will finally persist all data to the XML file on Application_Disposed.
I'm aware that I'll need to make reading/writing thread-safe, but besides that, does anyone see any problem using this approach?
Yes, I see a big problem: There are a number of reasons to why the whole application might die without you knowing about it, and without your data being saved to that xml file.
You'll find Application_Dispose can get fired multiple times (so might don't be the best place to dispose your DI containers etc) whereas Application_End will only fires once (you can prove this by adding logging)
https://bytes.com/topic/asp-net/answers/561768-event-sequence
https://learn.microsoft.com/en-us/previous-versions/ms178473(v=vs.140)?redirectedfrom=MSDN
It seems VS2019 IIS Express doesn't seem to call Application_End as it should when you stop debugging, But IIS will.

Keeping tables cached on server in ASP.NET project

I have an ASP.NET project which is a front-end to a database. In addition to the large tables, the DB contains a few small tables to help normalize the larger tables with common values. I have a VB.NET project which loads the smaller tables into memory, using "Shared" (i.e., "static" in C#) member variables, and uses them. I have a call to load the tables in Global.asax - Application_Start. This works for a while. That is, Application_Start runs when I first run my project, loads the cached values, and will correctly keep them in memory for a while.
What I'm seeing (when running my project via Visual Studio 2008 Debugger, hosted locally) is:
A) The Application_Start code will run more than once. Not in a row, but after the user has navigated to some other pages, I'll see (my breakpoint in) another call to initialize the cache, coming form Application_Start. Is it expected?
B) The "Shared" variable that was set to True when the cache was initialized is now False again (which should only happen when the class is first loaded). Similarly, all the data that was chached is no longer present. That is, it looks like VB is unloading all the Shared members. Is this expected?
If these are the expected behaviors, is there a way to do what I want? The code is in a module that is also used by other (non-ASP.NET) projects, and seems to work correctly for them. I'd rather not have to duplicate this functionality for something specific to ASP.NET, but would like to know what my options are. Thanks for any advice.
Here is an article you might find helpful about Caching Data at Application Startup. It sounds like you are doing everything right, but Application_Start should only get called once, unless some external change happens that restarts the app pool, but in this case i would think you would get detached from the debugger (assuming you are attached to the app pool process for your asp.net application).

Flush cached data from IIS?

I'm being asked to look into a problem that occurs intermittently on a WebServer running my team's application.
Essentially, we have a webservice that does a lookup between codes. If you have Code Type A, you can use it to look up the corresponding Code Type B. Periodically, when memory is running low, when this webservice is called, a null reference exception is being thrown. Essentially, this service loads a lookup file into cache with a dependency on the file, so if the file chages, the cache is reloaded with the new file. The priority on the cache object is set to default. I'm guessing that somewhere in the code, it isn't being verified that the cache object is still there and when memory on the server gets low, that object is dumped causing the error. I'd like to be able to recreate the error and verify before I start digging into this code.
Is there a way in IIS manager (or from the command prompt) to force a running web app to dump it's cache? I would think that this should recreate the condition and therefore recreate the bug. Not to mention, seeing the detail error should lead to the right section of code.
Thanks,
Steve Brouillard
My gut reaction would be to set the WebMethod's CacheDuration to zero, then back to whatever you want on an ongoing basis. I haven't tried this, but I think this would dump the cache then start it forming again...
I found a utility that can be added to ASP.NET apps that will allow you to dynamically manage the cache as a whole or individual cache objects. Thanks to .NET Rocks! and dnrtv.
Here's a link to the tool that I used. This allowed me to clear just the specific objects in question, on the fly, and prove the error.
Thanks to everyone for your help. ASP Alliance Cache Manager.
Steve

Resources