Improving performance
See original GitHub issueI’ve been using NSwag for some time now, and have been browsing the generated code on more than one occasion. Today I was made aware of a some work David Fowler is doing to educate the community on best practices around async code. One of his ‘bad’ examples rang a bell.
Assuming your code generation hasn’t undergone major changes recently, NSwag generates code like this:
var responseData_ = response_.Content == null ? null : await response_.Content.ReadAsStringAsync().ConfigureAwait(false);
var result_ = default(AcceptanceReply);
try
{
result_ = Newtonsoft.Json.JsonConvert.DeserializeObject<AcceptanceReply>(responseData_, _settings.Value);
return result_;
}
catch (System.Exception exception_)
{
throw new SwaggerException("Could not deserialize the response body.", (int)response_.StatusCode, responseData_, headers_, exception_);
}
The whole string is read into memory before it gets deserialized into an object.
Fowler is suggesting this improved approach: https://github.com/davidfowl/AspNetCoreDiagnosticScenarios/blob/master/Scenarios/Services/PokemonService.cs#L36
public async Task<PokemonData> GetPokemonAsync()
{
var response = await _client.GetAsync(_url);
// This is a hack to work around the fact that this JSON api returns text/plain
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
// Use the built in methods to read the response as JSON
return await response.Content.ReadAsAsync<PokemonData>();
}
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:24 (21 by maintainers)
Top Results From Across the Web
Top 30 Ways to Improve Work Performance [2023]
Top 3 ways to improve work performance. 1. Limit distractions. According to Udemy In Depth: 2018 Workplace Distraction Report: Most workers (84%) ...
Read more >24 Excellent Ways to Improve Work Performance
1. Limit distractions 2. Use the right tools 3. Plan and prioritize 4. Delegate tasks whenever possible 5. Communicate better 6. Improve your...
Read more >21 Ways to Improve Work Performance & Continuously Grow
Practicing self-care, prioritizing organization, and acquiring new skills are fundamental practices that can help you improve work performance.
Read more >30 Quick Ways to Improve Work Performance and Quality
Read on to learn strategies you can start implementing today to improve your job performance. 1. Stop Multitasking and Start Focusing. One of...
Read more >12 Ways To Improve Your Performance at Work
1. Focus on one task at a time 2. Become more organized 3. Limit distractions 4. Improve communication skills 5. Set stretch goals...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@juanperezADD I was able to implement the change by creating my own
.liquid
templates and putting them in a directory specified in NSwagStudio. I’ll need to polish them up and remove proprietary bits before I can share them here though.That’s me 😃