Run job on ASP.NET website service start in IIS - asp.net

I have some method that is invoked on Application_Start. And it starts on first page request by any user.
But i have another application, dependent on that task that fires during Application_Start.
So that if IIS/website is restarted- another application cant get some data from website, until someone requests page on the running website.
Is there a way to run some task on website start; without any interaction from user side, just to start website in IIS and do the job i want?
PS more details on implementation architecture:
Website starts an instance of messaging service and starts sending messages through that service(that is located on another server). I know that it would have been better to create wcf or windows service for this kind of things, but someone wrote this website before i was included in this project. So clients(that receive messages from messaging server) do not know anything about website. Here website is just administrative panel to turn on/off some messaging features.
My Solution
Created windows service for my task. Works well, under local system account has no problems with writing to windows event logs.

Check out this "The Dangers of Implementing Recurring Background Tasks In ASP.NET"
Also you should consider installing Quartz.NET as a service, so that you can setup scheduled tasks remotely.

You can try this.
Put an entry in your code into event log
Use the windows task scheduler to create a task
Create a trigger as "Begin the task On an event"
Create the action with the job you want

Related

ASP.NET sync long process w/ Requirements

I am working with an e-commerce platform, and I have a task to synchronize with some remote accounting software. The task requires syncing orders, products, inventory...etc. With large amounts of data being synced,the process can take awhile. So, I don't think asp.net application would be the best place to handle this. So, the requirements are:
To be able to schedule this process to run overnight
To be able to manually fire off this process and pass into it some variables like order numbers to export.
Possibly get back status info when fired off manually.
Has to work on .net 3.5
Issues: Can't use a windows service because the site is hosted remotely on a shared service, and the host won't allow a service.
Ideas: I'm having a really hard time finding the best way to handle this outside asp.net that fits all requirements, but I do have access to their FTP and thought possibly a console app that hosts a web-service may work, and I can put Quartz scheduler in global file to fire off service from the site.
Anyway, please offer some thoughts and experiences if you have them on which methods have worked for you.
Can't use a windows service because the site is hosted remotely on a shared service, and the host won't allow a service.
That might be a problem. Does this hosting service provide any other kind of scheduling functionality? If not then you may need to consider changing your hosting services.
You're correct in that ASP.NET is not the tool you'd use for scheduling tasks. A web application is a request/response system (and is very much at the mercy of the hosting process, IIS usually for ASP.NET). So you need some way to schedule the task to execute at regular intervals. Windows Services, Windows Task Scheduler, or some other task scheduling tool.
As for the requirement to be able to invoke the process manually, that's a simple matter of separating the invocation of the logic from the logic itself. Picture the following components:
A module which performs the logic, not bound to any UI or any way of invoking it. Basically a Class Library project (or part of one).
A Windows Service or Console Application which references the Class Library and invokes the logic.
A Web Application which references the Class Library and invokes the logic.
Once you've sorted out how to schedule the Console Application, just schedule it and it's all set. If the process returns some information then the Console Application can also perform any notifications necessary to inform people of that information.
The Web Application can then also have an interface somewhere to invoke the process manually. Since the process "can take a while" then of course you won't want the interface to wait for it to complete. This can result in timeouts and leave the system in an unknown state. Instead you'd want to return the UI to the user indicating that the process has started (or been queued) and that they will be notified with the results when it completes. There are a couple of options for this...
You can use a BackgroundWorker to actually invoke the process. When the process completes, send a notification to the user who invoked it.
You can write a record to a database table to "queue" the process and have something like a Windows Service or scheduled Console Application (same scenario as above) which regularly polls that table for queued tasks, performs the task, and sends the notification. (Of course updating the status in the table along the way so it doesn't perform it twice.)
There are pros and cons either way, it's really up to you how you'd like to proceed. Ultimately you're looking at two main things here:
Separate the logic itself from the scheduling/invocation of the logic.
Utilize a scheduling system to schedule tasks. (If your hosting provider doesn't have one, find one that does.)

How to send mail at a certain time from asp.net web application

I am new to asp.net. I have a project working. I need to send different mails every day at 12 am to different users. I need to know how to implement the method that will wake up every day at a certain period. Please help me with as much details possible as I am a complete beginner
Use Quartz.NET.
Quartz.NET is a full-featured, open source enterprise job scheduling system written in .NET platform that can be used from smallest apps to large scale enterprise systems.
You can use Windows Task Scheduler as well, but you need some specific windows permissions to do that on production server.
You can create a Windows Service that will be executed automatically at the time you want. The windows service should contain the code to send email.You can check out these links on Simple Windows Service Sample and Simple Windows Service which sends auto Email alerts. You can also implement a timer in your application that can manage it. If you want to do it this way then check this article.

how to hide a complete asp.net webform

I want a page to run in the background in my Asp .Net web application.
That page should not be visible to the user.
The exact use of this page: the user will schedule a mail, that is to be send later. After he scheduled, the page should be hidden.
Can we do it?
Platform version (.NET 4)
What you really want is a service.
However, there are several kludgy ways to do back ground tasks with asp.net
http://www.codeproject.com/Articles/12117/Simulate-a-Windows-Service-using-ASP-NET-to-run-sc
http://www.west-wind.com/weblog/posts/2007/May/10/Forcing-an-ASPNET-Application-to-stay-alive
http://www.mikesdotnetting.com/Article/129/Simple-task-Scheduling-using-Global.asax
If the user closes the browser before your scheduled event has occurred, it will never take place.
You really want a backend service that processed queued events. When the user schedules an email it adds it to the queue and then gets picked up and processed by the backend service.
http://quartznet.sourceforge.net/ is one option for it, or you could build a window's service and the queue manually.
Alternatively you could look into a service bus approach such as http://www.nservicebus.com/ which is backed by MSMQ.

How do I implement Quartz.Net to schedule tasks for my MVC3 web app?

I'm building a project to send messages to users. The client wants a way to schedule these messages to be sent out at a certain time, for example, he creates the message at 2am but wants it to be sent out at 10am without his intervention, where do I begin with this sort of thing? I'm using ASP.NET MVC3, any help is appreciated.
Update
Darin has suggested Quartz.net, I've finally gotten around to attempting to set it up. But I'm not really understanding how to implement it with my web app.
I'm assuming I should be able to make an httprequest from my service to an action on my webapp, triggered by quartz. But I'm not sure how to communicate between the webapp and this service, such as sending instructions to the quartz server.
So far, I've created a windows service, set up the installers, and added the Quartz.net server 2010 solution to my service project, am I on the right track?
Using a managed Windows Service with Quartz.NET or a console application which you would schedule with the Windows task scheduler seems like a good approaches to achieve that.
Welp, there are scheduled tasks... either make a localhost request at a specific time, or write an executable/service to be called.
A possible alternative if you can't use scheduled tasks (but may be dependent upon the website being available to the Internet) is to make a remote request-maker program or use a website monitoring service. Have either of those make a request to a particular page/querystring on your site periodically. Then, make the web application perform a particular task (send an email) whenever that resource is requested.
A few free monitoring services are limited to one request every hour or half-hour, or you can pay to have it checked more often. The resource's code could be made to record the message-sending event, (thus making them only get sent once, regardless of how often the request is made)

Polling Long Running Windows Service from ASP.NET

We have an application that uses Lucene.NET within a windows service to reindex our app for search. Our admin section can trigger a full reindex in Lucene, and currently the only way to review its progress is through a log file written to disc.
This is clunky. We'd like to poll the service to determine the reindexing progress.
Does anyone have any insight into this?
Named pipes would be the way I would do cross process communication in this instance, if both processes would be running on the same machine.
If both processes are on different machines, it gets hairier and will probably involve something along the lines of a web service communicating with the process and then asp.net calling the web service.

Resources