-
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
19 changed files
with
247 additions
and
12 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
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,11 @@ | ||
namespace Example05.Configuration; | ||
|
||
public sealed record Settings | ||
{ | ||
public const string SectionName = "Settings"; | ||
|
||
public string QueueName { get; init; } = default!; | ||
public string ConnectionString { get; init; } = default!; | ||
public TimeSpan ConsumerDelay { get; init; } = TimeSpan.FromSeconds(1); | ||
public TimeSpan ProducerDelay { get; init; } = TimeSpan.FromSeconds(1); | ||
} |
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 Example05.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,25 @@ | ||
using Example05.Configuration; | ||
using Example05.Contracts; | ||
using Example05.Extensions; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Example05.Consumers; | ||
|
||
public sealed class MessageConsumer | ||
{ | ||
private readonly IOptions<Settings> _options; | ||
private readonly ILogger<MessageConsumer> _logger; | ||
|
||
public MessageConsumer(IOptions<Settings> options, ILogger<MessageConsumer> logger) | ||
{ | ||
_options = options ?? throw new ArgumentNullException(nameof(options)); | ||
_logger = logger ?? throw new ArgumentNullException(nameof(logger)); | ||
} | ||
|
||
public async Task Handle(Message message) | ||
{ | ||
_logger.LogConsumedMessage(message.Id); | ||
|
||
await Task.Delay(_options.Value.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 Example05.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 Example05.Configuration; | ||
using Example05.Consumers; | ||
using Example05.Contracts; | ||
using Example05.Producers; | ||
using Microsoft.Extensions.Options; | ||
using Wolverine; | ||
using Wolverine.AzureServiceBus; | ||
|
||
namespace Example05; | ||
|
||
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.UseWolverine(options => | ||
{ | ||
options.UseAzureServiceBus(settings.ConnectionString).AutoProvision(); | ||
options.PublishMessage<Message>().ToAzureServiceBusQueue(settings.QueueName); | ||
options.ListenToAzureServiceBusQueue(settings.QueueName); | ||
}); | ||
|
||
builder.Services.AddSingleton<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,13 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Worker"> | ||
|
||
<PropertyGroup> | ||
<UserSecretsId>ServiceBusDemo-Example05-UserSecrets</UserSecretsId> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.Hosting" /> | ||
<PackageReference Include="System.Text.Json" /> | ||
<PackageReference Include="WolverineFx.AzureServiceBus" /> | ||
</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 Example05.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 Example05.Configuration; | ||
using Example05.Contracts; | ||
using Example05.Extensions; | ||
using Microsoft.Extensions.Options; | ||
using Wolverine; | ||
|
||
namespace Example05.Producers; | ||
|
||
public sealed class MessageProducer : BackgroundService | ||
{ | ||
private readonly IServiceScopeFactory _factory; | ||
private readonly IOptions<Settings> _options; | ||
private readonly ILogger<MessageProducer> _logger; | ||
|
||
public MessageProducer(IServiceScopeFactory factory, IOptions<Settings> options, ILogger<MessageProducer> logger) | ||
{ | ||
_factory = factory ?? throw new ArgumentNullException(nameof(factory)); | ||
_options = options ?? throw new ArgumentNullException(nameof(options)); | ||
_logger = logger ?? throw new ArgumentNullException(nameof(logger)); | ||
} | ||
|
||
protected override async Task ExecuteAsync(CancellationToken cancellationToken) | ||
{ | ||
using var scope = _factory.CreateScope(); | ||
var bus = scope.ServiceProvider.GetRequiredService<IMessageBus>(); | ||
|
||
while (!cancellationToken.IsCancellationRequested) | ||
{ | ||
var message = new Message(); | ||
await bus.SendAsync(message); | ||
_logger.LogProducedMessage(message.Id); | ||
await Task.Delay(_options.Value.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 Example05; | ||
|
||
var builder = Host.CreateApplicationBuilder(args); | ||
builder.AddServices(); | ||
var host = builder.Build(); | ||
host.Run(); |
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": { | ||
"Example05": { | ||
"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": "example05-queue", | ||
"ConnectionString": "Endpoint=sb://namespace.service-bus.windows.net" | ||
} | ||
} |