-
Notifications
You must be signed in to change notification settings - Fork 526
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Service Bus emulator support (#6737)
- Loading branch information
1 parent
c934862
commit 4494825
Showing
42 changed files
with
2,306 additions
and
120 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using System.Text.Json.Nodes; | ||
using Aspire.Hosting.Azure.ServiceBus; | ||
|
||
var builder = DistributedApplication.CreateBuilder(args); | ||
|
||
var serviceBus = builder.AddAzureServiceBus("sbemulator"); | ||
|
||
serviceBus | ||
.WithQueue("queue1", queue => | ||
{ | ||
queue.DeadLetteringOnMessageExpiration = false; | ||
}) | ||
.WithTopic("topic1", topic => | ||
{ | ||
var subscription = new ServiceBusSubscription("sub1") | ||
{ | ||
MaxDeliveryCount = 10, | ||
}; | ||
topic.Subscriptions.Add(subscription); | ||
|
||
var rule = new ServiceBusRule("app-prop-filter-1") | ||
{ | ||
CorrelationFilter = new() | ||
{ | ||
ContentType = "application/text", | ||
CorrelationId = "id1", | ||
Subject = "subject1", | ||
MessageId = "msgid1", | ||
ReplyTo = "someQueue", | ||
ReplyToSessionId = "sessionId", | ||
SessionId = "session1", | ||
SendTo = "xyz" | ||
} | ||
}; | ||
subscription.Rules.Add(rule); | ||
}) | ||
; | ||
|
||
serviceBus.RunAsEmulator(configure => configure.ConfigureEmulator(document => | ||
{ | ||
document["UserConfig"]!["Logging"] = new JsonObject { ["Type"] = "Console" }; | ||
})); | ||
|
||
builder.AddProject<Projects.ServiceBusWorker>("worker") | ||
.WithReference(serviceBus).WaitFor(serviceBus); | ||
|
||
builder.Build().Run(); |
45 changes: 45 additions & 0 deletions
45
playground/AzureServiceBus/ServiceBus.AppHost/Properties/launchSettings.json
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
{ | ||
"$schema": "http://json.schemastore.org/launchsettings.json", | ||
|
||
"profiles": { | ||
"https": { | ||
"commandName": "Project", | ||
"dotnetRunMessages": true, | ||
"launchBrowser": true, | ||
"applicationUrl": "https://localhost:15887;http://localhost:15888", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development", | ||
"DOTNET_ENVIRONMENT": "Development", | ||
"DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "https://localhost:16175", | ||
"DOTNET_RESOURCE_SERVICE_ENDPOINT_URL": "https://localhost:17037", | ||
"DOTNET_ASPIRE_SHOW_DASHBOARD_RESOURCES": "true" | ||
} | ||
}, | ||
"http": { | ||
"commandName": "Project", | ||
"dotnetRunMessages": true, | ||
"launchBrowser": true, | ||
"applicationUrl": "http://localhost:15888", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development", | ||
"DOTNET_ENVIRONMENT": "Development", | ||
"DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "http://localhost:16175", | ||
"DOTNET_RESOURCE_SERVICE_ENDPOINT_URL": "http://localhost:17038", | ||
"DOTNET_ASPIRE_SHOW_DASHBOARD_RESOURCES": "true", | ||
"ASPIRE_ALLOW_UNSECURED_TRANSPORT": "true" | ||
} | ||
}, | ||
"generate-manifest": { | ||
"commandName": "Project", | ||
"launchBrowser": true, | ||
"dotnetRunMessages": true, | ||
"commandLineArgs": "--publisher manifest --output-path aspire-manifest.json", | ||
"applicationUrl": "http://localhost:15888", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development", | ||
"DOTNET_ENVIRONMENT": "Development", | ||
"DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "http://localhost:16175" | ||
} | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
playground/AzureServiceBus/ServiceBus.AppHost/ServiceBus.AppHost.csproj
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>$(DefaultTargetFramework)</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<IsAspireHost>true</IsAspireHost> | ||
<UserSecretsId>c12f723f-2545-4f8f-8c3b-fb7bdeadbd55</UserSecretsId> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<AspireProjectOrPackageReference Include="Aspire.Hosting.AppHost" /> | ||
<AspireProjectOrPackageReference Include="Aspire.Hosting.Azure.ServiceBus" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\ServiceBusWorker\ServiceBusWorker.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
8 changes: 8 additions & 0 deletions
8
playground/AzureServiceBus/ServiceBus.AppHost/appsettings.Development.json
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
playground/AzureServiceBus/ServiceBus.AppHost/appsettings.json
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning", | ||
"Aspire.Hosting.Dcp": "Warning" | ||
} | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using System.Text; | ||
using Azure.Messaging.ServiceBus; | ||
|
||
namespace ServiceBusWorker; | ||
|
||
internal sealed class Consumer(ServiceBusClient client, ILogger<Consumer> logger) : BackgroundService | ||
{ | ||
protected override async Task ExecuteAsync(CancellationToken cancellationToken) | ||
{ | ||
var processor = client.CreateProcessor("queue1", new ServiceBusProcessorOptions | ||
{ | ||
AutoCompleteMessages = true, | ||
MaxConcurrentCalls = 1, // Process one message at a time | ||
}); | ||
|
||
processor.ProcessMessageAsync += MessageHandler; | ||
|
||
processor.ProcessErrorAsync += ErrorHandler; | ||
|
||
await processor.StartProcessingAsync(cancellationToken); | ||
} | ||
|
||
private Task MessageHandler(ProcessMessageEventArgs args) | ||
{ | ||
// Process the message | ||
logger.LogInformation("Received message: {Message}", Encoding.UTF8.GetString(args.Message.Body)); | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
private Task ErrorHandler(ProcessErrorEventArgs args) | ||
{ | ||
logger.LogError(args.Exception, "Error processing message"); | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
public override Task StopAsync(CancellationToken cancellationToken) | ||
{ | ||
logger.LogInformation("Stopping consumer..."); | ||
return Task.CompletedTask; | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
using Azure.Messaging.ServiceBus; | ||
|
||
namespace ServiceBusWorker; | ||
|
||
internal sealed class Producer(ServiceBusClient client, ILogger<Producer> logger) : BackgroundService | ||
{ | ||
protected override async Task ExecuteAsync(CancellationToken cancellationToken) | ||
{ | ||
logger.LogInformation("Starting producer..."); | ||
|
||
await using var sender = client.CreateSender("queue1"); | ||
|
||
var periodicTimer = new PeriodicTimer(TimeSpan.FromSeconds(5)); | ||
|
||
while (!cancellationToken.IsCancellationRequested) | ||
{ | ||
await periodicTimer.WaitForNextTickAsync(cancellationToken); | ||
|
||
await sender.SendMessageAsync(new ServiceBusMessage($"Hello, World! It's {DateTime.Now} here."), cancellationToken); | ||
} | ||
} | ||
|
||
public override Task StopAsync(CancellationToken cancellationToken) | ||
{ | ||
logger.LogInformation("Stopping producer..."); | ||
return Task.CompletedTask; | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using ServiceBusWorker; | ||
|
||
var builder = Host.CreateApplicationBuilder(args); | ||
|
||
builder.AddServiceDefaults(); | ||
|
||
builder.AddAzureServiceBusClient("sbemulator"); | ||
|
||
builder.Services.AddHostedService<Consumer>(); | ||
builder.Services.AddHostedService<Producer>(); | ||
|
||
var host = builder.Build(); | ||
|
||
await host.RunAsync(); |
Oops, something went wrong.