views:

523

answers:

8

Hi,

I have written a C# WebService. The problem is that after I publish it to IIS it won't automatically start unless any of its methods is called. This is very frustrating because this WebService has to continuously do some background work immediately after it starts (its constructor executes). If IIS is restarted, the WebService will just sit idly until one of its methods is called. Is there a way to overcome this and force the WebService to execute its constructor immediately after it is published or IIS restarted?

+10  A: 

If It has to continuously do some background work immediately after it starts why not implement this in a Windows service? I think you can write a WCF service which will be hosted in the Windows service. That way clients can still call your service, the service can do its background work and wont be dependent on IIS as the host window service will run on its own process.

ydobonmai
-1 - off not seeing real world scenarios that require this.
TomTom
Tom, why It wont be done in a real world? :-) The beauty of WCF is that I can host a service in any kind of host and one can take advantage of windows service in this scenario. Why not? Please explain.
ydobonmai
Real world, 1: WCF service may be part of a website. 2: may be exposed on a shared host - i.e. no install possible. 3: may simply need some startup and I dont want to establish connections and compile on first request. As I said - the IIS team agrees.
TomTom
Well,in general a WCF service should NOT be a part of the website as a best practice. The WCF service could just be a library which will be used in/called by different clients(website, winforms) and can be hosted on different host (IIS, Windows service)
ydobonmai
Well, in general this may apply for larger installs, for small ones it may be stupid to split them out. Want examples where it makes sense? You seem to need a lot of education.
TomTom
Tom, sure. We are all here to learn. Isn't it.:-)
ydobonmai
Yeah, but not everyone learns in every sentence. One example for you: my PBX (software based phone system). Website + web service. What you propose? 1: two ip addresses, 2: one of them on non-standard port or 3: website and /API.svc for the simple web service. My bet would be 3. Most convenient.
TomTom
Ashish: you're right about the option of hosting the WCF service in a Windows service, but this probably shouldn't be the first choice. Hosting in IIS gets you the advantages of hooking into IIS app pool management tools, failover, app pool restart, etc. -- all things you would have to implement yourself if you self-hosted. In many cases, the optimal architecture is a WCF service hosted in IIS and handling requests, collaborating with a Windows service that does the background processing. Of course this depends greatly on the application. Self-hosted services are very useful too!
itowlson
I don't think this should be marked down. Ashish's suggestion is a valid one given the original question, which should almost certainly be using a Windows Service instead of IIS as host. He's correct that you can host a WCF service - from any code, in fact - without even IIS installed on the machine. IIS *is* generally a more sensible choice for a service host, but that doesn't make this option invalid - that decision is up to the OP.
Dan Puzey
@itowlson +1 for this :- "the optimal architecture is a WCF service hosted in IIS and handling requests, collaborating with a Windows service that does the background processing"
ydobonmai
I understand that WCF services have the advantages you mentioned plus they're faster than webservices. However, it's not clear to me why shouldn't a webservice do background work. Can you elaborate on that?
hancock
+4  A: 

If you need it to run all the time, your design is flawed. Create a windows service instead.

leppie
-1 - off not seeing real world scenarios that require this.
TomTom
Au contraire, TomTom, this is exactly the "real world" scenario that Windows services were designed for. As Axyradax says, a Web service isn't meant to do background work continuously, it's there to serve requests. A Windows service is there to do background work continuously, and/or serve requests. Just because "Web service" has the word "service" in it doesn't mean it can or should be doing the work of a Windows service.
itowlson
Real world, 1: WCF service may be part of a website. 2: may be exposed on a shared host - i.e. no install possible. 3: may simply need some startup and I dont want to establish connections and compile on first request. As I said - the IIS team agrees.
TomTom
+1 for "Just because "Web service" has the word "service" in it doesn't mean it can or should be doing the work of a Windows service."
ydobonmai
I'm not convinced the IIS team does agree. You're identifying warm-up (which everybody agrees is a useful thing) with continuous background processing. If the IIS team had wanted people to use `w3wp.exe` as a generic service host, surely they would have made a background processing utility rather than just a warm-up utility.
itowlson
Try that: I have a service tha works out of a cache and logging into the backend takes like 30 sconds, so I want to pull the data in the background every 5 seconds and then have it avaialle when a user calls. Valid business case for warm up and continour background work from a web service.
TomTom
@TomTom - No, that isn't a valid case. In this case what you should be doing is having a Windows Service that continuously retrieves the data, and then a Web Service that talks to the Windows Service to retrieve the data it has cached. Each type of service acting in the way it is designed to work. Don't confuse your own lack of knowledge and incorrect architecture for everybody else being wrong.
Greg Beech
Dont confuse your arrogance, Beech, with the real world. I can show yo ua lot of very illogical r very sensible scenarios where your accogance falls flat. Two examples: Money (shared hsoting vs. at least virtual server to install software) and regulations (website update = ok, install application = tons of red tape, takes weeks to approove any change.).
TomTom
@TomTom - So now you're confusing the limitations of your own environment with the right way to do things?
Greg Beech
No, but contrary to you I take the question as what it is - a question how to do something. The user may be in the same sitaution others are - in that"the right way" may be an academic exercie that is not applicable due to external limitations that you tend to show ignorance towards. So, my answer is how to do it. And then I would say "but i would advice to sue a windwos service". But not rant around how that is bad bad bad because i am too stupid to answer.
TomTom
@TomTom - But you did answer, you didn't explain the limitations you were assuming, and you didn't advise use of a Windows Service. The reason I didn't answer was because Ashish already posted a good one, so I just voted that up; no point in duplicating information.
Greg Beech
+3  A: 

I don't think a web service is meant to do some background work continuously - it's there to serve requests for its methods.

Axarydax
This ignores the fact that it may still be good to keep the servie running. I havea front end for a system where a login takes 15-20 seconds. I dont want to log in the moment a user requests something - I want the system to warm up and keep warm.
TomTom
Can you elaborate on that one? I'm just weighting the options here ... why shouldn't webservices do background work?
hancock
Web Service is meant to be an API - interface to something running on server machine, for example database. As it is just the interface, it's not something doing the actual work. For example, if I were to have a house robot, its web service would just read its status and add commands to its database (yes, I'd make a robot with a database) and robot's control system would interpret these commands and do the actual work.
Axarydax
+1  A: 

You need to create windows service. Here are the steps to create windows service for scheduled task.

Brij
A: 

Apart from the (correct) remark that you should use a Windows Service for stuff that needs to run in the background: Can you use the Application_Start Event in the global.asax?

Michael Stum
AFAIK, that event would only when the web service recieves its first request.
Jørn Schou-Rode
Yes. It does not warm start the application.
TomTom
The webservice does not currently have a Global.asax file. Can I start it automatically from there if I add it?
hancock
+1  A: 

I agree with the others, you should use a windows service. If you're using WCF, you can easily host your web service inside a windows service and get the best of both worlds. See this msdn article for a tutorial.

Jonas H
See shared hosting for the real world. May not be possible, causes possibly a lot of administrative overhead. There are good reasons to NOT self-host.
TomTom
A: 

And I do not agree. There are good reasons to make sure that a web service is started with the computer - to allow it to start filling it's caches and / or to simlpy avoid the slow first execution.

The IIS team is with me on that. This is why they developped soemthing for that:

http://www.iis.net/expand/ApplicationWarmUp

This module can make sure a website is "warm" (as in: started) when IIS starts.

TomTom
App warmup is certainly a good idea for cache-heavy applications (and web services), but the question was regarding "continuous background work". IMO relying on such background work to be running due to application warmup is a hack.
Jonas H
Depends a LOT on what this background work is doing. It could be "clean up temp folder every 30 seconds". It could be "pick new market data to expose every 15 seconds" - all valid continous background workd even IN a web application.
TomTom
It will only warm up on the first request... then your app my be shutdown after some amount of time with no requests, then it will start again (with another warmup).
leppie
Ah, you mean the user is too stupid to turn of the shutdown and keep the service running? Seriously?
TomTom
Till some admin comes and recycles your appdomains...
leppie
Ah, so now not only the user, also the admins are stupid? I suggest you look for a host employing non-stupid admins.
TomTom
-1 - All the examples you've given are things that should be done in a Windows Service, not a Web Service. You're giving things like not being permitted to install a Windows Service as a reason, yet you're the only person who has mentioned this restriction which means it's pure conjecture and thus has no validity. Continuous work has *no place* in a Web Service, which should simply *respond to requests*.
Greg Beech
@TomTom: the tool you linked *doesn't* do "background work." It calls a few pages to allows the server to pre-cache data and connect providers/etc, but these are one-time operations. It's not for running "continuous" tasks as per the original question.
Dan Puzey
Also @TomTom, regarding app recycling: IIS will recycle your AppDomain automatically under a huge number of conditions. It doesn't have to be a manual action, and any user who is stupid *enough* to turn that off deserves what they'd get...
Dan Puzey
So what? How are your ramblings helping the OP who asked a specific question? Not at all.
TomTom
@TomTom This seems interesting, however it's beta and I don't think I'll ever get approval for installing such software on the production server.
hancock
This still is about the only way. Well, no, there is another way ;) Public website? Set up HTTP monitoring.. using http://www.monitis.com/. let it call into your website and... here you go. Wake up call at least every 5 minutes. Or set up something similar yourself. Seems like the only way to do that.
TomTom
A: 

Actually, you can run a Thread or a job automatically in "Global.asax.cs". E.g.

 public System.Threading.Thread schedulerThread = null;
 protected void Application_Start(Object sender, EventArgs e)
  {
    schedulerThread = new System.Threading.Thread(YourLoopBackgroudMethodHere);
    schedulerThread.Start();
  }

And Don't forget to close the thread when your site application end.

protected void Application_End(Object sender, EventArgs e)
  {
    if (null != schedulerThread)
    {
      schedulerThread.Abort();
    }
  }
jujusharp
The thread or task will automatic run once your web site program was running!
jujusharp
The webservice does not currently have a Global.asax file. Can I start it automatically from there if I add it?
hancock
Sorry, I didn't log on this site days ago, yes, you can create a Global.asax file in your project and add your code in the Start function. Hope you've solve the issue already!
jujusharp