Last active
November 18, 2025 06:23
-
-
Save rkttu/8229a8c28f1e6f32cf6cd18ea234cfe8 to your computer and use it in GitHub Desktop.
A lightweight Aspire orchestration sample based on a .NET 10 file-based app.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env dotnet | |
| #:sdk [email protected] | |
| #:property PublishAot=false | |
| #:package [email protected] | |
| #pragma warning disable ASPIRECSHARPAPPS001 | |
| using Microsoft.Extensions.Configuration; | |
| // To specify password/secrets: | |
| // dotnet user-secrets set --id=apphosttest ConfigName Value | |
| var builder = DistributedApplication.CreateBuilder(args); | |
| builder.Configuration | |
| .AddJsonFile("apphost.json") | |
| .AddUserSecrets("apphosttest") | |
| .AddEnvironmentVariables(); | |
| var garnet = builder.AddGarnet("cache"); | |
| _ = builder.AddCSharpApp("worker", "worker.cs") | |
| .WithReference(garnet) | |
| .WaitFor(garnet); | |
| _ = builder.AddCSharpApp("minapi", "minapi.cs") | |
| .WithReference(garnet) | |
| .WaitFor(garnet) | |
| .WithHttpsEndpoint() | |
| .WithHttpEndpoint(); | |
| using var app = builder.Build(); | |
| app.Run(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "DOTNET_ENVIRONMENT": "Development", | |
| "ASPNETCORE_URLS": "http://localhost:18888", | |
| "ASPIRE_DASHBOARD_OTLP_ENDPOINT_URL": "http://localhost:18889", | |
| "ASPIRE_DASHBOARD_OTLP_HTTP_ENDPOINT_URL": "http://localhost:18890", | |
| "ASPIRE_ALLOW_UNSECURED_TRANSPORT": "true", | |
| "Logging": { | |
| "LogLevel": { | |
| "Default": "Information", | |
| "Microsoft": "Warning", | |
| "Microsoft.Hosting.Lifetime": "Information" | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env dotnet | |
| #:sdk Microsoft.NET.Sdk.Web | |
| #:property PublishAot=false | |
| #:package [email protected] | |
| using Microsoft.AspNetCore.Mvc; | |
| using StackExchange.Redis; | |
| var builder = WebApplication.CreateBuilder(args); | |
| builder.AddRedisClient("cache"); | |
| using var app = builder.Build(); | |
| app.MapGet("/", async ([FromServices] IConnectionMultiplexer cache) => | |
| { | |
| var database = cache.GetDatabase(); | |
| var messageKey = new RedisKey("Message"); | |
| var lastUpdatedKey = new RedisKey("LastUpdated"); | |
| if (!await database.KeyExistsAsync(messageKey)) | |
| await database.StringSetAsync(messageKey, new RedisValue("Hello, World!")); | |
| var message = (await database.StringGetAsync(messageKey)).ToString(); | |
| if (await database.KeyExistsAsync(lastUpdatedKey)) | |
| message += " / " + (await database.StringGetAsync(lastUpdatedKey)).ToString(); | |
| return Results.Ok(message); | |
| }); | |
| app.Run(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env dotnet | |
| #:sdk Microsoft.NET.Sdk.Worker | |
| #:property PublishAot=false | |
| #:package Microsoft.Extensions.Hosting@10.* | |
| #:package [email protected] | |
| using StackExchange.Redis; | |
| var builder = Host.CreateApplicationBuilder(args); | |
| builder.Services.AddHostedService<Worker>(); | |
| builder.AddRedisClient("cache"); | |
| using var app = builder.Build(); | |
| app.Run(); | |
| public sealed class Worker( | |
| ILogger<Worker> logger, | |
| IConnectionMultiplexer cache | |
| ) : BackgroundService | |
| { | |
| protected override async Task ExecuteAsync(CancellationToken stoppingToken) | |
| { | |
| var key = new RedisKey("LastUpdated"); | |
| var database = cache.GetDatabase(); | |
| while (true) | |
| { | |
| var current = DateTimeOffset.Now; | |
| await database.StringSetAsync(key, "Last updated: " + current.ToString()); | |
| logger.LogInformation("Worker running at: {Time}", current); | |
| await Task.Delay(TimeSpan.FromSeconds(1d), stoppingToken); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment