-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
250 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using Azure.Messaging.ServiceBus; | ||
|
||
namespace Example06.Configuration; | ||
|
||
public sealed record Settings | ||
{ | ||
public const string SectionName = "Settings"; | ||
|
||
public string QueueName { get; init; } = default!; | ||
public string ConnectionString { get; init; } = default!; | ||
public RetrySettings RetrySettings { get; init; } = new(); | ||
public TimeSpan ConsumerDelay { get; init; } = TimeSpan.FromSeconds(1); | ||
public TimeSpan ProducerDelay { get; init; } = TimeSpan.FromSeconds(1); | ||
public ServiceBusTransportType TransportType { get; init; } = ServiceBusTransportType.AmqpWebSockets; | ||
} | ||
|
||
public sealed record RetrySettings | ||
{ | ||
public int RetryCount { get; init; } = 3; | ||
public TimeSpan RetryDelay { get; init; } = TimeSpan.FromSeconds(5); | ||
} |
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,26 @@ | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Example06.Configuration; | ||
|
||
public sealed class SettingsValidator : IValidateOptions<Settings> | ||
{ | ||
public ValidateOptionsResult Validate(string? name, Settings? settings) | ||
{ | ||
if (settings is null) | ||
{ | ||
return ValidateOptionsResult.Fail($"{nameof(Settings)} is required."); | ||
} | ||
|
||
if (string.IsNullOrWhiteSpace(settings.QueueName)) | ||
{ | ||
return ValidateOptionsResult.Fail($"{nameof(Settings.QueueName)} is required."); | ||
} | ||
|
||
if (string.IsNullOrWhiteSpace(settings.ConnectionString)) | ||
{ | ||
return ValidateOptionsResult.Fail($"{nameof(Settings.ConnectionString)} is required."); | ||
} | ||
|
||
return ValidateOptionsResult.Success; | ||
} | ||
} |
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,26 @@ | ||
using Example06.Configuration; | ||
using Example06.Contracts; | ||
using Example06.Extensions; | ||
using Microsoft.Extensions.Options; | ||
using Rebus.Handlers; | ||
|
||
namespace Example06.Consumers; | ||
|
||
public sealed class MessageConsumer : IHandleMessages<Message> | ||
{ | ||
private readonly Settings _settings; | ||
private readonly ILogger<MessageConsumer> _logger; | ||
|
||
public MessageConsumer(IOptions<Settings> options, ILogger<MessageConsumer> logger) | ||
{ | ||
_settings = (options ?? throw new ArgumentNullException(nameof(options))).Value; | ||
_logger = logger ?? throw new ArgumentNullException(nameof(logger)); | ||
} | ||
|
||
public async Task Handle(Message message) | ||
{ | ||
_logger.LogConsumedMessage(message.Id); | ||
|
||
await Task.Delay(_settings.ConsumerDelay); | ||
} | ||
} |
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,13 @@ | ||
namespace Example06.Contracts; | ||
|
||
public sealed record Message | ||
{ | ||
public Guid Id { get; init; } | ||
public string Text { get; init; } | ||
|
||
public Message() | ||
{ | ||
Id = Guid.NewGuid(); | ||
Text = $"Text for Id {Id}"; | ||
} | ||
} |
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 Example06.Configuration; | ||
using Example06.Consumers; | ||
using Example06.Contracts; | ||
using Example06.Producers; | ||
using Microsoft.Extensions.Options; | ||
using Rebus.Config; | ||
using Rebus.Routing.TypeBased; | ||
|
||
namespace Example06; | ||
|
||
public static class DependencyInjection | ||
{ | ||
public static void AddServices(this HostApplicationBuilder builder) | ||
{ | ||
builder.AddSettings(); | ||
builder.AddServiceBus(); | ||
} | ||
|
||
private static void AddSettings(this HostApplicationBuilder builder) | ||
{ | ||
builder.Services.Configure<Settings>(builder.Configuration.GetSection(Settings.SectionName)); | ||
builder.Services.AddSingleton<IValidateOptions<Settings>, SettingsValidator>(); | ||
} | ||
|
||
private static void AddServiceBus(this HostApplicationBuilder builder) | ||
{ | ||
var settings = builder.Configuration.GetSettings(); | ||
|
||
builder.Services.AddRebus(cfg => | ||
{ | ||
cfg.Transport(x => x.UseAzureServiceBus(settings.ConnectionString, settings.QueueName)); | ||
cfg.Routing(r => r.TypeBased().Map<Message>(settings.QueueName)); | ||
return cfg; | ||
}); | ||
|
||
builder.Services.AddRebusHandler<MessageConsumer>(); | ||
|
||
builder.Services.AddHostedService<MessageProducer>(); | ||
} | ||
|
||
private static Settings GetSettings(this ConfigurationManager configuration) | ||
{ | ||
var settings = new Settings(); | ||
configuration.GetSection(Settings.SectionName).Bind(settings); | ||
return settings; | ||
} | ||
} |
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 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Worker"> | ||
|
||
<PropertyGroup> | ||
<UserSecretsId>ServiceBusDemo-Example06-UserSecrets</UserSecretsId> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Azure.Identity" /> | ||
<PackageReference Include="Microsoft.Extensions.Hosting" /> | ||
<PackageReference Include="Rebus.AzureServiceBus" /> | ||
<PackageReference Include="Rebus.ServiceProvider" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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,10 @@ | ||
namespace Example06.Extensions; | ||
|
||
public static partial class LoggingExtensions | ||
{ | ||
[LoggerMessage(Level = LogLevel.Information, Message = "Message ({MessageId}) consumed.")] | ||
public static partial void LogConsumedMessage(this ILogger logger, Guid messageId); | ||
|
||
[LoggerMessage(Level = LogLevel.Information, Message = "Message ({MessageId}) produced.")] | ||
public static partial void LogProducedMessage(this ILogger logger, Guid messageId); | ||
} |
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,35 @@ | ||
using Example06.Configuration; | ||
using Example06.Contracts; | ||
using Example06.Extensions; | ||
using Microsoft.Extensions.Options; | ||
using Rebus.Bus; | ||
|
||
namespace Example06.Producers; | ||
|
||
public sealed class MessageProducer : BackgroundService | ||
{ | ||
private readonly IServiceScopeFactory _factory; | ||
private readonly Settings _settings; | ||
private readonly ILogger<MessageProducer> _logger; | ||
|
||
public MessageProducer(IServiceScopeFactory factory, IOptions<Settings> options, ILogger<MessageProducer> logger) | ||
{ | ||
_factory = factory ?? throw new ArgumentNullException(nameof(factory)); | ||
_settings = (options ?? throw new ArgumentNullException(nameof(options))).Value; | ||
_logger = logger ?? throw new ArgumentNullException(nameof(logger)); | ||
} | ||
|
||
protected override async Task ExecuteAsync(CancellationToken cancellationToken) | ||
{ | ||
using var scope = _factory.CreateScope(); | ||
using var bus = scope.ServiceProvider.GetRequiredService<IBus>(); | ||
|
||
while (!cancellationToken.IsCancellationRequested) | ||
{ | ||
var message = new Message(); | ||
await bus.Send(message); | ||
_logger.LogProducedMessage(message.Id); | ||
await Task.Delay(_settings.ProducerDelay, cancellationToken); | ||
} | ||
} | ||
} |
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,6 @@ | ||
using Example06; | ||
|
||
var builder = Host.CreateApplicationBuilder(args); | ||
builder.AddServices(); | ||
var host = builder.Build(); | ||
await host.RunAsync(); |
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,12 @@ | ||
{ | ||
"$schema": "http://json.schemastore.org/launchsettings.json", | ||
"profiles": { | ||
"Example06": { | ||
"commandName": "Project", | ||
"dotnetRunMessages": true, | ||
"environmentVariables": { | ||
"DOTNET_ENVIRONMENT": "Development" | ||
} | ||
} | ||
} | ||
} |
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.Hosting.Lifetime": "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,19 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.Hosting.Lifetime": "Warning" | ||
}, | ||
"Console": { | ||
"FormatterName": "simple", | ||
"FormatterOptions": { | ||
"SingleLine": true, | ||
"TimestampFormat": "HH:mm:ss.ffff " | ||
} | ||
} | ||
}, | ||
"Settings": { | ||
"QueueName": "example06-queue", | ||
"ConnectionString": "Endpoint=sb://namespace.service-bus.windows.net" | ||
} | ||
} |