Skip to content
This repository has been archived by the owner on Dec 13, 2018. It is now read-only.

Commit

Permalink
Update the method used to read JSON asynchronously
Browse files Browse the repository at this point in the history
  • Loading branch information
ChengTian committed Sep 10, 2014
1 parent 43fdf2a commit 13adde6
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ public async Task AuthenticatedEventCanGetRefreshToken()
private static async Task<HttpResponseMessage> ReturnJsonResponse(object content)
{
var res = new HttpResponseMessage(HttpStatusCode.OK);
var text = await JsonConvert.SerializeObjectAsync(content);
var text = await Task.Factory.StartNew(() => JsonConvert.SerializeObject(content));
res.Content = new StringContent(text, Encoding.UTF8, "application/json");
return res;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ private static async Task<Transaction> SendAsync(TestServer server, string uri,
private static async Task<HttpResponseMessage> ReturnJsonResponse(object content)
{
var res = new HttpResponseMessage(HttpStatusCode.OK);
var text = await JsonConvert.SerializeObjectAsync(content);
var text = await Task.Factory.StartNew(() => JsonConvert.SerializeObject(content));
res.Content = new StringContent(text, Encoding.UTF8, "application/json");
return res;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ private static async Task<Transaction> SendAsync(TestServer server, string uri,
private static async Task<HttpResponseMessage> ReturnJsonResponse(object content)
{
var res = new HttpResponseMessage(HttpStatusCode.OK);
var text = await JsonConvert.SerializeObjectAsync(content);
var text = await Task.Factory.StartNew(() => JsonConvert.SerializeObject(content));
res.Content = new StringContent(text, Encoding.UTF8, "application/json");
return res;
}
Expand Down

3 comments on commit 13adde6

@kevinchalet
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My 2 cents: these Task-returning methods have been removed for a reason: they were not really asynchronous and used Task.Factory.StartNew/Task.Run internally. Offloading pure CPU-bound operations to the thread pool is generally not a good idea and often incur a little overhead, and that's why this kind of practices should be limited to UI, where responsiveness is essential. Don't miss this topic if you want more information: JamesNK/Newtonsoft.Json#66

Even if these methods are only used in unit tests, I'd suggest removing Task.Factory.StartNew and making ReturnJsonResponse synchronous (or using Task.FromResult if you wanna keep it async) to reduce the cost of thread switching and limit its impact on the execution duration.

@ChengTian
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @PinpointTownes , thank you for your suggestion and detailed explanation. I created an issue for it at #61

@kevinchalet
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, thanks 😄

Please sign in to comment.