-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support the use of IConfiguration (#487)
This PR adds support for configuring the EventBus, its transports, events and consumers via `IConfiguration`. It eases the getting started process and allows overriding certain behavior without changing code for example changing the transport's connection string in production and development. A sample that uses `IConfiguration` from `appsettings.json` has also been added.
- Loading branch information
1 parent
146a63c
commit a5c8c4f
Showing
43 changed files
with
677 additions
and
157 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Worker"> | ||
|
||
<PropertyGroup> | ||
<UserSecretsId>fdb14b87-4a29-455c-9912-67a1e0c64081</UserSecretsId> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Azure.Identity" Version="1.8.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Tingle.EventBus.Transports.Azure.ServiceBus\Tingle.EventBus.Transports.Azure.ServiceBus.csproj" /> | ||
<ProjectReference Include="..\..\src\Tingle.EventBus.Transports.InMemory\Tingle.EventBus.Transports.InMemory.csproj" /> | ||
</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,15 @@ | ||
namespace ConfigSample; | ||
|
||
internal class ImageUploaded | ||
{ | ||
public string? ImageId { get; set; } | ||
public string? Url { get; set; } | ||
public long SizeBytes { get; set; } | ||
} | ||
|
||
internal class VideoUploaded | ||
{ | ||
public string? VideoId { get; set; } | ||
public string? Url { get; set; } | ||
public long SizeBytes { get; set; } | ||
} |
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,31 @@ | ||
using Azure.Identity; | ||
using ConfigSample; | ||
using Tingle.EventBus.Transports.Azure.ServiceBus; | ||
|
||
var host = Host.CreateDefaultBuilder(args) | ||
.ConfigureServices((hostContext, services) => | ||
{ | ||
var configuration = hostContext.Configuration; | ||
|
||
services.AddEventBus(builder => | ||
{ | ||
builder.AddConsumer<VehicleTelemetryEventsConsumer>(); | ||
builder.AddConsumer<VisualsUploadedConsumer>(); | ||
|
||
// Add transports | ||
builder.AddAzureServiceBusTransport(); | ||
builder.AddInMemoryTransport("in-memory-images"); | ||
builder.AddInMemoryTransport("in-memory-videos"); | ||
|
||
// Transport specific configuration | ||
var credential = new DefaultAzureCredential(); | ||
builder.Services.PostConfigure<AzureServiceBusTransportOptions>( | ||
name: AzureServiceBusDefaults.Name, | ||
configureOptions: o => ((AzureServiceBusTransportCredentials)o.Credentials).TokenCredential = credential); | ||
}); | ||
|
||
services.AddHostedService<VisualsProducerService>(); | ||
}) | ||
.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,11 @@ | ||
{ | ||
"profiles": { | ||
"ConfigSample": { | ||
"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,26 @@ | ||
using System.Text.Json.Nodes; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace ConfigSample; | ||
|
||
internal class VehicleDoorOpenedEvent | ||
{ | ||
public string? VehicleId { get; set; } | ||
public VehicleDoorKind Kind { get; set; } | ||
public DateTimeOffset? Opened { get; set; } | ||
public DateTimeOffset? Closed { get; set; } | ||
} | ||
|
||
internal class VehicleTelemetryEvent | ||
{ | ||
public string? DeviceId { get; set; } | ||
public DateTimeOffset Timestamp { get; set; } | ||
public string? Action { get; set; } | ||
public VehicleDoorKind? VehicleDoorKind { get; set; } | ||
public VehicleDoorStatus? VehicleDoorStatus { get; set; } | ||
[JsonExtensionData] | ||
public JsonObject? Extras { get; set; } | ||
} | ||
|
||
internal enum VehicleDoorStatus { Unknown, Open, Closed, } | ||
internal enum VehicleDoorKind { FrontLeft, FrontRight, RearLeft, ReadRight, Hood, Trunk, } |
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,42 @@ | ||
namespace ConfigSample; | ||
|
||
internal class VehicleTelemetryEventsConsumer : IEventConsumer<VehicleTelemetryEvent> | ||
{ | ||
private readonly ILogger logger; | ||
|
||
public VehicleTelemetryEventsConsumer(ILogger<VehicleTelemetryEventsConsumer> logger) | ||
{ | ||
this.logger = logger ?? throw new ArgumentNullException(nameof(logger)); | ||
} | ||
|
||
public async Task ConsumeAsync(EventContext<VehicleTelemetryEvent> context, CancellationToken cancellationToken) | ||
{ | ||
var telemetry = context.Event; | ||
|
||
var status = telemetry.VehicleDoorStatus; | ||
if (status is not VehicleDoorStatus.Open and not VehicleDoorStatus.Closed) | ||
{ | ||
logger.LogWarning("Vehicle Door status '{VehicleDoorStatus}' is not yet supported", status); | ||
return; | ||
} | ||
|
||
var kind = telemetry.VehicleDoorKind; | ||
if (kind is null) | ||
{ | ||
logger.LogWarning("Vehicle Door kind '{VehicleDoorKind}' cannot be null", kind); | ||
return; | ||
} | ||
|
||
var timestamp = telemetry.Timestamp; | ||
var updateEvt = new VehicleDoorOpenedEvent | ||
{ | ||
VehicleId = telemetry.DeviceId, | ||
Kind = kind.Value, | ||
Closed = status is VehicleDoorStatus.Closed ? timestamp : null, | ||
Opened = status is VehicleDoorStatus.Open ? timestamp : null, | ||
}; | ||
|
||
// the VehicleDoorOpenedEvent on a broadcast bus would notify all subscribers | ||
await context.PublishAsync(updateEvt, cancellationToken: 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,42 @@ | ||
namespace ConfigSample; | ||
|
||
internal class VisualsProducerService : BackgroundService | ||
{ | ||
private readonly IEventPublisher publisher; | ||
private readonly ILogger logger; | ||
|
||
public VisualsProducerService(IEventPublisher publisher, ILogger<VisualsProducerService> logger) | ||
{ | ||
this.publisher = publisher ?? throw new ArgumentNullException(nameof(publisher)); | ||
this.logger = logger ?? throw new ArgumentNullException(nameof(logger)); | ||
} | ||
|
||
protected override async Task ExecuteAsync(CancellationToken stoppingToken) | ||
{ | ||
await Task.Delay(TimeSpan.FromSeconds(2), stoppingToken); // delays a little so that the logs are better visible in a better order (only ended for sample) | ||
|
||
logger.LogInformation("Starting production ..."); | ||
|
||
var delay = TimeSpan.FromSeconds(20); | ||
var times = 10; | ||
|
||
var rnd = new Random(DateTimeOffset.UtcNow.Millisecond); | ||
|
||
for (var i = 0; i < times; i++) | ||
{ | ||
var id = Convert.ToUInt32(rnd.Next()).ToString(); | ||
var size = Convert.ToUInt32(rnd.Next()); | ||
var image = (i % 2) == 0; | ||
var url = $"https://localhost:8080/{(image ? "images" : "videos")}/{id}.{(image ? "png" : "flv")}"; | ||
|
||
_ = image | ||
? await DoPublishAsync(new VideoUploaded { VideoId = id, SizeBytes = size, Url = url, }, stoppingToken) | ||
: await DoPublishAsync(new ImageUploaded { ImageId = id, SizeBytes = size, Url = url, }, stoppingToken); | ||
|
||
await Task.Delay(delay, stoppingToken); | ||
} | ||
} | ||
|
||
private async Task<ScheduledResult?> DoPublishAsync<T>(T @event, CancellationToken cancellationToken) where T : class | ||
=> await publisher.PublishAsync(@event, cancellationToken: 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,31 @@ | ||
namespace ConfigSample; | ||
|
||
internal class VisualsUploadedConsumer : IEventConsumer<ImageUploaded>, IEventConsumer<VideoUploaded> | ||
{ | ||
private static readonly TimeSpan SimulationDuration = TimeSpan.FromSeconds(1.3f); | ||
|
||
private readonly ILogger logger; | ||
|
||
public VisualsUploadedConsumer(ILogger<VisualsUploadedConsumer> logger) | ||
{ | ||
this.logger = logger ?? throw new ArgumentNullException(nameof(logger)); | ||
} | ||
|
||
public async Task ConsumeAsync(EventContext<ImageUploaded> context, CancellationToken cancellationToken) | ||
{ | ||
var id = context.Event.ImageId; | ||
var thumbnailUrl = $"https://localhost:8080/thumbnails/{id}.jpg"; | ||
|
||
await Task.Delay(SimulationDuration, cancellationToken); | ||
logger.LogInformation("Generated thumbnail from image '{ImageId}' at '{ThumbnailUrl}'.", id, thumbnailUrl); | ||
} | ||
|
||
public async Task ConsumeAsync(EventContext<VideoUploaded> context, CancellationToken cancellationToken = default) | ||
{ | ||
var id = context.Event.VideoId; | ||
var thumbnailUrl = $"https://localhost:8080/thumbnails/{id}.jpg"; | ||
|
||
await Task.Delay(SimulationDuration, cancellationToken); | ||
logger.LogInformation("Generated thumbnail from video '{VideoId}' at '{ThumbnailUrl}'.", id, thumbnailUrl); | ||
} | ||
} |
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,16 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Debug", | ||
"Microsoft": "Information", | ||
"System": "Information" | ||
}, | ||
"Console": { | ||
"FormatterName": "simple", | ||
"FormatterOptions": { | ||
"SingleLine": true, | ||
"TimestampFormat": "HH:mm:ss " | ||
} | ||
} | ||
} | ||
} |
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,46 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft": "Warning", | ||
"Microsoft.Hosting.Lifetime": "Information" | ||
} | ||
}, | ||
|
||
"EventBus": { | ||
"WaitTransportStarted": false, | ||
"Naming": { | ||
"Convention": "DotCase", | ||
"UseFullTypeNames": false | ||
}, | ||
"DefaultTransportName": "azure-service-bus", | ||
"Transports": { // keyed by name of the transport | ||
"azure-service-bus": { | ||
"DefaultEntityKind": "Queue", // required if using the basic SKU (does not support topics) | ||
"FullyQualifiedNamespace": "{your_namespace}.servicebus.windows.net" | ||
}, | ||
"in-memory-images": { | ||
"DefaultEventIdFormat": "DoubleLongHex" | ||
}, | ||
"in-memory-videos": { | ||
"DefaultEntityKind": "Queue" | ||
} | ||
}, | ||
"Events": { | ||
"ConfigSample.ImageUploaded": { // FullName of the type | ||
"TransportName": "in-memory-images" | ||
}, | ||
"ConfigSample.VideoUploaded": { // FullName of the type | ||
"TransportName": "in-memory-videos", | ||
"Consumers": { | ||
"ConfigSample.VisualsUploadedConsumer": { // FullName of the type | ||
"UnhandledErrorBehaviour": "Discard", | ||
"Metadata": { | ||
"generation": "2022" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
15 changes: 12 additions & 3 deletions
15
src/Tingle.EventBus.Transports.Amazon.Abstractions/AmazonTransportConfigureOptions.cs
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
Oops, something went wrong.