Skip to content

Instantly share code, notes, and snippets.

@serpaulius
serpaulius / web.xml
Created March 4, 2020 14:10
.NET Core IIS web.config log to file locally
<!-- ... -->
<aspNetCore processPath="dotnet" arguments=".\Some.API.dll" stdoutLogEnabled="true" stdoutLogFile=".\logs\stdout" hostingModel="inprocess">
<handlerSettings>
<handlerSetting name="debugLevel" value="file" />
<handlerSetting name="debugFile" value=".\logs\debug.log" />
</handlerSettings>
</aspNetCore>
<!-- ... -->
@serpaulius
serpaulius / JsonElementSample.cs
Created March 4, 2020 10:04
.NET Core 3 System.Text.Json JsonElement sample usage
// ...
using System.Text.Json;
// ...
// Get properties from a JSON object
// ...
var contentString = await someHttpClientResponse.Content.ReadAsStringAsync();
using JsonDocument doc = JsonDocument.Parse(String.IsNullOrWhiteSpace(contentString) ? "{}" : contentString);
var jsonElement = doc.RootElement.Clone();
var id = jsonElement.GetProperty("id").GetString();
@serpaulius
serpaulius / SomeService.cs
Last active March 4, 2020 10:19
.NET Core 3 HttpClient (POST) request with bearer token and Accept + Content-Type application/json headers
// ...
// Get a token from some external service
var accessToken = await GetToken();
using var httpClient = httpClientFactory.CreateClient();
var defaultRequestHeaders = httpClient.DefaultRequestHeaders;
// Accept: application/json
if (defaultRequestHeaders.Accept == null || !defaultRequestHeaders.Accept.Any(m => m.MediaType == "application/json"))
{
@serpaulius
serpaulius / Startup.cs
Created March 4, 2020 09:49
.NET Core 3 RabbitMQ connection model with multiple host support
public void ConfigureServices(IServiceCollection services)
{
// ...
services.AddSingleton(new ConnectionFactory()
{
UserName = "guest",
Password = "guest",
Port = 5672,
AutomaticRecoveryEnabled = true
@serpaulius
serpaulius / Startup.cs
Created March 4, 2020 09:40
.NET Core 3 HttpClient via (company) proxy with caller credentials
public void ConfigureServices(IServiceCollection services)
{
//...
services.AddHttpClient("proxified")
.ConfigurePrimaryHttpMessageHandler(() =>
{
return new HttpClientHandler()
{
UseDefaultCredentials = true,
@serpaulius
serpaulius / Startup.cs
Last active March 4, 2020 09:30
.NET Core 3 simple health check with response text
public void ConfigureServices(IServiceCollection services)
{
// ...
services.AddHealthChecks();
// ...
}
// ...
@serpaulius
serpaulius / Startup.cs
Created March 4, 2020 09:24
.NET Core 3 NSwag.AspNetCore with API key auth (X-Api-Key)
public void ConfigureServices(IServiceCollection services)
{
// ...
services.AddSwaggerDocument(config =>
{
config.AddSecurity("x-api-key", Enumerable.Empty<string>(), new OpenApiSecurityScheme
{
Type = OpenApiSecuritySchemeType.ApiKey,
Name = "x-api-key",
@serpaulius
serpaulius / Startup.cs
Created March 4, 2020 08:52
.NET Core 3 wildcard CORS
public void ConfigureServices(IServiceCollection services)
{
// ...
services.AddCors(options =>
{
options.AddDefaultPolicy(builder =>
{
builder.SetIsOriginAllowedToAllowWildcardSubdomains()
.SetIsOriginAllowed(origin => true)