Skip to content

Instantly share code, notes, and snippets.

View afscrome's full-sized avatar

Alex Crome afscrome

View GitHub Profile
// This is an example of using the Aspire Interaction service to prompt users
// to install a missing dependencies if it's not present.
//
// Ideally you can get all your dependencies through `dotnet restore`, or other package mangers that run as part of your app host,
// but that may not be possible, particularly if third parties don't provide package manager friendly setup.
//
// `Helper.IsInstalled()` and `Helper.Install()` implementations are not included and will vary based on your dependency
public static T WithDependencyInstaller<T>(this T builder)
where T : IDistributedApplicationBuilder
// Define our initial value
var value = "initial value";
resource
// Important - need to use a lambda here to ensure the latest value is loaded when the app is restarted
.WithEnvironment("Dynamic", () => value)
// Add a custom command, when invoked, this
// 1. uses the interaction service to prompt for a value
// 2. Update the value used in the `WithEnvironment` lambda
// 3. Restarts the resource to force it to pick up the new env var.
@afscrome
afscrome / ConfigFiles.cs
Last active June 10, 2024 16:44
Writing Config Files with Aspire
public record class ConfigFileContext(
DistributedApplicationModel AppModel,
Stream Stream,
CancellationToken CancellationToken
);
[DebuggerDisplay($"{{{nameof(Path)},nq}}")]
public class ConfigFileAnnotation(string path, Func<ConfigFileContext, Task> generator) : IResourceAnnotation
{
public FileInfo Path { get; } = new FileInfo(path);
@afscrome
afscrome / DelayStartAnnotation.cs
Last active May 16, 2024 11:28
Aspire Readiness checks
public class DelayStartAnnotation(IResource waitForResource) : IResourceAnnotation
{
public IResource WaitForResource { get; } = waitForResource;
}
public class BackgroundRefreshableCache<TKey, TValue>
{
public TimeSpan RefreshInterval { get; }
private readonly Func<TKey, Task<TValue>> _loader;
private ImmutableDictionary<TKey, Task<TValue>> _cache = ImmutableDictionary<TKey, Task<TValue>>.Empty;
public BackgroundRefreshableCache(Func<TKey, Task<TValue>> loader)
{
_loader = loader;
}
@afscrome
afscrome / Program.cs
Created November 1, 2019 15:20
Cosmos Slow Networking
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;
namespace Cosmos
{
class Program
{
@afscrome
afscrome / Program.cs
Created October 23, 2019 17:28
Cosmos Slow Opening
// If you run the following with the cosmos emulator disabled, it takes about 2 minutes before the OpenAsync call fails
// Comment out the SetLocation line, and it takes about 8.
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using Microsoft.Azure.Documents.Client;
namespace CosmosTimeoutTest
{
@afscrome
afscrome / BackgroundRefreshable.cs
Created June 25, 2018 16:07
Background Refreshable
using NUnit.Framework;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace BackgroundRefreshable
{
///<summary>
/// Provides support for loading, storing and refreshing a value asyncronously.
///
@afscrome
afscrome / InstallSolr.ps1
Created November 28, 2016 23:26
Script for installing solr as a windows service
#Requires -Version 3.0
<#
.Description
Installs Apache Solr
.Example
.\ServiceInstall.ps1
Installs Apache Solr as the 'solr' service running on port 8983.
#This script calculates how many PRs each person merged
param(
[Parameter(Mandatory=$true)]
[string]$PersonalAccessToken,
[Parameter(Mandatory=$true)]
[string]$OrgOrUser,
[Parameter(Mandatory=$true)]
[string]$RepoName
)
$base64Token = [Convert]::ToBase64String([char[]]$PersonalAccessToken)