question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Improving performance

See original GitHub issue

I’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:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:24 (21 by maintainers)

github_iconTop GitHub Comments

3reactions
daipluspluscommented, Feb 21, 2019

@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.

2reactions
daipluspluscommented, Oct 31, 2018

Nice, maybe you also sync with @Jehoel here: #1697

That’s me 😃

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found