Monday, August 15, 2022
HomeSoftware DevelopmentASP.NET Core Diagnostic Situations - Scott Hanselman's Weblog

ASP.NET Core Diagnostic Situations – Scott Hanselman’s Weblog

[ad_1]


ThreadingDavid and buddies has an awesome repository crammed with examples of “damaged patterns” in ASP.NET Core purposes. It is a implausible studying useful resource with each markdown and code that covers quite a lot of frequent areas when writing scalable providers in ASP.NET Core. A number of the steering is common objective however is defined via the lens of writing internet providers.

This is a couple of nice DON’T and DO examples, however remember to Star the repo and test it out for your self! That is considerably superior stuff however in case you are doing excessive output low latency internet providers AT SCALE the following tips will make an enormous distinction while you’re doing a one thing 100 thousand time a second!

DON’T – This instance makes use of the legacy WebClient to make a synchronous HTTP request.

public string DoSomethingAsync()
{
var shopper = new WebClient();
return shopper.DownloadString(http://www.google.com);
}

DO – This instance makes use of an HttpClient to asynchronously make an HTTP request.

static readonly HttpClient shopper = new HttpClient();

public Process<string> DoSomethingAsync()
{
return shopper.GetStringAsync("http://www.google.com");
}

This is a record of ASP.NET Core Steering. This one is fascinating. ASP.NET Core would not buffer responses which permits it to be VERY scalable. Massively so. As such you do have to be conscious that issues must occur in a sure order – Headers come earlier than Physique, and so forth so that you wish to keep away from including headers after the HttpResponse has began.

DON’T – Add headers as soon as you have began sending the physique.

app.Use(async (subsequent, context) =>
{
await context.Response.WriteAsync("Hey ");

await subsequent();

// This will likely fail if subsequent() already wrote to the response
context.Response.Headers["test"] = "worth";
});

DO – Both verify if it is began earlier than you ship the headers:

app.Use(async (subsequent, context) =>
{
await context.Response.WriteAsync("Hey ");

await subsequent();

// Test if the response has already began earlier than including header and writing
if (!context.Response.HasStarted)
{
context.Response.Headers["test"] = "worth";
}
});

And even BETTER, add the headers on the OnStarting name again to ensure they’re getting set.

app.Use(async (subsequent, context) =>
{
// Wire up the callback that can hearth simply earlier than the response headers are despatched to the shopper.
context.Response.OnStarting(() =>
{
context.Response.Headers["test"] = "worth";
return Process.CompletedTask;
});

await subsequent();
});

There is a ton of nice steering round async programming. In case you are returning one thing small or trivial, like a easy worth, DON’T Process<>:

public class MyLibrary
{
public Process<int> AddAsync(int a, int b)
{
return Process.Run(() => a + b);
}
}

DO use ValueTask<> as this instance not solely would not use an additional threads and avoids heap allocation totally:

public class MyLibrary
{
public ValueTask<int> AddAsync(int a, int b)
{
return new ValueTask<int>(a + b);
}
}

There is a ton of fine studying over there so go test it out! https://github.com/davidfowl/AspNetCoreDiagnosticScenarios


Sponsor: Make login Auth0’s downside. Not yours. Present the handy login options your clients need, like social login, multi-factor authentication, single sign-on, passwordless, and extra. Get began without cost.




About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, guide, father, diabetic, and Microsoft worker. He’s a failed stand-up comedian, a cornrower, and a e book writer.

facebook
twitter
subscribe
About   E-newsletter

Internet hosting By
Hosted in an Azure App Service










[ad_2]

RELATED ARTICLES

Most Popular

Recent Comments