CleanCodeJN.GenericApis.ServiceBusConsumer 1.0.6

There is a newer version of this package available.
See the version list below for details.
dotnet add package CleanCodeJN.GenericApis.ServiceBusConsumer --version 1.0.6                
NuGet\Install-Package CleanCodeJN.GenericApis.ServiceBusConsumer -Version 1.0.6                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="CleanCodeJN.GenericApis.ServiceBusConsumer" Version="1.0.6" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add CleanCodeJN.GenericApis.ServiceBusConsumer --version 1.0.6                
#r "nuget: CleanCodeJN.GenericApis.ServiceBusConsumer, 1.0.6"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install CleanCodeJN.GenericApis.ServiceBusConsumer as a Cake Addin
#addin nuget:?package=CleanCodeJN.GenericApis.ServiceBusConsumer&version=1.0.6

// Install CleanCodeJN.GenericApis.ServiceBusConsumer as a Cake Tool
#tool nuget:?package=CleanCodeJN.GenericApis.ServiceBusConsumer&version=1.0.6                

Generic Web Apis for Service Bus - Extension Package

Service Bus Consumer with IOSP and Command Pattern

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.

Features

  • Built in Service Bus Consumer for multiple configurable Topics and Subscriptions
  • Uses Mediator and IOSP to execute Commands for processing Events
  • Built in Retry concept
  • Custom Configuration implementation required for event logging and configuration management
  • On latest .NET 8.0

How to use

  • Implement IServiceBusConsumerConfigurationService
  • Add RegisterServiceBusConsumer<YourServiceBusConsumerConfigurationService>() to your Program.cs
  • Add Service Bus Configuration to your appsettings.json and your Configuration classes
  • Consume Events by just posting events to the Service Bus

Step by step explanation

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 RegisterServiceBusConsumer<YourServiceBusConsumerConfigurationService>() to your Program.cs

builder.Services.RegisterServiceBusConsumer<SampleServiceBusConsumerConfigurationService>(
   builder.Configuration["ServiceBus:ConnectionString"],
   [typeof(UpdateInvoiceEventRequest).Assembly]);

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);

List<Assembly> assemblies = [
    typeof(CleanCodeJN.GenericApis.Sample.Business.AssemblyRegistration).Assembly,
    typeof(CleanCodeJN.GenericApis.Sample.Core.AssemblyRegistration).Assembly,
    typeof(CleanCodeJN.GenericApis.Sample.Domain.AssemblyRegistration).Assembly,
    Assembly.GetExecutingAssembly(),
];

builder.Services.Configure<SampleConfiguration>(builder.Configuration);

builder.Services
            .RegisterValidatorsFromAssembly(typeof(CleanCodeJN.GenericApis.Sample.Core.AssemblyRegistration).Assembly)
            .RegisterGenericCommands(assemblies)
            .RegisterAutomapper(assemblies)
            .RegisterServiceBusConsumer<SampleServiceBusConsumerConfigurationService>(builder.Configuration["ServiceBus:ConnectionString"], assemblies)
            .RegisterDbContextAndRepositories<MyDbContext>();

await builder.Build().RunAsync();

Sample Project

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.8 90 10/21/2024
1.0.7 97 10/20/2024
1.0.6 100 10/20/2024
1.0.5 83 10/9/2024
1.0.4 79 10/8/2024
1.0.3 93 9/27/2024
1.0.2 84 9/27/2024
1.0.1 88 9/25/2024
1.0.0 84 9/25/2024

Package Updates for Generic APIs, Hosting.Abstractions and Azure Service Bus Messaging.