Creating a "Heartbeat" or Windows-Service-Like functionality in Asp.Net - asp.net

I've heard Jeff and Joel discuss on a podcast what they called a "Heartbeat" which essentially is creating something that acts similar to running a windows service in an website. I was hoping to get some more insight into how something like this would be implemented. Has anyone implemented something like this before and what did you use it for?
Thanks!

I found the answer in a combination of places. I took what Jeff Attwood did for stackoverlow here as well as the Code Project article and made something that is completely reusable and able to easily be hooked up using an IoC tool. I've posted the full details here

Basically you use a web page to kick off a process... but you put a cap on how often the process can run.
Something like this:
TimeSpan timeSinceLastRun = DateTime.Now.Subtract(lastRunTime);
if(timeSinceLastRun > interval) {
RunCustomProcess();
lastRunTime = DateTime.Now;
}
this way you just have to ensure that occasionally someone (or some program) visits the page. Hitting the page many times won't adversely affect your process..

This Code project article: Simulate a Windows Service using ASP.NET to run scheduled jobs, explains it all.

You can use ASP.NET Health Monitoring and wire up something to WebHeartbeatEvent.

We are implementing something like that between the client and server, as we have windows forms client and WCF service acts as a server.
The aim of the heartbeat is to sayd "I am still alive" from the server side.
Check this link for introduction for Heartbeat in WCF

Related

what are the things need to do in ASP.NET application maintenance project?

I recently got into a project where I need to support for two mobile applications build using asp.net.
Can anybody share the jobs need to do in maintaining those projects ?
Thanks.
Nilesh
I think you may consider the following points while you are doing maintenance projects :
1) You should talk to your manager and make them understand that you are overloaded and you can't do effective work if everyone keeps bombarding you with requests which they want fulfilled immediately.
2) You should keep tracking on what you have done and how.
3) Keep backups regular basis of all the tasks you have completed.
4) Keep updating your manager on what task you are working and what you have understand and how you want to work/achieve this.
5) Ideally, all incoming requests should be entered into an issue tracker like JIRA or Mantis. Or at least mailed to the product owner, not you. And he/she should deal with all the complaints from the users too over "why is my request not ready yet?!", allowing you to focus on the development work.
Hope it helps you. :)

ASP.NET: Scheduled tasks

While the economy has been in the shitter, I've written possibly the most coolest site EVER for unemployed developers looking to buzzword-ify their resumes. (Don't hate the player, hate the game.)
The only problem is, it needs a scheduled task to run once a day to do some data mining. I spent many, many hours a few months ago researching solutions, but nothing seemed sure-fire.
If I have shared hosting and cannot remote in (e.g. mstsc and create a Schedule Task), how can I create a task that will run once a day on the backend of an ASP.NET website?
After all the research I did, I don't think it's possible. Per my last analysis, someone has to visit the site at least once a day to instantiate an instance of HttpApplication.
Does anyone have any solution to making sure an operation runs automatically, no matter whether anyone visits the site, and without anything but FTP access to the website?
Like I said, I've done A LOT of looking into this in the past, and it didn't seem possible. IF YOU HAVE EXPERIENCE implementing a solution, please, contribute your advice! But not postulating or conjecturing needed--it's far more nuanced and difficult than you're surely imagining.
I had a similar problem. I wrote a cheap utility that issued a web request from my desktop to my web app on a regular schedule.
How To: Send Data Using the WebRequest Class
You could have a look at Quartz.NET for scheduling jobs.
DotNetNuke which is a popular open source .net content management system does this. You can download the source and see exactly how they did it.
I agree with HackedByChinese, but if you don't want to run it from your desktop, you could get something like http://aremysitesup.com/ to ping your site for you.
Can you schedule a task in SQL Server that could call a CLR function? Your hosting provider may not allow either ot both of those, but if they do, then you are golden.
You can use ATrigger scheduling service. A .Net library is also available to create scheduled tasks without overhead.
You can set to call your datamining URL every 1day.
Related example: http://atrigger.com/docs/wiki/17/use-case-delayed-processing
Benefits:
Reporting
Error Log
Tasks List
Great Timing Features
Disclaimer: I was among the ATrigger team. It's a freeware and I have not any commercial purpose.

Speeding up a Web Service

I have a web service running and I consume it from my desk application that is written on Compact Framework.
It takes 13 seconds to retrieve 8 results which is kinda slow. I also expect to be retrieving more results in the future. The database query runs fast.
Two questions: how do I detect where the speed slow down occurs? Do I put timers in the Web services code?
I would like to detect whether it is the network or the application code.
This is my first exposure to web services in a real environment so please bear with me.
i used asp.net 2.0 and c# to write a simple web service.
Another good profiler is the EQATEC Profiler. I did a write up on it here: http://elegantcode.com/2009/07/02/eqatec-profiler-and-net-cf-profiling-and-regular-net/
And it works find for .net CF projects. But this will allow you to see if there performance issues in unexpected places.
Your already on the right track of adding event logging, and include timers in them. Note, doing so will add to the over all time it takes, so you'll want to remove them after you track down the culprit. Also look into running the same webservice call multiple-times without re-initiating the connection, that may be cause as well.
-Jay
A starting point is to profile your web service to see where the delay is comming from
Did you know the CLR Profiler? There are some tools you can use to see what is happening
http://msdn.microsoft.com/en-us/library/ms998579.aspx
The database connectivity from your service to the DB could be a possible cause for slowdown. Adding timers should do the trick. If the code isnt too huge, you can look at the coding constructs to come up with an informed decision of where exactly things can be slow. Then add the timers. You would get a fair idea of where things are slowing down.
Two biggest pain points are going to be instantiating the web service reference and transferring all the data over the network. Pending anything turning up where some obvious blunder was made, I would look at ways of reducing the size of your xml and ways of better handling your web service reference.
All I know about the compact framework is that it is a pain to work in. I've worked on a number of web projects though and profiling your server, putting in logging to record the time taken will be helpful. If all the time is being taking post server response, however, it won't do much more than prove your server is working quickly.
SoapUI is a fantastic java application for consuming web services. It has a lot of functionality, including time metrics. I would start with that and see how long it takes to consume the same thing your client would be. Failing issues there, start with what I recommended above.

article about number of connected users?

Several months (maybe even a year or two) ago, I saw an asp .net article that showed how to tell how many people were connected to a running web application. Of course I only glanced over the article & didn't save it. Does anyone remember seeing the article or know where I can find it or perhaps something like it? I have searched Google from the best of my memory of the title & content but I'm getting no hits.
The reason I'm asking is because I have a WCF web service that has crashed several times after I publish updates and the only thing I can think that would cause these weird problems is that people are connected to it & its corrupting the files. I'm not going to publish any more updates during the day now, but we also have a couple of people that work during the night and it would be nice to see if people are connected or not before "flipping the switch".
Any help would be greatly appreciated...
Thanks,
Wali
The following article shows how you can use Session_Start and Session_End of the Global Application Class to count the number of active sessions:
How to show number of online users / visitors for ASP.NET website?
In your search, consider using keywords like
perfmon asp.net sessions
Intel has a good article. Unfortunately, it's 404 at the moment, but Google cache has a nice copy. Original link to the Intel "Using perfmon to tune n-tier .NET applications"
When your WCF service crashes, there are likely to be entries in the Windows event log. If not, then the service should be doing logging on its own. I suggest you look and find out whether the service may not have been telling you wnat's wrong.

Should I use a Windows Service or an ASP.NET Background Thread?

I am writing a web application in ASP.NET 3.5 that takes care of some basic data entry scenarios. There is also a component to the application that needs to continuously poll some data and perform actions based on business logic.
What is the best way to implement the "polling" component? It needs to run and check the data every couple of minutes or so.
I have seen a couple of different options in the past:
The web application starts a background thread that will always run while the web application does. (The implementation I saw started the thread in the Application_Start event.)
Create a windows service that is always running
What are the benefits to either of these options? Are there additional options?
I am leaning toward a windows service because it is separated and can run on a different server (more scalable) as well as there is more control over when it is started/stopped, etc. However, I feel like the compactness of having the "background" logic running in the process of the web application might make the entire solution more understandable.
I'd go for the separate Windows service primarily for the reasons you give:
You can run it on a different server if necessary.
You can start and stop it independently of the web site.
I'd also add that it could well have some impact on the performance of the web site itself - something you want to avoid.
The buzz-word here is "separation of concerns". The web site is concerned with presenting the data to the user, the service with checking the integrity of the data.
You can also update the web site and service independently of each other should you need to.
I was going to suggest that you look at a scheduled task and let Windows control when the process runs, but I re-read your question and noted that you wanted the checks to run every couple of minutes. The overhead of starting the process might be too great in this case - though some experimentation would probably prove this one way or the other.
If you use a scheduled task there's also the possibility that you could start the next check before the current one has finished - something you can code for if you're in complete control.
Why not just use a console app that has no ui? Can do all that the windows service can and is much easier to debug and maintain. I would not do a windows service unless you absolutely have to.
You might find that the SQL Server job scheduler sufficient for what you want.
Console application does not do well in this case. I wrote a TAPI application which has to stay in the background and intercept incoming calls. But it did it only once because the tapi manager got GCed and was never available for the second incoming call.

Resources