Time-consuming communication with HttpClient, WebApi and WCF - c#

I need help to understand where my time problem is. I have a winform/wpf application which communicate with a WCF service through a webapi 2 and a System.Net.Http.HttpClient.
Client => HttpClient => webapi => wcf service.
When I deploy this and run, it takes the first time very long time to get an answer back. But second time and more it is very fast.
If I don't run it for a while it sleeps again.
Why is it so slow in the beginning, what will I look at?

When first call WebApi will initzialize (IIS have to run Api, and by default ISS start api after first call). This take some time. And in IIS You have the default AppPool Idle Time-out (minutes) set to 20 minutes, so after 20 minutes app will go to sleep mode, and IIS have to wake up app.
WebApi why 1st call is slow?
Almost the same problem is with WCF
WCF why 1st call is slow?
So in Your app you have problem with slow 1st api call, and after this you have problem with slow 1st wcf call. You have doubled the slow.

Related

Web api calls are slow in azure

I'm developing a web api and I hosted it on azure, I have a call that takes about 2.5 seconds in my local machine but takes a lot longer when the app is hosted in azure as you can see in this figure:
it's taking 12.8 seconds which is not expected, why is this happening, and what is the part highlighted in red? why does it take about 10 seconds to start with the first operation in the code? I have "AlwaysOn " on ON so this is not my api going to sleep, also, sometimes the call takes less time (4-6 seconds) which an inconsistency, please enlighten me.
If, CPU usage is not high, one reason could be SNAT port exhaustion / pending, if you have too many open TCP connections (including SQL Server's) then new connection will wait.
You can check that from your app service "Diagnose and solve problems" -> "Availability and Performance" -> "SNAT Port Exhaustion".
If this is the case, this is a good place to start: https://learn.microsoft.com/en-us/aspnet/web-api/overview/advanced/calling-a-web-api-from-a-net-client
Have you tried to increase the tier of your app service plan? This will help you to understand if it's an infrastructure or code problem

Getting spikes in gRPC calls every hour

I have a website running with around 7 servers (C#). And there is a gRPC service (golang) running with 3 instances. Each web server connects to and makes calls to the gRPC service. There are around 8000 calls per minute to the service.
The call to this service is not that critical, so lately we reduced the deadline of the call to 20 milliseconds. Here we noticed something strange. There was a spike in the "deadline exceeded" errors every hour throughout the day. And it happens exactly at 0th minute i.e. 2pm 3pm 4pm etc.
Why does this happen?
I came across this link saying gRPC resets the connection every hour, but nothing more than that.
So my question is does gRPC internally refresh the connection every hour. If yes is there anyway to tweak this behavior. If no then can someone give some direction as to how I can debug why this is happening.
No, grpc-go does not refresh connections. The only time it initiates a disconnect is if you configure "max idle" (ref) and the connection has been idle for longer than that time limit. By default this is disabled, so it's unlikely to be the culprit in this case.

How To Keep My Web Services Awake?

I have written two web services that I am running on GoDaddy. One is a Microsoft WCF web service and the other is a RESTful Web API service. They are both working, but they rarely get traffic. If I don't call the web services for some period of time they seem to go to sleep. Then when I load the pages that call the web services they take some 20 to 30 seconds to retrieve data from the services. After that if I continue to call them repeatedly they load in just a second or two. Is this normal or did I do something wrong in my configuration? Is there some way to keep them active?
Entirely normal. You can either increase the recycle time limit in IIS (but you will still get recycled eventually) or you can write a quick scheduled task like the following to run every 10 minutes or so:
powershell Invoke-WebRequest -Uri "http://example.com"
Although I would caution that you should forcefully restart the service sometime during low usage hours just to clear the process memory / resource utilization.

Self hosted WCF Service with Timer

I am following this example to create self hosted WCF service. Ideally I would like service to hookup with timer to check every half an hour if certain value is updated in database and if yes service would perform some task else will keep checking every half an hour. I have read it online that using timer in IIS hosted WCF is not a good idea, how about using it on self hosted wcf service? any examples?
Thanks,
I think a better option for you would be to create a simple console app that performs your task if the value is updated and then create a Scheduled Task in Windows that runs this console app every half hour. That way you can let Windows manage the timing part and you just have to write the code that checks the DB and updates it if necessary.
Not sure what version of Windows you are running, but on you can get to scheduled tasks from the Control Panel.
Create a Scheduled Task on XP
Create a Scheduled Task on Windows 7
The reason a timer in a IIS hosted WCF service is "not a good idea" is a IIS service has a much different lifetime than a self hosted service. See this SO question and answer for some details and this MSDN article for even more details.
Basically a WCF service can be "shut down" while being hosted inside IIS if no-one has connected to it within a timeout period. If you need regular periodic maintenance like you are describing you will need to use a self hosted service and have that service start a timer up that fires every half hour in it's OnStart() call.

WCF service call

I have a WCF service which takes computer ID,IDs on a network as input parameter and saves the computer stats for past 12 hours[like how much time the computer was locked, active, idle etc..] in the database.
Also I have a website from where I can set the scheduling for few computers at some time t for stats[ for past 12 hours as mentioned above]. This scheduling information[computer is and time] will be saved to database.
Now the issue is how to use the WCF service to make sure it runs on that particular scheduling time and also how to show the computer stats on the website when the WCF has been called and stats have been generated. If I use a window service to call WCF service how will I ensure that it runs on that scheduled time, also how to inform the website that the stats have generated.
Any help would be appreciated.
Cheers!
Set up a Schedule Task that will make call to WCF Service Method you want to run using something like cUrl
I believe the website needs refreshing so it can pick the data after the WCF Method gets executed so you can again use cUrl to make a web page call

Categories