![]() |
VOOZH | about |
dotnet add package CleanCodeJN.GenericApis.ServiceBusConsumer --version 3.2.0
NuGet\Install-Package CleanCodeJN.GenericApis.ServiceBusConsumer -Version 3.2.0
<PackageReference Include="CleanCodeJN.GenericApis.ServiceBusConsumer" Version="3.2.0" />
<PackageVersion Include="CleanCodeJN.GenericApis.ServiceBusConsumer" Version="3.2.0" />Directory.Packages.props
<PackageReference Include="CleanCodeJN.GenericApis.ServiceBusConsumer" />Project file
paket add CleanCodeJN.GenericApis.ServiceBusConsumer --version 3.2.0
#r "nuget: CleanCodeJN.GenericApis.ServiceBusConsumer, 3.2.0"
#:package CleanCodeJN.GenericApis.ServiceBusConsumer@3.2.0
#addin nuget:?package=CleanCodeJN.GenericApis.ServiceBusConsumer&version=3.2.0Install as a Cake Addin
#tool nuget:?package=CleanCodeJN.GenericApis.ServiceBusConsumer&version=3.2.0Install as a Cake Tool
This CleanCodeJN package for Service Bus simplifies the development of asynchronous microservices by providing a framework that leverages the power of MediatR and IOSP to consume service bus events from topics and execute commands to process these events.
Implement the full IServiceBusConsumerConfigurationService
public class SampleServiceBusConsumerConfigurationService(
IOptionsMonitor<SampleConfiguration> configuration,
ILogger<SampleServiceBusConsumerConfigurationService> logger) : IServiceBusConsumerConfigurationService
{
public virtual bool IsLocalEnvironment() => Environment.GetEnvironmentVariable("IS_LOCAL")?.Equals("true") ?? false;
public virtual void PrintLogoForDebugging() => StringExtensions.PrintLogo();
public virtual string PrintStartTextForDebugging() => "Please add the event as JSON and press ENTER twice.";
public virtual string GetServiceBusConnectionString() => configuration.CurrentValue.ServiceBus.ConnectionString;
public virtual ServiceBusConfiguration GetServiceBusTopicConfiguration() => configuration.CurrentValue.ServiceBus;
public virtual void LogIncomingEvent(string name, string body) => logger.LogDebug($"EventRequest_{name.Replace(" ", string.Empty)}", body);
public virtual string MaxRetryMessage(ProcessMessageEventArgs args) => "Max Retry reached";
public virtual void LogMaxRetryReached(ProcessMessageEventArgs args) => logger.LogCritical(message: MaxRetryMessage(args));
public virtual List<Assembly> GetCommandAssemblies() => [typeof(UpdateInvoiceEventRequest).Assembly];
public virtual Task LogAndHandleException(Exception exception, string message)
{
logger.LogCritical(exception, message);
return Task.CompletedTask;
}
public virtual void LogExecutionResponse(string body, Response response, Exception exception = null) =>
logger.LogDebug($"EventResponse_{JsonSerializer.Deserialize<JsonElement>(body).GetProperty("Name").GetString().Replace(" ", string.Empty)}_{(response.Succeeded ? "Success" : "Failure")}", new Dictionary<string, string>
{
{ nameof(response.Succeeded), response.Succeeded.ToString() },
{ nameof(response.Message), response.Message ?? exception?.Message },
{ nameof(response.Info), response.Info },
{ nameof(exception), exception?.StackTrace },
{ "data", body }
});
}
Or derive from ServiceBusConsumerConfigurationServiceBase for using reasonable defaults and only override the following methods
public class SampleServiceBusConsumerConfigurationService(
IOptionsMonitor<SampleConfiguration> configuration,
ILogger<SampleServiceBusConsumerConfigurationService> logger) : ServiceBusConsumerConfigurationServiceBase(logger)
{
public override ServiceBusConfiguration GetServiceBusTopicConfiguration() => configuration.CurrentValue.ServiceBus;
public override List<Assembly> GetCommandAssemblies() => [typeof(UpdateInvoiceEventRequest).Assembly];
}
Add AddCleanCodeJNServiceBusConsumer<TServiceBusConsumerConfigurationService, TDataContext> to your Program.cs
builder.Services.RegisterServiceBusConsumer<SampleServiceBusConsumerConfigurationService>(
builder.Configuration["ServiceBus:ConnectionString"],
[typeof(UpdateInvoiceEventRequest).Assembly]);
builder.Services.AddCleanCodeJNServiceBusConsumer<SampleServiceBusConsumerConfigurationService, MyDbContext>(options =>
{
options.ApplicationAssemblies =
[
Assembly.GetExecutingAssembly(),
];
options.ServiceBusConnectionString = builder.Configuration["ServiceBus:ConnectionString"];
});
Add Service Bus Configuration to your appsettings.json and your Configuration classes
{
"ServiceBus": {
"MaxRetryCount": 30,
"RetryDelayInMinutes": 10,
"ConnectionString": "",
"TopicConfigurations": [
{
"Name": "topicA",
"SubscriptionName": "general",
"MaxAutoLockRenewalDurationInMinutes": 15,
"PrefetchCount": 10,
"MaxConcurrentCalls": 5
},
{
"Name": "topicB",
"SubscriptionName": "general",
"MaxAutoLockRenewalDurationInMinutes": 15,
"PrefetchCount": 10,
"MaxConcurrentCalls": 5
}
]
}
}
Consume Events by just posting events to the Service Bus
{
"InstanceId": "<Guid>",
"Name": "Event Name",
"Type": "CleanCodeJN.GenericApis.ServiceBusConsumer.Sample.Commands.UpdateInvoiceEventRequest",
"Environment": "Production",
"CreatedAt": "2024-03-25 16:05:50",
"CreatedFrom": "Event Producer",
"RequestId": "Businnss process Id",
"RetryCount": 0,
"Topic": "invoices",
"Data":
{
// Payload
}
}
If you want to use the Generic Apis Commands and Repositories together with the Service Bus Consumer, than register everything as below
var builder = Host.CreateApplicationBuilder(args);
builder.Services.Configure<SampleConfiguration>(builder.Configuration);
builder.Services.AddCleanCodeJNServiceBusConsumer<SampleServiceBusConsumerConfigurationService, MyDbContext>(options =>
{
options.ApplicationAssemblies =
[
typeof(CleanCodeJN.GenericApis.Sample.Business.AssemblyRegistration).Assembly,
typeof(CleanCodeJN.GenericApis.Sample.Core.AssemblyRegistration).Assembly,
typeof(CleanCodeJN.GenericApis.Sample.Domain.AssemblyRegistration).Assembly,
Assembly.GetExecutingAssembly(),
];
options.ServiceBusConnectionString = builder.Configuration["ServiceBus:ConnectionString"];
});
await builder.Build().RunAsync();
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 net10.0 is compatible. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
This package is not used by any NuGet packages.
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 3.2.0 | 87 | 6/10/2026 |
| 3.1.1 | 109 | 4/22/2026 |
| 3.1.0 | 119 | 3/16/2026 |
| 3.0.0 | 318 | 11/12/2025 |
| 2.1.8 | 227 | 10/29/2025 |
| 2.1.7 | 232 | 10/23/2025 |
| 2.1.6 | 165 | 10/18/2025 |
| 2.1.5 | 164 | 10/17/2025 |
| 2.1.4 | 346 | 9/18/2025 |
| 2.1.3 | 261 | 5/28/2025 |
| 2.1.2 | 224 | 5/21/2025 |
| 2.1.1 | 220 | 5/19/2025 |
| 2.1.0 | 549 | 3/25/2025 |
| 2.0.0 | 174 | 2/28/2025 |
| 1.0.8 | 246 | 10/21/2024 |
| 1.0.7 | 204 | 10/20/2024 |
| 1.0.6 | 220 | 10/20/2024 |
| 1.0.5 | 209 | 10/9/2024 |
| 1.0.4 | 203 | 10/8/2024 |
| 1.0.3 | 201 | 9/27/2024 |
Package Updates.