WebAPI Slow first call to service

不羁的心 提交于 2020-01-01 19:07:06

问题


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?


回答1:



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. :)



来源:https://stackoverflow.com/questions/36056536/webapi-slow-first-call-to-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!