Cheat Sheet: ASP.NET Core
aspnet core

Series of short snippets of FUC. This one is for Azure SDKs.
July 2, 2018

I often find myself searching the internet for the same four lines of C# all over again, because of my inability to remember anything and the complexity of my filesystem structure… That’s why I decided to start putting together quick snippets of Frequently Used Code (FUC).

This one is about ASP.NET Core.

It’s a living document, I keep updating it.

Enable CORS on REST API

Startup.cs -> ConfigureServices():

services.AddCors();

Startup.cs -> Configure():

app.UseCors(builder => builder.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin());

Location of NuGet packages

NuGet packages are installed to:

%UserProfile%\.nuget\packages

Service lifetimes in dependency injection

Transient: Created each time they’re requested from service container. Best for lighweight, stateless services.

Scoped: Created once per client request (connection).

Singleton: Created the first time they’re requested (or when ConfigureServices is run and service is registered with an instance). Every subsequent request uses the same instance.

Source: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-3.0#service-lifetimes

Disposal of services

Only services created by the service container are disposed of automatically.

comments powered by Disqus