Skip to content

Instantly share code, notes, and snippets.

View cgillum's full-sized avatar

Chris Gillum cgillum

View GitHub Profile
@cgillum
cgillum / FetchAccessToken.cs
Created June 9, 2017 19:18
Using on-behalf-of to exchange an id_token for an access_token for MS Graph
public string FetchAccessToken()
{
string idToken = this.Request.Headers["X-MS-TOKEN-AAD-ACCESS-TOKEN"];
string tokenUrl = Environment.GetEnvironmentVariable("WEBSITE_AUTH_OPENID_ISSUER").TrimEnd('/') + "/oauth2/token";
var paramBuilder = new StringBuilder();
paramBuilder.Append("grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer");
paramBuilder.Append("&client_id=").Append(Environment.GetEnvironmentVariable("WEBSITE_AUTH_CLIENT_ID"));
paramBuilder.Append("&client_secret=").Append(Environment.GetEnvironmentVariable("WEBSITE_AUTH_CLIENT_SECRET"));
paramBuilder.Append("&scope=").Append(WebUtility.UrlEncode("https://graph.microsoft.com/user.read"));
@cgillum
cgillum / counter-entity.cs
Last active May 18, 2019 01:23
Example of "counter" entity in Durable Functions.
public static async Task Counter(
[EntityTrigger] IDurableEntityContext ctx)
{
int currentValue = ctx.GetState<int>();
switch (ctx.OperationName)
{
case "add":
int amount = ctx.GetInput<int>();
currentValue += amount;
@cgillum
cgillum / counter-entity-class.cs
Last active May 24, 2019 00:33
Example of "counter" entity written as a C# class
public class CounterEntity
{
[JsonProperty("value")]
public int CurrentValue { get; set; }
public void Add(int amount) => this.CurrentValue += amount;
public void Reset() => this.CurrentValue = 0;
public int Get() => this.CurrentValue;
@cgillum
cgillum / CodeGen.cs
Last active July 19, 2020 23:59
Convert EventSource to ILogger for DurableTask.AzureStorage
namespace ConsoleApp1
{
using System;
using System.Collections.Generic;
using System.Diagnostics.Tracing;
using System.Linq;
using System.Reflection;
using DurableTask.AzureStorage;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
@cgillum
cgillum / DaprWorkflowParallelTaskChaining.cs
Created February 18, 2023 01:00
Shows how to fan out to a multiple parallel task chaining sequences using simple C# constructs in Dapr Workflow.
class ParallelTaskChaining : Workflow<string, string>
{
public override async Task<string> RunAsync(WorkflowContext context, string input)
{
// Fan-out to 10 parallel task chains
List<Task> parallelChains = new();
for (int i = 0; i < 10; i++)
{
// RunSequence encapsulates the task chain as a single task.
Task chainTask = RunSequence(context);
@cgillum
cgillum / LRUCache.cs
Last active June 13, 2023 00:14
Simple LRU cache implementation
public class LRUCache<TKey, TValue>
{
readonly int capacity;
readonly Dictionary<TKey, LinkedListNode<(TKey key, TValue value)>> cache;
readonly LinkedList<(TKey key, TValue value)> list;
readonly object syncLock = new object();
public LRUCache(int capacity)
{
this.capacity = capacity;