Last active
November 27, 2024 16:19
-
-
Save oising/3dd68b7605cae511434ced4971b6551a to your computer and use it in GitHub Desktop.
Allow configuring partitions and consumer groups for Aspire 8/9 event hubs emulator
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
using Aspire.Hosting.Azure; | |
using System.Text.Json; | |
var builder = DistributedApplication.CreateBuilder(args); | |
var eh = builder.AddAzureEventHubs("eh") | |
.RunAsEmulator() | |
.AddEventHub("hub1"); | |
builder.Eventing.Subscribe<AfterEndpointsAllocatedEvent>( | |
async (e, ct) => | |
{ | |
var emulatorResource = builder.Resources | |
.OfType<AzureEventHubsResource>().Single(x => x is | |
{ | |
IsEmulator: true | |
}); | |
string targetPath = "/Eventhubs_Emulator/ConfigFiles/Config.json"; | |
var configFileMount = emulatorResource.Annotations.OfType<ContainerMountAnnotation>() | |
.Single(v => v.Target == targetPath); | |
var json = await File.ReadAllTextAsync(configFileMount.Source!); | |
var config = JsonSerializer.Deserialize<EmulatorConfig>(json); | |
var hub = config!.UserConfig.NamespaceConfig[0].Entities[0]; | |
hub.PartitionCount = "4"; | |
// note: do NOT add $default - it will be added automatically by the emulator | |
hub.ConsumerGroups = [ | |
new() { Name="foo" }, | |
new() { Name = "bar" }]; | |
await using var s = File.OpenWrite(configFileMount.Source!); | |
JsonSerializer.Serialize(s, config); | |
}); | |
builder.AddProject<Projects.dummy>("console") | |
.WithReference(eh); | |
builder.Build().Run(); | |
public class EmulatorConfig | |
{ | |
public UserConfig UserConfig { get; set; } | |
} | |
public class UserConfig | |
{ | |
public List<NamespaceConfig> NamespaceConfig { get; set; } | |
public LoggingConfig LoggingConfig { get; set; } | |
} | |
public class LoggingConfig | |
{ | |
public string Type { get; set; } | |
} | |
public class NamespaceConfig | |
{ | |
public string Type { get; set; } | |
public string Name { get; set; } | |
public List<Entity> Entities { get; set; } | |
} | |
public class Entity | |
{ | |
public string Name { get; set; } | |
public string PartitionCount { get; set; } | |
public List<ConsumerGroup> ConsumerGroups { get; set; } | |
} | |
public class ConsumerGroup | |
{ | |
public string Name { get; set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This worked perfectly for my Aspire 9 project. Many thanks!