Workleap.DomainEventPropagation.Subscription
1.0.2-preview.15
Prefix Reserved
See the version list below for details.
dotnet add package Workleap.DomainEventPropagation.Subscription --version 1.0.2-preview.15
NuGet\Install-Package Workleap.DomainEventPropagation.Subscription -Version 1.0.2-preview.15
<PackageReference Include="Workleap.DomainEventPropagation.Subscription" Version="1.0.2-preview.15" />
paket add Workleap.DomainEventPropagation.Subscription --version 1.0.2-preview.15
#r "nuget: Workleap.DomainEventPropagation.Subscription, 1.0.2-preview.15"
// Install Workleap.DomainEventPropagation.Subscription as a Cake Addin #addin nuget:?package=Workleap.DomainEventPropagation.Subscription&version=1.0.2-preview.15&prerelease // Install Workleap.DomainEventPropagation.Subscription as a Cake Tool #tool nuget:?package=Workleap.DomainEventPropagation.Subscription&version=1.0.2-preview.15&prerelease
Workleap.DomainEventPropagation
These libraries must be used in conjunction with Azure Event Grid in order to publish and receive domain events. It is meant to be used in a multi-services architecture where each service is responsible for its own data and publishes events to notify other services of changes. Read more about this in the architecture center
For the offering to work as intended, users need to use the Publishing lbirary and either of the Subscription libraries. It is required to publish domain events with our Publishing library and subscribe to domain events with either the Subscription
or Subscription.PullDelivery
libraries depending on which of the Push or Pull delivery methods is preferred.
Getting started
Limitations
For now, librairies has the following limitations :
- Receiving Cloud Events using push delivery
- Using Event Grid events with Namespace Topics (Microsoft limitation)
- Publishing either Cloud or Event Grid events to multiple topics
- Application Insights telemetry for Cloud Event publishing
- Tracing for Cloud Event publishing
IPublishingDomainEventBehavior
behaviors for pull delivery
Publish domain events
Note that in order to use pull-delivery with Event Grid, you will need to leverage namespace Topic. It's important to know that those topics only support CloudEvent v1.0 schema. More information on namespace topics can be found here
Install the package Workleap.DomainEventPropagation.Publishing in your .NET project that wants to send events to an Event Grid topic. Then, you can use one of the following methods to register the required services.
// Method 1: Automatically bind the options to a well-known configuration section
services.AddEventPropagationPublisher();
// appsetting.json (or any other configuration source)
{
"EventPropagation": {
"Publisher": {
"TopicEndpoint": "<azure_topic_uri>",
"TopicAccessKey": "<secret_value>",
"TopicType": "<custom/namespace>", // Custom if unset
"TopicName": "<azure_namespacetopic_name>" // Mandatory only if topic type = namespace
}
}
}
// Method 2: Automatically bind the options to a well-known configuration section with Azure Identity (RBAC)
services.AddEventPropagationPublisher(options =>
{
options.TokenCredential = new DefaultAzureCredential();
});
// appsetting.json (or any other configuration source)
{
"EventPropagation": {
"Publisher": {
"TopicEndpoint": "<azure_topic_uri>",
"TopicType": "<custom/namespace>", // Custom if unset
"TopicName": "<azure_namespacetopic_name>" // Mandatory only if topic type = namespace
}
}
}
// Method 3: Set options values directly in C#
services.AddEventPropagationPublisher(options =>
{
options.TopicEndpoint = "<azure_topic_uri>";
// Using an access key
options.TopicAccessKey = "<secret_value>";
// Using Azure Identity (RBAC)
options.TokenCredential = new DefaultAzureCredential();
// Topic type, Custom by default
options.TopicType = TopicType.Custom;
// Namespace topic name, mandatory if topic type = namespace
options.TopicName = "<azure_namespacetopic_name>";
});
Note that you can use either an access key or a token credential in order to authenticate to your eventGrid topic, not both.
Then, in order to publish a domain event, you first need to define your domain events using the IDomainEvent
interface.
Decorate the domain event with the [DomainEvent]
attribute, specifying a unique event name.
[DomainEvent("example")]
public class ExampleDomainEvent : IDomainEvent
{
public string Id { get; set; }
}
// Or if you want to specify what schema should be used
[DomainEvent("example", EventSchema.CloudEvent)]
public class ExampleDomainEvent : IDomainEvent
{
public string Id { get; set; }
}
Once your domain events are defined, you can inject and use the IEventPropagationClient
service.
var domainEvent = new ExampleDomainEvent
{
Id = Guid.NewGuid().ToString()
};
await this._eventPropagationClient.PublishDomainEventAsync(domainEvent, CancellationToken.None);
You can specify additional metadata for your domain events to leverage subscription filtering within Azure. This is only supported on domain events that use the CloudEvent schema.
var domainEvent = new ExampleDomainEvent
{
Id = Guid.NewGuid().ToString()
};
await this._eventPropagationClient.PublishDomainEventAsync(domainEvent, x => x.Subject = "<custom_subject>", CancellationToken.None);
Subscribe to domain events with push delivery
Install the package Workleap.DomainEventPropagation.Subscription in your ASP.NET Core project that wants to receive events from Event Grid topics.
You can define your domain event handlers by implementing the IDomainEventHandler<>
interface and then registering them in the service collection later.
public class ExampleDomainEventHandler : IDomainEventHandler<ExampleDomainEvent>
{
public Task HandleDomainEventAsync(ExampleDomainEvent domainEvent, CancellationToken cancellationToken)
{
// Do something with the domain event
return Task.CompletedTask;
}
}
Then, you can use on of the following methods to register the required services and map the webhook endpoint.
// Method 1: Register only selected domain event handlers
services.AddEventPropagationSubscriber()
.AddDomainEventHandler<ExampleDomainEvent, ExampleDomainEventHandler>()
.AddDomainEventHandler<OtherDomainEvent, OtherDomainEventHandler>();
// Method 2: Register all domain event handlers from a given assembly
services.AddEventPropagationSubscriber()
.AddDomainEventHandlers(Assembly.GetExecutingAssembly());
// Register the webhook endpoint in your ASP.NET Core app (startup-based approach)
app.UseEndpoints(builder =>
{
builder.MapEventPropagationEndpoint();
});
// Register the webhook endpoint in your ASP.NET Core app (minimal APIs approach)
app.MapEventPropagationEndpoint();
Securing the webhook endpoint
It is required to expose an ASP.NET Core endpoint in order for Event Grid topics to be able to push events. By default, the registered endpoint will allow anonymous access, but it is possible to secure it as shown below:
// "RequireAuthorization" is a built-in ASP.NET Core method so you can specify any authorization policy you want
app.MapEventPropagationEndpoint().RequireAuthorization();
Now, follow this Microsoft documentation to continue the configuration.
Subscribe to domain events with pull delivery
Install the package Workleap.DomainEventPropagation.Subscription.PullDelivery in your ASP.NET Core project that wants to receive events from Event Grid topics. First, you will need to use one of the following methods to register the required services.
// Method 1: Register to pull delivery and bind the subscription options to the well-known configuration section named EventPropagation:Subscription
services.AddPullDeliverySubscription()
.AddTopicSubscription();
// appsetting.json (or any other configuration source)
{
"EventPropagation": {
"Subscription": {
"TopicEndpoint": "<azure_topic_uri>",
"TopicName": "<namespace_topic_to_listen_to>"
"SubscriptionName": "<subscription_name_under_specified_topic>",
"MaxDegreeOfParallelism": 10,
"TopicAccessKey": "<secret_value>", // Can be omitted to use Azure Identity (RBAC)
}
}
}
// Method 2: Register to pull delivery and bind to multiple subscriptions
services.AddPullDeliverySubscription()
.AddTopicSubscription("EventPropagation:TopicSub1")
.AddTopicSubscription("EventPropagation:TopicSub2");
// appsetting.json (or any other configuration source)
{
"EventPropagation": {
"TopicSub1": {
"TopicEndpoint": "<azure_topic_uri>",
"TopicName": "<namespace_topic_to_listen_to>"
"SubscriptionName": "<subscription_name_under_specified_topic>",
"MaxDegreeOfParallelism": 10,
"TopicAccessKey": "<secret_value>", // Can be omitted to use Azure Identity (RBAC)
},
"TopicSub2": {
"TopicEndpoint": "<azure_topic_uri>",
"TopicName": "<namespace_topic_to_listen_to>"
"SubscriptionName": "<subscription_name_under_specified_topic>",
"MaxDegreeOfParallelism": 10,
"TopicAccessKey": "<secret_value>", // Can be omitted to use Azure Identity (RBAC)
}
}
}
// Method 3: Set options values directly in C#
services.AddPullDeliverySubscription()
.AddTopicSubscription("EventPropagation:TopicSub1", options =>
{
options.TopicEndpoint = "<azure_topic_uri>";
// Namespace topic name
options.TopicName = "<topic_name>";
// Namespace topic subscription name
options.SubscriptionName = "<subscription_name>";
// Maximum degree of parallelism for processing events
options.MaxDegreeOfParallelism = 10;
// Using an access key
options.TopicAccessKey = "<secret_value>";
// Using Azure Identity (RBAC)
options.TokenCredential = new DefaultAzureCredential();
})
.AddTopicSubscription("EventPropagation:TopicSub2", options =>
{
// ...
});
Then you can define your domain event handlers by implementing the IDomainEventHandler<>
interface and register them using
// Method 1: Register only selected domain event handlers
services.AddPullDeliverySubscription()
.AddDomainEventHandler<ExampleDomainEvent, ExampleDomainEventHandler>()
.AddDomainEventHandler<OtherDomainEvent, OtherDomainEventHandler>();
// Method 2: Register all domain event handlers from a given assembly
services.AddPullDeliverySubscription()
.AddDomainEventHandlers(Assembly.GetExecutingAssembly());
// Handler sample
public class ExampleDomainEventHandler : IDomainEventHandler<ExampleDomainEvent>
{
public Task HandleDomainEventAsync(ExampleDomainEvent domainEvent, CancellationToken cancellationToken)
{
// Do something with the domain event
return Task.CompletedTask;
}
}
Configure the underlying Event Grid clients options
You can use the named options pattern to configure the behavior of the underlying Event Grid clients. For instance:
services.Configure<EventGridPublisherClientOptions>(EventPropagationPublisherOptions.EventGridClientName, options =>
{
options.Retry.NetworkTimeout = TimeSpan.FromSeconds(15);
});
Additional notes
- You may only define one domain event handler per domain event you wish to handle. If you would require more, use the single allowed domain event handler as a facade for multiple operations.
- Domain event handlers must have idempotent behavior (you could execute it multiple times for the same event and the result would always be the same).
- If your domain event types and handlers are in dedicated assemblies, you can reference the Workleap.DomainEventPropagation.Abstractions packages in order to avoid a dependency on third-parties like Azure and Microsoft extensions.
- For improved performance, it is possible to run multiple event handlers in parallel by adjusting the
MaxDegreeOfParallelism
option in the subscription configuration. The default value is 1.
Building, releasing and versioning
The project can be built by running Build.ps1
. It uses Microsoft.CodeAnalysis.PublicApiAnalyzers to help detect public API breaking changes. Use the built-in roslyn analyzer to ensure that public APIs are declared in PublicAPI.Shipped.txt
, and obsolete public APIs in PublicAPI.Unshipped.txt
.
A new preview NuGet package is automatically published on any new commit on the main branch. This means that by completing a pull request, you automatically get a new NuGet package.
When you are ready to officially release a stable NuGet package by following the SemVer guidelines, simply manually create a tag with the format x.y.z
. This will automatically create and publish a NuGet package for this version.
Included Roslyn analyzers
Rule ID | Category | Severity | Description |
---|---|---|---|
WLDEP01 | Usage | Warning | Use DomainEvent attribute on event |
WLDEP02 | Usage | Warning | Use unique event name in attribute |
WLDEP03 | Usage | Warning | Ensure event name follows the naming convention |
To modify the severity of one of these diagnostic rules, you can use a .editorconfig
file. For example:
## Disable analyzer for test files
[**Tests*/**.cs]
dotnet_diagnostic.WLDEP01.severity = none
dotnet_diagnostic.WLDEP02.severity = none
dotnet_diagnostic.WLDEP03.severity = none
To learn more about configuring or suppressing code analysis warnings, refer to this documentation.
License
Copyright © 2023, Workleap This code is licensed under the Apache License, Version 2.0. You may obtain a copy of this license at https://github.com/gsoft-inc/gsoft-license/blob/master/LICENSE.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net6.0
- Azure.Messaging.EventGrid (>= 4.25.0)
- OpenTelemetry.Api (>= 1.9.0)
- Workleap.DomainEventPropagation.Abstractions (>= 1.0.2-preview.15)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Workleap.DomainEventPropagation.Subscription:
Package | Downloads |
---|---|
Workleap.DomainEventPropagation.Subscription.ApplicationInsights
Propagate domain events with Azure |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.1.1-preview.9 | 40 | 12/18/2024 |
1.1.1-preview.8 | 37 | 12/18/2024 |
1.1.1-preview.7 | 42 | 12/17/2024 |
1.1.1-preview.6 | 41 | 11/29/2024 |
1.1.1-preview.5 | 39 | 11/17/2024 |
1.1.1-preview.4 | 41 | 10/30/2024 |
1.1.1-preview.3 | 43 | 10/28/2024 |
1.1.1-preview.2 | 40 | 10/28/2024 |
1.1.1-preview.1 | 39 | 10/28/2024 |
1.1.0 | 3,649 | 10/9/2024 |
1.0.2-preview.19 | 49 | 10/9/2024 |
1.0.2-preview.18 | 52 | 10/1/2024 |
1.0.2-preview.17 | 52 | 10/1/2024 |
1.0.2-preview.16 | 51 | 10/1/2024 |
1.0.2-preview.15 | 48 | 10/1/2024 |
1.0.2-preview.14 | 63 | 10/1/2024 |
1.0.2-preview.13 | 37 | 9/20/2024 |
1.0.2-preview.12 | 32 | 9/20/2024 |
1.0.2-preview.11 | 47 | 8/27/2024 |
1.0.2-preview.10 | 48 | 8/27/2024 |
1.0.2-preview.9 | 50 | 8/27/2024 |
1.0.2-preview.7 | 46 | 8/27/2024 |
1.0.2-preview.5 | 41 | 8/27/2024 |
1.0.2-preview.4 | 42 | 8/27/2024 |
1.0.2-preview.3 | 44 | 8/27/2024 |
1.0.2-preview.2 | 45 | 8/27/2024 |
1.0.1 | 1,857 | 8/20/2024 |
1.0.1-preview.2 | 67 | 8/20/2024 |
1.0.1-preview.1 | 68 | 8/16/2024 |
1.0.0 | 281 | 8/15/2024 |
0.6.2-preview.2 | 77 | 8/15/2024 |
0.6.2-preview.1 | 62 | 8/15/2024 |
0.6.1 | 5,342 | 8/15/2024 |
0.6.1-preview.5 | 73 | 8/14/2024 |
0.6.1-preview.4 | 67 | 8/14/2024 |
0.6.1-preview.3 | 62 | 8/12/2024 |
0.6.1-preview.2 | 36 | 8/5/2024 |
0.6.1-preview.1 | 46 | 7/26/2024 |
0.6.0 | 1,834 | 7/18/2024 |
0.5.6-preview.3 | 58 | 7/18/2024 |
0.5.6-preview.2 | 53 | 7/18/2024 |
0.5.6-preview.1 | 54 | 7/18/2024 |
0.5.5-preview.3 | 38 | 7/17/2024 |
0.5.5-preview.2 | 53 | 7/15/2024 |
0.5.4 | 2,228 | 6/19/2024 |
0.5.4-preview.5 | 52 | 6/17/2024 |
0.5.4-preview.4 | 53 | 6/17/2024 |
0.5.4-preview.3 | 51 | 6/14/2024 |
0.5.4-preview.2 | 49 | 6/14/2024 |
0.5.4-preview.1 | 50 | 6/13/2024 |
0.5.3 | 4,142 | 5/21/2024 |
0.5.3-preview.1 | 66 | 5/10/2024 |
0.5.2 | 11,258 | 4/17/2024 |
0.5.2-preview.2 | 71 | 4/12/2024 |
0.5.2-preview.1 | 57 | 4/8/2024 |
0.5.1 | 1,572 | 4/8/2024 |
0.5.1-preview.1 | 64 | 4/8/2024 |
0.4.1-preview.10 | 69 | 4/8/2024 |
0.4.1-preview.9 | 57 | 3/28/2024 |
0.4.1-preview.8 | 62 | 3/26/2024 |
0.4.1-preview.7 | 65 | 3/19/2024 |
0.4.1-preview.6 | 55 | 3/18/2024 |
0.4.1-preview.5 | 63 | 3/8/2024 |
0.4.1-preview.4 | 61 | 3/7/2024 |
0.4.1-preview.3 | 68 | 3/4/2024 |
0.4.1-preview.2 | 62 | 3/4/2024 |
0.4.1-preview.1 | 64 | 2/29/2024 |
0.4.0 | 15,060 | 2/27/2024 |
0.3.1-preview.6 | 67 | 2/26/2024 |
0.3.1-preview.5 | 63 | 2/26/2024 |
0.3.1-preview.4 | 66 | 2/23/2024 |
0.3.1-preview.3 | 64 | 2/16/2024 |
0.3.1-preview.2 | 73 | 2/6/2024 |
0.3.1-preview.1 | 60 | 2/6/2024 |
0.3.0 | 8,483 | 1/26/2024 |
0.2.1-preview.24 | 65 | 1/23/2024 |
0.2.1-preview.23 | 56 | 1/22/2024 |
0.2.1-preview.22 | 71 | 1/12/2024 |
0.2.1-preview.21 | 58 | 1/12/2024 |
0.2.1-preview.20 | 56 | 1/12/2024 |
0.2.1-preview.18 | 80 | 12/18/2023 |
0.2.1-preview.17 | 106 | 12/7/2023 |
0.2.1-preview.16 | 116 | 11/17/2023 |
0.2.1-preview.15 | 93 | 11/13/2023 |
0.2.1-preview.14 | 74 | 11/13/2023 |
0.2.1-preview.13 | 75 | 11/7/2023 |
0.2.1-preview.11 | 138 | 10/24/2023 |
0.2.1-preview.10 | 81 | 10/20/2023 |
0.2.1-preview.8 | 85 | 10/17/2023 |
0.2.1-preview.7 | 78 | 10/13/2023 |
0.2.1-preview.6 | 85 | 10/2/2023 |
0.2.1-preview.5 | 77 | 9/26/2023 |
0.2.1-preview.4 | 69 | 9/21/2023 |
0.2.1-preview.3 | 80 | 9/21/2023 |
0.2.1-preview.2 | 71 | 9/21/2023 |
0.2.1-preview.1 | 72 | 9/21/2023 |
0.2.0 | 12,710 | 9/21/2023 |
0.1.1-preview.6 | 84 | 9/20/2023 |
0.1.1-preview.5 | 73 | 9/20/2023 |
0.1.1-preview.4 | 77 | 9/19/2023 |
0.1.1-preview.3 | 94 | 9/13/2023 |
0.1.1-preview.2 | 85 | 9/13/2023 |
0.1.1-preview.1 | 95 | 9/11/2023 |
0.1.0 | 184 | 9/7/2023 |
0.1.0-preview.33 | 92 | 9/7/2023 |
0.1.0-preview.32 | 81 | 8/25/2023 |
0.1.0-preview.31 | 77 | 8/22/2023 |
0.1.0-preview.30 | 71 | 8/18/2023 |
0.1.0-preview.29 | 82 | 8/16/2023 |
0.1.0-preview.28 | 75 | 8/16/2023 |
0.1.0-preview.27 | 90 | 8/15/2023 |
0.1.0-preview.26 | 80 | 8/14/2023 |
0.1.0-preview.24 | 97 | 8/10/2023 |
0.1.0-preview.23 | 97 | 8/9/2023 |
0.1.0-preview.22 | 94 | 8/4/2023 |
0.1.0-preview.21 | 88 | 8/4/2023 |
0.1.0-preview.20 | 90 | 8/3/2023 |
0.1.0-preview.19 | 92 | 8/1/2023 |
0.1.0-preview.17 | 88 | 8/1/2023 |