Cannot keep alive Web Application on IIS after Recycling or Restarting - c#

I have an MVC application that send an e-mail periodically using Quartz.NET. Although the published application works properly on IIS, it cannot works after recycling application pool or restarting the application on IIS. Searching on the web I found several post suggesting to make some changes on config files or IIS, but none of them working properly. So, is there any method to solve the problem? I need a solution that can be applied on application side (or on IIS side if it is simple just making a config changes, etc.). I think this is a common problem when keeping an application on IIS, isn't it?
Note: I use Windows Server 2008 and IIS 7.5.

You do not mention in your question where your application is going to run so I guess it's going to be hosted in-house.
Following your comment I gather you do not have any problems installing and running a Windows Service on your server.
My suggestion - and something I've implemented in the past - is to use the ASP.NET MVC application only as a UI where you create, delete or suspend your jobs/triggers which will be persisted in a database so, whatever happens to your application, you won't lose your jobs/triggers and they will be executed as soon as the application goes back on-line.
The database will be shared with the other layer, the Windows Service, which will be responsible for running your scheduled jobs.
First step is to setup and use AdoJobStore to store Quartz.Net data. As you can see in the post there are a few providers you can use.
Second step is to create and configure your Windows Service. I would use TopShelf to host it. The implementation is very simple and straightforward; plus you can use the Quartz.Net integration provided here.
If you go through the documentation you won't find any problem integrating the solution.
Quartz.Net depends on some configuration you have to add in your app.config/web.config. In this answer there's a detail explanation about the configuration and AdoJobStore.
There are a few things to remember implementing this type of solution.
Your Web Application is going to set the property threadPool to ZeroSizeThreadPool in your config:
<add key="quartz.threadPool.type" value="Quartz.Simpl.ZeroSizeThreadPool, Quartz" />
or in your code:
properties["quartz.threadPool.type"] = "Quartz.Simpl.ZeroSizeThreadPool, Quartz";
and it's never going to start the Scheduler (your windows service is going to use that).

Related

IIS Application Initialization with IIS 10.0 and WebAPI

Hello and good morning everyone,
I ran into an issue where the services once started, are no longer subscribing to the service discovery. In testing they were running under IIS Express. However once I flipped it over to IIS and verified everything was still working; the service no longer hits the Application_Start() within the Global.asax.cs.
Doing a bit of research, it appears that IIS requires a little configuration to get this to do some initialization work once the Application Pool is started. I am running this in a Windows 10 Professional environment with IIS 10.0.17763.1
The goal: Get the service starting up to subscribe to the service discovery was it was running under IIS Express.
I first found on the MSDN the documentation for setting all of this up including prerequisites here IIS 8.0 Application Initialization
After going through all the steps and updating the required config entries, it appears I am still not Initializing my web service when the Application Pool starts.
I have created a new endpoint on the controller for testing purposes to get this to work.
AccountController:
[System.Web.Http.HttpGet]
public IHttpActionResult Init()
{
// here is where I am registering the service thats coming up
}
Route Definition file:
{"Name": "Account - Init",
"RouteTemplate": "api/{controller}/init",
"Defaults": "controller = AccountController, action = Init"}
Web.config
<applicationInitialization skipManagedModules="false" doAppInitAfterRestart="true" remapManagedRequestsTo="/api/account/init">
<add initializationPage="/api/account/init" />
</applicationInitialization>
I am under the assumption that the Initialization will only happen ONCE, and that happens when the Application Pool first starts, or is recycled. IIS internally will make a 'managed' call to the Web Application against the mapped resource it finds in the application's web.config file. Is my understanding of how it should be working incorrect? If so, could someone please clarify why?
I am not simply trying to 'get this to work'. I would like to understand why its 'not' currently working for me.
Thanks to all in advanced who take the time to respond here.
that is correct, your web.config will only be read once when the application starts.
you can add some logging messages to see it happen.
You can actually hit debugger points if you manage to connect your Visual studio to the application pool of the IIS process in time. There is an option to attach the debugger to a process, so you can use that.
This is painfully obvious in systems where the AppSettings are in a separate file, not web.config. if you make a change, nothing will happen, until you touch web.config which triggers an application restart.
Following up,
I fixed my own problem. It appears the code IS working correctly, that its hitting the Global.asax.cs Application_Start as it should. I had an exception being thrown not related to IIS after a change I made which was resulting in what appeared to "not work".
Following the instructions on the MSDN does work.

Deploy a backend application on a Windows Server

I'm making an application in C# with VS 2012 that checks a database every 15 seconds and perform some actions when it finds data. Right now I've created a Console Application so I can debug it easely but during relese this application needs to run in a IIS server.
How can I do that? I've read this question but it looks like some sort of workaround because to run it I need to perform these steps. Right now I'm reading the docs about Windows Service Application, Is this the right way?
EDIT Sorry but I've never used Windows server before, so as people pointed out IIS is only a web server, the thing I need to do is run my application in a Windows Server environment
IIS is a web-server and accordingly it should be used for hosting web applications.
Develop a windows service which does the job of checking the database in intervals and invoke a web service (which you can host in IIS)
If your application is performing some data query and manipulation on the server then I would recommend the approach to host it in a windows service.
Some advantages to this are:
The service will start and run independently of a user logging into the server.
You can configure the service to recover should it experience an exception (ideally not!).
The service will start automatically (if configured) when the server restarts.
You can configure which user group (or user) the service should run under so you have a more granual approach to security.
As it's running as a seperate process, you can monitor its memory and processor utilisation.
Debugging is slightly more cumbersome but not difficult, one approach I've used is to install the service locally, start it and then attach to it via the debugger. This question describes another approach I've also used.

Permanent data gathering

I have database and mvc application hosted on iis. I periodicaly gather data from internet and save them in sql database. And i calculate statistic and graphs from these data and publish them in mvc application.
Problem is that iis have recycling period about 1 hour -> meaning that my timer(function) which gather data from interenet is stoped whenever there is server restart, recycling or there is no request on the web page.
solutions i have found are:
turn of recycling - i don't own srv can't do that.
windows service - 99% hostings don't allow host ws...
So is there any solution, service, framework, which purpose is to gather data and i can be sure that it will not stop after some inactivity time or server restart? or is my logic completely wrong and i need to gather data diferently? can it be done on hosting which i don't own? can it be done using iis?
can it be done using iis?
If the IIS in question has app fabric installed, then that supports an auto start feature, which effectively lets you write 'service like' code which will keep running in the background.
Quick overview here

Which technology to use for running crawler and updating database in asp.net website?

I am developing a project for college and i need some suggestions over the development. Its a website which shows information from other websites like Links, Images etc.
I have prepared below given model for the website.
A Home.aspx page which shows data from tables (sql server).
I have coded a crawler (in c#) which can crawl (fetch data) required website data.
I want some way through which i can run the crawler at back end for some time interval and it can insert updates in the tables. I want that i can get updated information in my database so that Home.aspx shows updated info. (Its like smaller version of Google News website)
I want to host the wesbite in Shared Hosted Environment (i.e a 3rd party hosting provider company and that may use IIS platform)
I posted simliar situation to different .NET forums and communities and they suggested lot of different things such as
Create a web service (is it really necessary ?)
Use WCF
Create a Console application and run windows task sheduler (is it okay with asp.net (win forms website) and in shared hosted)
Run crawler on local machine and update database accordingly. (No i want everything online) etc etc
Please suggest me a clear way out so that i complete the task. Please suggest elobrated technology and methods which suits my project.
Waiting...
Thanks...
Your shared host constraint really impacts on technologies restrictions.
In theory, the best way to host your crawler would have been a Windows service, since you can take advantage of windows services configuration. A service is always up, can be automatically started at startup, writes errors in event log, can be automatically restarted after failure...
Then, you Home.aspx would have been a regular website in IIS.
If you really stay on a shared host (where you cannot setup a service), I would have make the crawler as a module which is run on your application startup.
Problem is, IIS application pool doesnt live forever if your web site is not in use, and it may stop the crawler. It is configurable, but I dont know how much in a shared host.
In IIS 7.5, think about starting your module at application warm up
Finally if you need to run the crawler at interval times (like every day at midnight), if your shared host does not let you set task scheduling, think about Quartz Framework, which allow you perform task scheduling inside your application (without the intervention of the OS)
Integrate your crawler code into a aspx page
Setup a task scheduler on your host to call that page every X minutes
When the page is called check that localhost has called the page
If localhost called it run the crawl routine and
If localhost hasn't called it throw a 404 eror

IIS vs Windows Service?

I have a C# application that needs to always be running. I originally planned on making this a windows service but I now have a requirement to make the application host a web admin console.
I haven't played with IIS in quite a few years so my question is this:
What would you recommend I use?
I've thought about making a windows service and embedding a web server such as Cassini but so far I'm not very happy with the open source web servers I've looked at.
Can IIS handle this? Do people use it for this type of scenario, and if so how?
This sounds like a job for two separate projects.
One is the original Windows Service. Windows Services are well suited for what you're doing.
The second is the Web Project that will be used to administer the Windows Service. This is the part that runs in IIS.
It depends on what you mean by always running. An ASP.NET web application deployed in IIS could very well be unloaded by the web server if there aren't any requests for certain amount of time killing all background threads. So if you want an ever running background thread it would be better suited to use a Windows Service. As far as the web admin is concerned, well, here you don't have much choice: ASP.NET in IIS. In order to do something useful those two applications should be able to find a common language to talk. So you could use a database to store the results into which could be used by both applications.
IIS will run your app on first request, not on server boot. So you will still need to run a service to ensure your app is always running.
You can use IIS as a webserver for your web admin part, and link your ASP.net app with your service by means of a configuration database (easy) or webservices (a little more tricky).
Windows and Web services are two very different creatures. A web service will expose external methods that you can implement against an application, while a windows service is an entity within itself. If you're planning on using this service on a timed interval to perform an operation, a Windows service would be the right way to go. If you use a web service, you will need to invoke the method you wish to run from a secondary application.
If you need to queue commands against your windows service, you could always create a database that was accessible by both your website and your windows service. This way you could send commands and query data between the two. Placing a web service in to serve as an intermidary between the two may be overkill.

Categories