Skip to content

Instantly share code, notes, and snippets.

@madhub
Last active November 7, 2022 04:25
Show Gist options
  • Save madhub/b961b64b24b1d5fe3ccaa0d2681ac063 to your computer and use it in GitHub Desktop.
Save madhub/b961b64b24b1d5fe3ccaa0d2681ac063 to your computer and use it in GitHub Desktop.

Table of Contents

  1. Propagate headers with .NET Core Web API
  • StreamConfiguationSource - reading configuration from any Stream source
  • ServiceProviderOptions.ValidateOnBuild - validates the dependencis injected into DI
  • Module Initializers - Module initializers are very useful when you want to eager load something before anything else executes
[ModuleInitializer]
public static void Magic()
{
    // Some powerful stuff here...
}
  • Simplified null checking
  • null coalesce operator and parentheses to automagically instantiate collections
private IList<Foo> _foo;

public IList<Foo> ListOfFoo 
    { get { return _foo ?? (_foo = new List<Foo>()); } }
  • The 'default' keyword in generic types: T t = default(T);
  • LINQ -
"map" => Select
Transforms data from one form into another

"reduce" => Aggregate
Aggregates values into a single result

"filter" => Where
Filters data based on a criteria
  • switch expression
// assign value of the switch to a variable
var text = light.Status switch
{
	ON => "Light is on",
	OFF => "Light is off",
	_ => "Unknown status"
}
  • null-coalescing operators - var variable = MightReturnNull() ?? "default";
  • file-scoped namespaces - namespace MyNameSpace; - begining of file
  • Startup Hooks - Register Code to execute before Program.main
  • IConfigurationRoot.DebugView()

Propagate headers with .NET Core Web API

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.HeaderPropagation" Version="6.0.10" />
  </ItemGroup>
var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
builder.Services.AddHeaderPropagation(o =>
 {
     // propogating the HttpRequest Headers
     o.Headers.Add("Authorization");
 });
builder.Services.AddHttpClient("DemoApp").AddHeaderPropagation();

var app = builder.Build();

// Configure the HTTP request pipeline.

app.UseAuthorization();
app.UseHeaderPropagation();
app.MapControllers();

app.Run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment