How can I programmatically redesign my own .net web application?

I have a complex server application that uses Nhibernate and Linq2SQL. About 3 times a day, the Linq2sql code throws a "value cannot be null" exception. Once this happens, the code will always throw an exception. Diagnosis and resolution of the root cause will be lengthy and will lead to instability.

The current β€œfix” is that the application pool is being restored every hour. However, the service does not work, because the problem occurs until processing takes place. I want the web service to detect an exception and process its own application pool. I want all other web requests to be executed until they are executed.

Edit: Error on both servers in a load balanced web farm. Clients do not switch from one server to another just because this code fails.

+9
source share
5 answers

The following code will restart the current site application pool. You need to add a link to Microsoft.Web.Administration

using (ServerManager iisManager = new ServerManager()) { SiteCollection sites = iisManager.Sites; foreach (Site site in sites) { if (site.Name == HostingEnvironment.SiteName) { iisManager.ApplicationPools[site.Applications["/"].ApplicationPoolName].Recycle(); break; } } } 
+24
source

The easiest way to "debug" an ASP.NET workflow to recycle an application pool is to somehow modify the web.config file. This change is picked up by the file system watcher and causes ASP.NET to process to load the new configuration.

The contents of the file should not be altered in any practical way; just add or remove a space character.

Edit:

If this is not efficient enough to get around your problem, you can run the whole pig and use the directory services to manually use the application pool utility.

 // Set up the path identifying your application pool. var path = "IIS://YOURSERVERNAME/W3SVC/AppPools/YourAppPoolName"; // Create the directory entry to control the app pool var appPool = new DirectoryEntry(path); // Invoke the recycle action. appPool.Invoke("Recycle", null); 

Based on Code Project: IIS 6.0 software application pool programmatically .

+2
source

The last sentence was a killer.

To do this, you need to configure load balancing, which will be much more complicated than solving the original problem.

I would suggest you make some effort to resolve this exception. Please post some code from the relevant Linq request, maybe we can offer some suggestions.

0
source

I had the same problem; I had to blog about it, because now I can’t remember the main reason.

My gut tells me that it was one of the following:

  • The context where you are not located.
  • Problem with threads: make sure multiple threads cannot access the same context.

Or something like that. Just make sure you are using Linq2Sql correctly.

0
source

The solution that I ultimately chose was to set the maximum value for the amount of memory that appPool was allowed to consume. Then the code simply swallowed the memory until asp.net decided to recycle appPool.

In the advanced settings of the application pool, I set the limit of private memory to 800 000 Kb.

In the Catch section where the Linq code crashed, I allocate more memory than the limit:

 List<string> listOfMemory = new List<string>(); // in the app pool, you need to set the virtual memory limit to 800,000kb log.Error("Allocating so much memory that the app pool will be forced to recycle... "); for (int intCount = 1; intCount < 10000000; intCount++) { listOfMemory.Add("new string " + intCount.ToString()); } 

Now this means that only about 4 threads fail before a new w3wp process arrives. Prior to this decision, threads will successively fail until a person manually processes the application pool. Unfortunately, if you configured the application pool to be reused in the usual number of minutes, the fewer the number of minutes, the more often the crash occurs. And the more minutes, the more threads will fail.

This workaround limits damage.

0
source

Source: https://habr.com/ru/post/909472/


All Articles