Reputation: 2908
I have a very simple ASP.NET WebAPI endpoint that does one call to our database, and return those rows as JSON. The response size is around 180KB (180 records).
When I deploy that project to Azure that call takes around 100ms which is fine but only on the first minutes, then suddenly it slows down to 24 seconds consistently.
On the code below I'm serializing the object manually instead of just returning the modal directly (using webapi JSON serializer which is the same), for the sake of understanding where time is being spent.
[HttpGet]
[Route("{stuffId}/toys")]
[ResponseType(typeof(IQueryable<FooModel>))]
public HttpResponseMessage GetStuff(int stuffId)
{
var stuff = QueryProcessor.Execute(new GetStuffByIdQuery
{
StuffId = stuffId,
});
var mappedResult = stuff.Map();
var response = Request.CreateResponse(HttpStatusCode.OK);
var json = JsonConvert.SerializeObject(mappedResult);
response.Content = new StringContent(json, Encoding.UTF8, "application/json");
return response;
}
And what I realized is surprising, the time is being spent on the Serialization method.
var json = JsonConvert.SerializeObject(mappedResult);
What is more confusing is that, deploying the same code to another website on Azure other than our production environment, or running it locally pointing to production databases it's always fast.
Any idea why is this happening?
Upvotes: 4
Views: 3052
Reputation: 11
You can do
var mappedResult = stuff.Map().ToList();
This worked for me
Upvotes: 1
Reputation: 2908
Following the idea that matt_lethargic pointed out, I discovered that there was something being done on the .map
call that was causing the issue.
I was mislead by the fact that the execution was being delayed, so doing stopwatches I was not getting into the exact problem.
Basically there was a instantiation per each row doing made on the ResourceManager
to access some values from a resources file.
Upvotes: 2