Quantcast
Channel: Web API
Viewing all articles
Browse latest Browse all 4850

ASP.NET Web API - Will there be a performance impact in using response.Content.LoadIntoBufferAsync().Wait()?

$
0
0

I had to catch the errors that occur during XML serialization (mostly due to invalid ASCII control codes), so I went on and replaced the controller endpoint as below.

Found the solution on how to do that here - http://stackoverflow.com/questions/16552715/how-do-i-trap-a-serializationexception-in-web-api/16554959#16554959

[DataContract(Name="ActivityData")]
[XmlRoot("Activity")]
public class ActivityData 
{
[DataMember]
public string Id { get; set; }

[DataMember]
public ActivityType type {get; set; }

....other Data members
}

**Before:**

[HttpGet]
public ActivityData GetActivityData(string startYear, string endYear)
{
ActivityData activityData = Repository.GetActivityData(startYear, endYear);

return activityData;
}

**After:**

[HttpGet]
public HttpReponseMessage GetActivityData(string startYear, string endYear)
{
try{ 
var responseContent = Repository.GetActivityData(startYear, endYear);
var response = Request.CreateReponse(HttpStatusCode.OK, responseContent);

//Below code ensures that any errors during serialization is caught. 
//But does it affect performance?
response.Content.LoadIntoBufferAsync().Wait(); 

return response;
}

catch(Exception ex){
logger.error(ex);
return Request.CreateResponse(HttpStatusCode.InternalServerError, ex);
} 
}

My question is that, will this have any imapct on the API's performance?

Does returning the object directly continuously 'stream' the data as it gets serialized and by Loading it into buffer, would the API serialize the entire object and then return it all together? or is just the same behaviour on both occasions?

I noticed the memory consumption go up when the code was hitting the return statement on both occasions. I would just like to know for sure that there wouldn't be any performance issues because of this change.

The returned XML document can be anywhere between a few MBs to a few hundred MBs of payload with number of rows ranging between hundreds and up to amillion rows. (depending on the start and end date), so is there a limit to the buffersize?

[Update]

I also noticed that, on both occasions, the memory consumption of w3wp.exe went up. Once, when getting data back from database, and the other time when it hit the return statement. It was taking up more than 1.5GB of memory. And it didn't release the memory, even after stopping visual studio. However, when I made a second request whilst the VS was left running, it released nearly 800MB of memory, and started going up again when it was processing the second request. This was happening on both versions of the code. Is this a concern when there are multiple requests? 

Thanks in advance.


Viewing all articles
Browse latest Browse all 4850

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>