2

I was having issues with WCF and slow startup times, so i switched all of my code to use WebAPI. While the startup time has reduced massively (~8-15 seconds down to ~2) I still have an odd latency on startup. Currently its about ~2 seconds but subsequent calls are under 0.5 seconds consistently, until a timeout occurs.

I reviewed all of my ApplicationPool settings to ensure there wasn't any startup delays or idle timeout settings.

I found this link Very slow first call to web service which didn't help me - i still get the same speed issue on a cold start. Also found this one First call to web service each day is slow again doesn't seem to apply to me.

I enabled tracing on my WebAPI service and found that the only difference between my first and second call are two lines showing WebHostHttpControllerTypeResolver.GetControllerTypes. I can't seem to find too much about this but it is the only difference i can see.

Is there any other sort of logging that i could use to determine what is actually going on?

Community
  • 1
  • 1
Dominic Cotton
  • 789
  • 10
  • 34
  • Are you saying you get _random timeouts_? Also, do you create any DB-connections during startup? –  Mar 17 '16 at 09:43
  • Well the timeouts are never the same, but the same ballpark...to expand, i publish the server - first call is ~2 seconds - subsequent calls are ~0.5 seconds. After maybe 20 minutes (hard to determine the actual time) the same occurs - ~2 then ~0.5. The only thing in startup is route config and now trace logging – Dominic Cotton Mar 17 '16 at 09:44
  • 3
    `20` minutes - that's the default AppPool **Idle Time-out (minutes)**. So it sounds like IIS unloads your app pool so that the next time you hit it after 20 mins it "loads it all up again" (not sure if it recompiles) hence the lag. You could make it `0` to disable timeout (beware repercussions) –  Mar 17 '16 at 09:48
  • Does wcf service have any ServiceBehavior attributes(`InstanceContextMode.PerCall`)? – isxaker Mar 17 '16 at 09:50
  • OK so after saying in my post that i had checked everything, i obviously missed this - it was 20! I have reset will monitor for the next hour or so. Thanks @MickyD – Dominic Cotton Mar 17 '16 at 09:51
  • @DomCotton You are quite welcome good sir. –  Mar 17 '16 at 09:57
  • So far so good! :) However i would quite like to understand what `WebHostHttpControllerTypeResolver` is doing exactly and if there is anyway to pre-compile or something? As you said: "beware repercussions!" – Dominic Cotton Mar 17 '16 at 09:59

1 Answers1

4

This is a summary of my conversation beneath the question


Well the timeouts are never the same, but the same ballpark...to expand, i publish the server - first call is ~2 seconds - subsequent calls are ~0.5 seconds. After maybe 20 minutes (hard to determine the actual time) the same occurs - ~2 then ~0.5.

20 minutes - that's the default AppPool Idle Time-out (minutes). So it sounds like IIS unloads your app pool so that the next time you hit it after 20 mins it "loads it all up again" (not sure if it recompiles) hence the lag. You could make it 0 to disable timeout (beware repercussions) or just make it a larger value.

If you disable timeout, you might want to consider adding a scheduled task to recycle your app-pool at times of low usage, perhaps 1:00 am or a time suitable for your needs.

A Note of WCF

I was having issues with WCF and slow startup times

This is my experience too (with IIS-hosted WCF) and like I mention above, just disabled the timeout on the app-pools for my WCF services. This keeps them nice and warm. In addition, I have a daily recycle event at a time when I know my system won't be used to allow IIS to do some housekeeping.

WCF hosted in IIS is as far as IIS is concerned, just another IIS app. :)

  • Just a side note on the WCF issue, i was hosting this over Net Tcp via a windows service as opposed to IIS (figured i would cut out the overhead of application pools, etc) – Dominic Cotton Mar 17 '16 at 10:01
  • Ah. Could quite possibly be the fact the system is running inside a NT Service (different ecosystem and all). Normally isn't a problem but things can go wonky as soon as you make something a NT service at first. When you were making your NT service, did you have a test console app surrogate host to host your WCF service to aid debugging? (cause debugging NT services sucks) Did it run better as a console app by chance? –  Mar 17 '16 at 10:04
  • Well i never made it that far. The most i found was that there were some network services causing delays (NetBios). I managed to get my startup time down to ~3.5 but i found lots of people both in IIS and others with the same issue but no real solutions - hence the switch. I wanted to learn WebAPI anyway – Dominic Cotton Mar 17 '16 at 10:06
  • @DomCotton No problem. Glad you got it going. –  Mar 17 '16 at 10:09