-
-
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.
Sample for a custom IEventConfigurator (#250)
* Added example for a custom IEventConfigurator * Link custom event configurator to docs
- Loading branch information
1 parent
f057581
commit 5ac1e60
Showing
9 changed files
with
148 additions
and
0 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
11 changes: 11 additions & 0 deletions
11
samples/CustomEventConfigurator/CustomEventConfigurator.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,11 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Tingle.EventBus.Transports.Azure.QueueStorage\Tingle.EventBus.Transports.Azure.QueueStorage.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,21 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Tingle.EventBus.Registrations; | ||
|
||
namespace CustomEventConfigurator | ||
{ | ||
class MyConfigurator : IEventConfigurator | ||
{ | ||
public void Configure(EventRegistration registration, EventBusOptions options) | ||
{ | ||
if (registration.EventType == typeof(SampleEvent1)) | ||
{ | ||
registration.EntityKind = Tingle.EventBus.EntityKind.Queue; | ||
} | ||
|
||
if (registration.EventType == typeof(SampleEvent2)) | ||
{ | ||
registration.IdFormat = Tingle.EventBus.EventIdFormat.LongHex; | ||
} | ||
} | ||
} | ||
} |
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,38 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
using Tingle.EventBus.Registrations; | ||
|
||
namespace CustomEventConfigurator | ||
{ | ||
class Program | ||
{ | ||
public static void Main(string[] args) | ||
{ | ||
CreateHostBuilder(args).Build().Run(); | ||
} | ||
|
||
public static IHostBuilder CreateHostBuilder(string[] args) => | ||
Host.CreateDefaultBuilder(args) | ||
.ConfigureServices((hostContext, services) => | ||
{ | ||
services.AddEventBus(builder => | ||
{ | ||
// Transport agnostic configuration | ||
builder.Configure(o => | ||
{ | ||
o.Naming.Scope = "dev"; // queues will be prefixed by 'dev' | ||
o.Naming.UseFullTypeNames = false; | ||
}); | ||
builder.AddConsumer<SampleConsumer1>(); | ||
builder.AddConsumer<SampleConsumer2>(); | ||
|
||
// Setup extra configurators | ||
// You can have as many as you like, these are called after the default one. | ||
builder.Services.AddSingleton<IEventConfigurator, MyConfigurator>(); | ||
|
||
// Transport specific configuration | ||
builder.AddAzureQueueStorageTransport("UseDevelopmentStorage=true;"); | ||
}); | ||
}); | ||
} | ||
} |
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 Microsoft.Extensions.Logging; | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Tingle.EventBus; | ||
|
||
namespace CustomEventConfigurator | ||
{ | ||
public class SampleConsumer1 : IEventConsumer<SampleEvent1> | ||
{ | ||
private readonly ILogger logger; | ||
|
||
public SampleConsumer1(ILogger<SampleConsumer1> logger) | ||
{ | ||
this.logger = logger ?? throw new ArgumentNullException(nameof(logger)); | ||
} | ||
|
||
public Task ConsumeAsync(EventContext<SampleEvent1> context, CancellationToken cancellationToken = default) | ||
{ | ||
logger.LogInformation("Received event Id: {Id}", context.Id); | ||
logger.LogInformation("Event body: {EventBody}", System.Text.Json.JsonSerializer.Serialize(context.Event)); | ||
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,25 @@ | ||
using Microsoft.Extensions.Logging; | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Tingle.EventBus; | ||
|
||
namespace CustomEventConfigurator | ||
{ | ||
public class SampleConsumer2 : IEventConsumer<SampleEvent2> | ||
{ | ||
private readonly ILogger logger; | ||
|
||
public SampleConsumer2(ILogger<SampleConsumer2> logger) | ||
{ | ||
this.logger = logger ?? throw new ArgumentNullException(nameof(logger)); | ||
} | ||
|
||
public Task ConsumeAsync(EventContext<SampleEvent2> context, CancellationToken cancellationToken = default) | ||
{ | ||
logger.LogInformation("Received event Id: {Id}", context.Id); | ||
logger.LogInformation("Event body: {EventBody}", System.Text.Json.JsonSerializer.Serialize(context.Event)); | ||
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,9 @@ | ||
namespace CustomEventConfigurator | ||
{ | ||
public class SampleEvent1 | ||
{ | ||
public string? Make { get; set; } | ||
public string? Model { get; set; } | ||
public int Year { 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,11 @@ | ||
using System; | ||
|
||
namespace CustomEventConfigurator | ||
{ | ||
public class SampleEvent2 | ||
{ | ||
public string? VehicleId { get; set; } | ||
public string? Kind { get; set; } | ||
public DateTimeOffset Opened { get; set; } | ||
} | ||
} |