Auto restart IIS Website from console application - c#

We have an ASP.Net application used by many customers installed on their own servers. Because it is installed on their end with each having different databases and URL bindings etc I created a console application a while ago that gets a zip file and extracts it to c:\inetpub location to append the latest application changes. This console app is added to scheduled tasks to create an automated update.
Obviously when anyone accesses the site for the first time after it does this they have to wait a little longer whilst the site rebuilds. I changed the console application to include a Process.Start(urlofapp) so that it should hopefully do this as part of the update so the next morning that first user doesn't have to wait for the rebuild.
I have tested it ok running myself but not yet released as my concern is that this url process is kept open. Can anyone enlighten me as to whether this would be the case as I don't want this to happen or can give me any ideas as to how to rebuild the site manually as part of the console app.

IIS has had an auto-start apps feature for quite some time. You just have to enable it. You can find more info from the Gu, and the IIS site.

Safer instead to use a start page which loads everything on the server in the background. If you do this logic in an async method which in turn is called from an MVC controller for example then this can potentially run whilst the main page has finished. Alternatively use a threaded Task and show a start page that is only returned whilst setup is taking place.

Related

.Net Application logs out on certain pages

I have a .Net website and when I click the menu for certain pages I am automatically logged out. I tried debugging by opening code and hooking up to the environment database but I am able to open the pages locally. I am not sure why this is the case.
I would like some input about where to start troubleshooting.
I would first check if the server is faulting and losing the session somehow. Perhaps the worker process is crashing or the application pool is recycling for some reason. Without more information this is just a guess though.

Is there a way to have code run on a time schedule in an ASP.NET web app?

I have an ASP.NET web app providing access to a database on the server. What I need is a way to run code in the background on a given schedule that auto-updates the server database from another source.
I know how to do this in a windows app by adding a timer, linking up a function to the timer tick event and starting the timer. I do not know how to do this in a web app.
Is there a start-up event for a web app or somewhere where I can start this background process regardless of whatever any users are doing on the site?
You should not do this in an ASP.NET website - this is a major no-no. You are correct in thinking to use a timer on a background .exe. You should look into creating either a Windows Task (a console .exe executed by the server task timer), or a Windows Service. I would suggest the Windows Service as that is standard practice.
If you have access to the computer hosting your site I would write a little app that was run from the Task Scheduler.
The web server is not meant to handle long-running background tasks. It's the wrong tool.
If you dont have access to the hosting computer then I would suggest building some kind of interface whereby another computer rebuilt the database and uploaded it. I'm using the terms "interface" and "upload" in the loosest, broadest sense - apply your own definition.
I was searching for a solution myself couple of months ago, and even though I haven't found enough time to try it so far, I guess I can share the link with you. Maybe you'll find it helpful.
If yes, please, let me know here.
http://quartznet.sourceforge.net/
How to use Quartz.net with ASP.NET
you can use Windows Service or use Timer Control (In the Ajax Category)
Or
As other answers have stated, doing this full function - updating a database and scheduling it as an ASP.NET app is using the wrong tool for the job.
ASP.NET can be used to update a database - that's perfectly valid. Where it breaks down is in the timer. ASP.NET apps aren't meant to be long-running, which is necessary for the timer to work.
If you can do it, I'd strongly suggest using the approach others have suggested - a Windows Service or a Scheduled Task.
However, if you have no access to the actual server, other than to post ASP.NET code - you can't install a service and you can't set up a Windows app to run on a scheduled basis, here's an out-of-the box idea.
Set up a web service or ASPX page that does the update, and then call that page from a scheduled task on a machine you DO control.
So if this was at http://www.someserver.net/updatedb.aspx, there's no reason you can't set a scheduled task on your own PC to call that URL.
I'd consider this a last-ditch solution to be used only if you can't do one of the other options.
The global.asax.cs file has a method that is fired when your application starts: Application_Start. You can hook up your timer method in that event. Just beware, depending on how IIS configured, your app pool may shutdown. For example, if no one hits the site in 20 minutes for example. Just make sure if you HAVE to have this run every X minutes that you have IIS configured to ALWAYS be running and start your app. This is harder than it sounds. In the end, you may want to go with a regular windows scheduled task.

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

Background function in asp.net

Is it possible to write a background function in a master page that triggers after a specific period of time, say 5 hours?
function()
{
execute code from the clsGeneral Class
}
Please suggest how this can be implemented.
Pre IIS 7.5 you don't. (technically you can, but it's stupid). IIS is free to terminate your app whenever he wants, and when IIS is restarted, apps are restarted "lazily" (until someone opens a page of your web app, the web app isn't started).
From IIS 7.5 onward you could using the Application Warm-Up Module (sadly the beta was removed :-) ). With it you create a Thread that waits the specified time and does something. Quite easy.
The "right" solution is normally to create a Windows Service and use Quartz. Read here for example Scheduler for ASP.NET?

Is there any way to get publishing queue in sitecore?

Problem is that I use a 5.2 Sitecore, and when I start to publish something, it only shows the publishing window and a "Queued" message. There is a chance that some people full published several times(that means 15000 items and 4 slaves), and the publishing queue is stuck. The restart of the webserver/app pool is not an option.
Any experiences in sitecore publishing errors?
something like this
If several full publishes are queued then you will have to either wait and see if things finish or recycle the AppPool to start over. Try checking your Sitecore logs to see if there is any information in there that will tell you if publishing is actually locked up or if it is just taking a while to finish.
In my experience it is possible to see that locked up publishing dialog and still have the publishing queue clear itself. Unfortunately though you will most likely have to recycle the AppPool to fix this issue.
So the final "solution" was to be the AppPool restart. Shame that the full publish stuck the sitecore shell, but the db browser could publish. Whatever, think I should contact to some sitecore support guy. BTW thanks Andrew&Kim!
When this happens to me, I normaly wait until the publishing is done. But the problem is that noone knows how long time it will take.
Sometimes a reboot of the server accually does the trick, because the publish then dies. You could try to restart the server (if you have that opportunity of course) and then see if it helps. Otherwise ou have to be patient and wait until the publish queue is empty again.
It is possible to see the publishing queue on sitecorerocks - the visual studio sitecore plugin --> there is a job viewer that shows all the publishing task currently running plus the indexing task
AppPool restart is the way to go if you would like to "kill" the publishing operation.
Open IIS
Expand the main node
Select "Application Pools"
Find and select the worker process that your site uses
On the right hand side hit "Recycle" under "Application Pool Tasks" header
You can also take a look at (your site)/sitecore/admin/PublishQueueStats.aspx, or (your site)/sitecore/admin/jobs.aspx - take a look on a file system, see what other administrative tools available for you in "(your web root)/sitecore/admin" folder. Might find some gems!
Yes, you can see all items in publish queue by using code. There is a way to get the list of item IDs in publish queue using API (Sitecore.Kernel.dll).
IdList publishQueue = Sitecore.Publishing.PublishManager.GetPublishQueue(startDate, ToDate, Context.CurrentDatabase)

Categories