Skip to content

Instantly share code, notes, and snippets.

Avatar
🏠
Working from home

Damian Edwards DamianEdwards

🏠
Working from home
View GitHub Profile
@DamianEdwards
DamianEdwards / Program.cs
Created Aug 6, 2022
Some SwashBuckling with .NET 7 rc.1
View Program.cs
using System.Buffers.Text;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.Options;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
@DamianEdwards
DamianEdwards / WebApplicationAuthorizationBuilder.cs
Created Jun 16, 2022
Prototype pseudo-implementation of WebApplicationAuthorizationBuilder
View WebApplicationAuthorizationBuilder.cs
public class WebApplicationAuthorizationBuilder
{
private readonly WebApplicationBuilder _builder;
public WebApplicationAuthorizationBuilder(WebApplicationBuilder builder)
{
_builder = builder;
}
public WebApplicationAuthorizationBuilder AddPolicy(
@DamianEdwards
DamianEdwards / EndpointRoutingBuilders.md
Last active Jun 16, 2022
ASP.NET Core middleware & endpoint routing type & extension hierarchy
View EndpointRoutingBuilders.md
  • IApplicationBuilder Used to configure the application middleware pipeline
    • Map() -> IApplicationBuilder
    • MapWhen() -> IApplicationBuilder
    • Run() -> void
    • Use() -> IApplicationBuilder
    • UseMiddleware() -> IApplicationBuilder
    • UsePathBase() -> IApplicationBuilder
    • UseRouting() -> IApplicationBuilder
    • UseRouter(Action<IRouteBuilder> action) -> IApplicationBuilder
    • UseEndpointPoints() -> IApplicationBuilder
@DamianEdwards
DamianEdwards / Program.cs
Last active Mar 6, 2022
Endpoint Groups
View Program.cs
app.MapGroup("/apis", apis =>
{
apis.UseExceptionHandler("/error");
var problemJsonMediaType = new MediaTypeHeaderValue("application/problem+json");
apis.MapGet("/error", (HttpContext context) =>
{
// Get exception details
var error = context.Features.Get<IExceptionHandlerFeature>()?.Error;
var badRequestEx = error as BadHttpRequestException;
View Program.cs
// Now
app.MapGet("/todos/{id}", async (int id, TodoDb db) =>
await db.Todos.FindAsync(id)
is Todo todo
? Results.Ok(todo)
: Results.NotFound())
.WithName("GetTodoById")
.Produces<Todo>(StatusCodes.Status200OK)
.Produces(StatusCodes.Status404NotFound);
@DamianEdwards
DamianEdwards / Program.cs
Last active Feb 16, 2022
Potential ASP.NET Core Minimal APIs use of C# Discriminated Unions (DU)
View Program.cs
// Ref: https://github.com/cston/csharplang/blob/DiscriminatedUnions/proposals/tagged-unions.md
using System.ComponentModel.DataAnnotations;
using Microsoft.EntityFrameworkCore;
using MiniValidation;
var builder = WebApplication.CreateBuilder(args);
var connectionString = builder.Configuration.GetConnectionString("TodoDb") ?? "Data Source=todos.db";
builder.Services.AddSqlite<TodoDb>(connectionString);
View Microsoft.PowerShell_profile.ps1
function Get-ProgramFiles32() {
if (${env:ProgramFiles(x86)} -ne $null) {
return ${env:ProgramFiles(x86)}
}
return $env:ProgramFiles
}
function Get-VsInstallLocation() {
$programFiles = Get-ProgramFiles32
$vswhere = "$programFiles\Microsoft Visual Studio\Installer\vswhere.exe"
View craver-vs.omp.json
{
"$schema": "https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/schema.json",
"console_title": true,
"console_title_style": "template",
"console_title_template": "{{if .Root}}(Admin) {{end}}{{.Folder}}",
"blocks": [
{
"type": "prompt",
"alignment": "left",
"segments": [
@DamianEdwards
DamianEdwards / vs.omp.json
Last active Oct 6, 2021
Trying to make an oh-my-posh text segment that hides itself when there's no value
View vs.omp.json
{
"type": "text", // VS version
"style": "powerline",
"powerline_symbol": "\uE0C4",
"foreground": "#ffffff",
"background": "#5C2D91", // VS purple from https://visualstudio.microsoft.com/
"properties": {
"prefix": "",
"text": "{{if .Env.VSCMD_VER}} ﬏ {{.Env.VSCMD_VER}} {{end}}",
"postfix": ""
@DamianEdwards
DamianEdwards / ShutdownHostingStartup.cs
Created Mar 15, 2021
ASP.NET Core IHostingStartup that as soon as the site has started makes a request to itself then shuts itself down
View ShutdownHostingStartup.cs
using System;
using System.Linq;
using System.Net.Http;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Hosting.Server;
using Microsoft.AspNetCore.Hosting.Server.Features;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;