Run background task with console application from asp.net - asp.net

My requirement is to send emails within the web application from an external server that takes around 4 to 5 seconds on average to process and send the confirmation email. I do not want the user to wait for this, so i trigger a console application with Process().StartInfo.Start() and it does it in background. The question is how much i can rely on this as during normal days the application sends around 10 thousand emails daily and in high traffic days it may surpass 80 thousand. What possible issues/problems the application or server may run into? Is there any better solution for this?

You can use following method to achieve this:
Create a .aspx page and write you business code here (e.g. email
send)
Where required, call this page using Javascript along with parameters in querystring. You can create an image element and set its source to that page.
var img = new Image();
img.scr = "perform-operation.aspx?[parametervalues]";
Thank you

Console application has drawback, if you plan to invoke console application for each email separately, any new process takes long to load and clean up, this is too much overhead on cpu. Instead, the best alternative is to host another website in IIS, this new website will have its own Application Pool, which will create and host process. IIS will shutdown the process if no request is served. You can setup Web Farm by running more worker process per application.
Other alternative is to run console or windows service all the time, let it be in the memory but has a web service host listening for email requests, if a request is received from your web application, this background service will send email on new thread and go back on waiting state. Basically it is a self hosted WCF service. This is quite same as hosting another website on IIS.
I prefer IIS based hosting as it is easy to setup, does not require any extra permissions and in future, to scale horizontally, you can easily move this mail service to other server.

Related

Can IIS hosted WCF services perform background tasks?

I know that probably this question would be asked to many times but...
The wcf services hosted on iis7 in an asp. Net Web site can do things automatically? Like post a message to a pre-configured wall on Facebook given the permission to the application in a pre scheduled time?
For this to happen a client must send a request or it can do it alone?
The lifetime of a WCF service is typically determined by requests from a client i.e. if there is no client making requests then there is no service running.
Possible solutions:
Create a custom WCF ServiceHost, override the OnStart OnStop methods and create a background task.
Create an ASP.NET background task (external to WCF), which you can do with a library such as WebBackgrounder
Use the Windows Task Scheduler to trigger a task which polls your WCF service periodically which can then post outstanding messages to Facebook.

IIS & hosted ASP.Net application

Let's suppose I have an ASP.Net web application hosted by IIS. In addition to the functionality serving requests I added an action executed periodically on specified interval, e.g. every 30 minutes. This action is not related to external web requests, for example, its purpose is deleting useless log files.
The question is: if nobody comes to my web site for long period of time (day, week) will IIS still keep my application alive and the above action will be executed every 30 min. Or IIS will shut down/suspend activity of the application if there are no requests for long time?
Perhaps it is a lame question but I failed to find the definite answer.
IIS doesn't shut down, but IIS will recycle your app after a certain period of time of unuse.
So if your app is responsible for firing off the event, then no it won't run if your app is not running (recycled).
Either have a service hit your website once in awhile or setup a scheduled task on the server if you have access to the machine.
If you're on shared hosting you'll have to check their features to see if they have something like that.
Also, if you're just trying to delete log files or something trivial and not time sensitive (since you won't have logs unless someone is hitting your site), you could just perform your action in Application_Start. This even will fire whenever IIS restarts your application.

How to communicate between ASPX and WinForms

How can I send commands to a WinForms application from an ASPX web page? We have already explored executing the WinForms application with different command line parameters but wanted something more smart.
Thanks.
Create a web server within your application that listens for HTTP GET and/or POST commands and acts appropriately. Then use AJAX to send request, i.e., http://localhost/myapp/?command=print&file=teletubies.jpg
Your web server, which is just a program that listens on port 80 and sends responses according to the very simple HTTP protocol, within your application then parses the requested URL and decides that it should print the file teletubies.jpg based on the query string in the URL.
Web pages (whether running asp.net or a competing platform) are always reactive. They receive commands (requests) and respond. They are not proactive, and don't send commands. This is how the core technology on which the internet is built works.
This means is you want an asp.net page to send a message to a client app, the only way to do it is for the client app to frequently poll the page, possibly using System.Net.WebClient.
I don't know of any way this can be done due to security. I know I wouldn't want people to be able to access the running applications on MY machine from their web app.

How to trigger scheduled server side events from an ASP page

I have an asp (or aspx) page, it can get weather from webservice, and then send email or sms to me. But it is a web page, only when people visit, It will be activated. so I need a timer to scheduling to invoke it every day.
the code in the website hosting, so can't write winform, console application and windows service in the website hosting. How to achieve my request, google app can do it?
Some hosters allow (1) windows scheduled job. (not in that list of 3 you gave). Use it to call your web page.
Some hosters allow SQL Agent jobs. Call the page from that.
Failing that, have a service/task/job running on an off hosting server that triggers it. (eg cron on your router, windows server)

How to make a Web (WCF) Service act as Windows Service

The question,
I have a web application - .net 4.
The client is having a requirement that he want to send email to his users on a regular basis about his new courses etc.
The webapplication created will hosted on a "Shared hosting environment" with no excess to windows services - file system other than the web root folder through FTP.
The PROBLEM is that ...
Due to shared hosting i cannot create a windows service for him which will check the database - if there are any scheduled mail to send every 5 min.
So my question is - Is there anyway i can run a wcf web service or any other web based service or page or handler which can keep running and automatically checks the database for any new scheduled mails - if yes start sending it automatically in an different thread.
Any different suggestion or answers are also very much welcomed. Thanks SO Experts.
Create a webmethod in WCF that needs to be called, and that can be reached through a url, for example:
http://www.example.org/checkandsend/email/
Add a new scheduled task on your own PC, that calls that URL every five minutes. Voila.
Rick Strahl: Forcing an ASP.NET Application to 'stay alive'

Resources