How do I restart an ASP.NET application immediately after running IISRESET? - c#

I have windows task which restarts IIS at midnight 00:00. In my application there is a background thread which runs a global refresh at around 02:00.
My problem is that the application starts only on the first request from a browser. This may not occur for quite some time and the global refresh can be late in starting.
Is there any way to start the application without first browsing to the web application?

Ideally you should keep maintenance tasks such as this separate from your web application (either as a scheduled task or Windows service).
But, if you really need to do it this way could create a batch file that does:
iisreset /restart
"C:\Program Files\GnuWin32\bin\wget.exe" -O nul http://www.myapp.com/default.aspx
Then run this batch file as your scheduled task at 12:00. This will restart IIS and warm up your application.
You can get GNU wget.exe from:
WGET for Windows (SourceForge)

You can have another task that accesses your web site after IIS is restarted.
Still, I can't see why would you have a thread doing maintenance inside your IIS worker process. If the process dies from some reason (for example - because of the recycling configuration in the web site's application pool) the work won't get done. It's better to do this from a separate process, such as windows service or a scheduled windows task.

You shouldn't have any threads scheduled inside an IIS web application - becasue IIS has some logic to recycle the worker process and your application when it is not used. Its better to run it as a separate application (scheduled separately).

You could also use a Powershell script called by task manager. Here is simple six-line script we use to "warm up" SharePoint servers.
You could repurpose or find a similar script for a basic .NET application.

Related

Process.Start Hanging, Not Starting Process

I have an api running in IIS written in .net framework 4.7.1 that needs to start a console app, but when I call Process.Start(#"\\boxName\d$\moreFoldersHere\FileName.exe"), it hangs on execution, and the console app does not start. I have verified that the app pool under which the site is running has full perms to both the directory containing the exe and the exe itself, and I have made the service account an administrator on the box with no luck. I have no trouble running the app, but even when I set the app pool to run as myself, the behavior does not change.
Purely for context... the api needs to trigger a process that may take an unreasonable amount of time to complete. Obviously I don't want the api response waiting on it, so I don't want to wait for the result of the process; I just want to fire-and-forget. I also don't want iis cleanup killing it after some period of "no activity", and I need to potentially have multiple instances of this same process executing at the same time on the same box.
I found the answer... In IIS, in the App pool, LoadUserProfile needs to be set to True.
I'm not sure why it defaulted to False, but everything is working fine now.
Since it's a console app, your issue is likely the need to read the output. See this answer.

Run ASP.net c# Web Application Automatically

I have a ASP.net web application that checks the status of my servers, it then wraps all this information up and puts it in a email. My Question how do I run this automatically say every day at like 2:00am, or like every 12 Hours?
Thanks
The best solution is to create a simple MS Windows Service which will do this job.
You'd better implement this as a separate process from your ASP.NET application. Phil Haack has summarized the reasons in this blog post. A Windows service for example or even a console application using the windows scheduler could work just fine for this task.
You want a scheduler - I recommend Quartz.NET.
As others have said, your code doesn't have to be in a web app.
If it is, then schedule a job that uses WebClient to make a request to your web app.
Check out WebDriver.
It's intended as a test / qa framework, but there's nothing stopping you from using it in a console application, which you can then run as a Scheduled Task. Note that whatever machine runs the Scheduled Task has to have a browser that WebDriver can fire up.
The easiest solution would be to create a scheduled task on your server using the Windows Task Scheduler and setup this job so that it uses internet explorer to visit your webpage.
If you open the task scheduler and create a new task. In the "Run" field put:
"C:\Program Files\Internet Explorer\IEXPLORE.EXE""http://www.yoursite.com/yourpage.aspx"
Then in the "Start in" field put:
"C:\Program Files\Internet Explorer"
Now configure this task to run at 2:00am every day.

Quartz on a lightly loaded server

I have several important Quartz events that MUST go off at specific times of the night. Lately I have been noticing that not all the events are run. I have a feeling that overnight our server load is very light (ie. zero users) and that the web server kind of goes to sleep, and hence so does Quartz. Does this seem plausible? I am using Quartz.net within the web server, and not as a separate service.
Yes, that is plausible. In general, it is considered a bad practice to have IIS run scheduled tasks, that is a job better left for a Windows Service, or the built-in Windows Scheduled tasks (which has been much improved for Windows Server 2008).
Your worker process might have been shut down because there are no load. By default, IIS shuts down worker processes after 20 minutes without ingoing requests (you can alter this in the Application Pool settings). Also, worker processes are likely to be restarted after a certain amount of time or requests, or if they consume too much memory.
A quick-fix for your specific problem might be to use the Windows Task Scheduler to request the site periodically to keep it alive - or have it request a URL that triggers your task at the predefined time.
ASP.Net, by default, will shut down AppDomain's after a period of inactivity.
The recommended course of action is to implement timed events either in a Windows Service or as an executable launched from Window's Scheduled Tasks.
It is also possible to change the IIS configuration so that it will not shut down your AppDomain. How exactly this is accomplished varies between versions of IIS, but instructions can easily be found by searching.

Can you configure IIS7 to autostart a Windows Process Activation Service (WAS) application when the application pool starts/recycles?

IIS 7.5 introduces the notion of auto-start providers, that allow you to get WAS to auto-load an application or assemblies when an application pool starts up.
Can a similar thing be achieved with IIS7?
Basically, we have an application that runs under WAS, and has an in-memory cache of data. When an application pool recycle occurs, my WAS deployed app won't actually be activated until the first hit for it is received. This means that the cache is cold when the first hit is received. It would be good to be able to pre-start the application as soon as the app pool is recycled.
Other options we've considered are:
Deploying the application as a Windows service so it doesn't re-cycle (this would work, but the application lifecycle management of IIS/WAS is a useful thing apart from this issue)
Writing a separate service whose job is to ping our application to warm it up.
However, the nicest way would be to get IIS7/WAS to do this for us.
In Windows 2008 you can log events that occur on the application pool, so you can log recycle events.
You can configure the event viewer to start a program when a specified message has been logged. You could call your service or load the assemblies in that program.
Would this be a feasible solution for you?
Regards,
Michel
in the advanced settings of your application pool set your application pool to generate Recycle event log entry every time it is recycled; I think the option is "Specific Time". Then you can use Windows Task Scheduler, create a script or something you want it to run that will hit your site so it can initialize. Set the trigger for the task to an even, set the Event Filter that you want to trigger the task and voila.

Check if IIS Application is running without causing it to run

I am writing a web monitor app that gives information about all app pools / apps on my IIS server. I am looking for a way in C# to programmatically check if an IIS application is running without causing it to run if it is not.
Here is what I have found...
application is running, there are one or more sessions active.
all sessions dropped off...application still "running", app pool worker process is running.
At some point determined by IIS, the Application_End is finally executed...however, the app pool worker process is still running.
After 20 minutes, the app pool worker process finally shuts down due to inactivity.
It is between #3 and #4 that I am having trouble. The application has ended, but if I try to send an Http Request to the app, it will automatically start up. I am looking for a way to programmatically determine if the application has ended (while the worker process is still active awaiting shutdown) without restarting the Application.
I can try to provide more details if you are unsure what I am talking about.
Regards,
Jeremy
I found this on MSDN
public bool CheckIISRunning()
{
ServiceController controller = new ServiceController("W3SVC");
return controller.Status == ServiceControllerStatus.Running;
}
For this to work you need to have
System.ServiceProcess added as a reference.
Microsoft has created a VBScript file to identify the IIS Application Pools (by Process Id). If you search "iisapp.vbs" on google it should point you in the right direction.
Note: I'm not 100% sure that it will work with Windows Server 2008, but does work with 2003.
In IIS7 you can use appcmd apppool /? to see what possibilities are available.

Categories

Resources