2

I've created a simple WCF service which is hosted within a windows service. WCF service is automatically started on windows service start, but when I connect to the WCF service from a client for the first time it takes a couple of seconds (as any other WCF service).

I'm aware on how to avoid this delay when the service is hosted on IIS, but not when it's hosted within anything else.

To be exact:

Is there a way to wake-up the WCF service which is hosted within a windows service without a client reqest being made to it? Possibly from the windows service code?

Miljenko Barbir
  • 1,193
  • 1
  • 11
  • 32
  • The delay is most likely on the client side, not the service side. It takes a bit longer the first time the proxy is created. – Tim Jan 14 '15 at 19:09
  • I've tested this and tests prove the opposite... When I start the service and the client application, make the request, it takes 3-4 seconds for the WCF service to respond. If I shut down the client, start another instance, the first call from this new instance is as fast as any other. On the other hand if I restart the service and make the another request with the client it takes about those 3-4 seconds to respond. Meaning the delay must be on the WCF service side. – Miljenko Barbir Jan 15 '15 at 18:40

1 Answers1

3

With IIS hosting, the delay is not because the WCF service needs to "wake up". The reason this happens in IIS hosting is that IIS will unload the app pool from memory after a period of inactivity (or due to some other internal state). This does not apply with windows service hosting.

The delay you are experiencing is likely because when your client calls the service for the first time it must spin up a wcf channel to make the call. Subsequent calls can then be made using the same channel so there is no lead time.

To prevent this behavior you must keep the channel open all the time. There are several ways to do this, but the simplest is to use sessions. See Erik Funkenbusch's excellent answer for more information. However, you should consider if you absolutely need this, as sessions are an overhead and don't scale well.

tom redfern
  • 30,562
  • 14
  • 91
  • 126
  • I'm not sure if you're saying that the problem is on service side or client side, but I've tested this a bit and... When I start the service and the client application, make the request, it takes 3-4 seconds for the WCF service to respond. If I shut down the client, start another instance, the first call from this new instance is as fast as any other. On the other hand if I restart the service and make the another request with the client it takes about those 3-4 seconds to respond. Meaning the delay must be on the WCF service side. – Miljenko Barbir Jan 15 '15 at 18:40
  • That's odd. Is the service configured as a singleton? Does it load in a large amount of data as part of it's job? – tom redfern Jan 15 '15 at 21:31
  • +1 thanks for explaining the reason for that behavior, but what is the best practice to deal with such problem, call it from server scheduled task each morning?? – Jonathan Applebaum Feb 13 '18 at 09:36
  • 1
    @jonathana you must keep the channel open. There are several ways to do this, but the simplest is to use sessions. See this answer for more information: https://stackoverflow.com/a/26739739/569662 – tom redfern Feb 13 '18 at 19:58
  • @tomredfern the link looks like just what i need for my `TCP` `windows service`, many thanks. – Jonathan Applebaum Feb 13 '18 at 21:00