Skip to content

Instantly share code, notes, and snippets.

@madhub
Last active February 10, 2024 03:14
Show Gist options
  • Save madhub/1dd5646c8d9ecdee25e32ab00e02d7d5 to your computer and use it in GitHub Desktop.
Save madhub/1dd5646c8d9ecdee25e32ab00e02d7d5 to your computer and use it in GitHub Desktop.
tips

Temmp https://github.com/jasonterando/S3BufferedUpload

Enabling verbose logging to see any AWS S3 SDK logic :

Amazon.AWSConfigs.LoggingConfig.LogResponses = Amazon.ResponseLoggingOption.Always;
Amazon.AWSConfigs.LoggingConfig.LogTo = Amazon.LoggingOptions.Console;
Amazon.AWSConfigs.AddTraceListener("Amazon", new System.Diagnostics.ConsoleTraceListener());

Generate Random bytes

var bytes = new byte[size];
RandomNumberGenerator.Create().GetBytes(bytes);

Exploring The AsyncLocal

https://dzone.com/articles/quick-and-efficient-distributed-tracing-in-net

Enable ASP.NET COre to use HTTP2 for non TLS connections

Add below entry to the appsettings or configure programatically .Non TLS HTTP2 does not allow protocol negotiation More information here

{
  "Kestrel": {
    "EndpointDefaults": {
      "Protocols": "Http2"
    }
  }
}
//HttpClient myHttpClient = new HttpClient(new HttpClientHandler ()
//{
//   ServerCertificateCustomValidationCallback  = (_,_, _, _) => true
//})
HttpClient myHttpClient = new HttpClient()
{
    DefaultRequestVersion = HttpVersion.Version20,
    DefaultVersionPolicy = HttpVersionPolicy.RequestVersionExact
   
};
string requestUrl = "http://localhost:5123/weatherforecast";
try
{
    Console.WriteLine($"GET {requestUrl}.");
    HttpResponseMessage response = await myHttpClient.GetAsync(requestUrl);
    response.EnsureSuccessStatusCode();
    Console.WriteLine($"Response HttpVersion: {response.Version}");
    string responseBody = await response.Content.ReadAsStringAsync();
    Console.WriteLine($"Response Body Length is: {responseBody.Length}");
    Console.WriteLine($"------------Response Body------------");
    Console.WriteLine(responseBody);
    Console.WriteLine($"------------End of Response Body------------");
}
catch (HttpRequestException e)
{
    Console.WriteLine($"HttpRequestException : {e.Message}");
}
Console.WriteLine($"Press Enter to exit....");
Console.ReadLine();

Using http/2 with ASP.NET Core - Traefik/Kubernetes/container example

https://josef.codes/using-http2-with-asp-net-core-traefik-kubernetes-container-example/ image

Adding Generic Type to Dependency injection

Complete alternative example

public class MessageHolder<T>
{
    public T Message { get; set; }
}
builder.Services.AddScoped(typeof(MessageHolder<>));

public class WeatherForecastController{
public WeatherForecastController(MessageHolder<string> messageHolder,MessageHolder<StringBuilder> messageHolder1) {
}
}

HttpRequest & Response Formatter

https://github.com/Squidex/squidex/blob/6b7c6fc8a6df01cac9e26bde575621c06b491e67/backend/src/Squidex.Infrastructure/Http/DumpFormatter.cs

Handling CancelKeyPress using a CancellationToken

https://www.meziantou.net/handling-cancelkeypress-using-a-cancellationtoken.htm

C# – How to make concurrent requests with HttpClien

https://makolyte.com/csharp-how-to-make-concurrent-requests-with-httpclient/

DotNet command with logs

dotnet build my_project.csproj /binaryLogger

ASP.NET Core Route constraints

https://learn.microsoft.com/en-us/aspnet/core/fundamentals/routing?WT.mc_id=DT-MVP-5003978&view=aspnetcore-7.0#route-constraints

Mocking an HttpClient using ASP.NET Core TestServer

https://www.meziantou.net/mocking-an-httpclient-using-asp-net-core-testserver.htm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment