CleanCodeJN.GenericApis.ServiceBusConsumer 1.0.1

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.1                
NuGet\Install-Package CleanCodeJN.GenericApis.ServiceBusConsumer -Version 1.0.1                
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.1" />                
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.1                
#r "nuget: CleanCodeJN.GenericApis.ServiceBusConsumer, 1.0.1"                
#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.1

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

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 IServiceBusConsumerConfigurationService

public class ServiceBusConsumerConfigurationService(
    IOptionsMonitor<SampleConfiguration> configuration,
    ILogger<ServiceBusConsumerConfigurationService> logger) : IServiceBusConsumerConfigurationService
{
    public virtual bool IsLocalEnvironment() => Debugger.IsAttached;

    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.ServiceBusConnectionString;

    public virtual ServiceBusConfiguration GetServiceBusTopicConfiguration() => configuration.CurrentValue.ServiceBus;

    public virtual void LogIncomingEvent(string name, string body) => logger.LogDebug(EventRequest(name), body);

    public virtual string MaxRetryMessage(ProcessMessageEventArgs args) => "Max Retry reached";

    public virtual void LogMaxRetryReached(ProcessMessageEventArgs args) => logger.LogCritical(message: "Max Retry reached");

    public List<Assembly> GetCommandAssemblies() => [typeof(SendEventRequest).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(body, response), 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 }
    });

    private static string EventResponse(string body, Response response) => $"EventResponse_{JsonSerializer.Deserialize<JsonElement>(body).GetProperty("Name").GetString().Replace(" ", string.Empty)}_{(response.Succeeded ? "Success" : "Failure")}";

    private static string EventRequest(string name) => $"EventRequest_{name.Replace(" ", string.Empty)}";
}

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,
            "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 99 10/21/2024
1.0.7 102 10/20/2024
1.0.6 105 10/20/2024
1.0.5 88 10/9/2024
1.0.4 85 10/8/2024
1.0.3 97 9/27/2024
1.0.2 88 9/27/2024
1.0.1 92 9/25/2024
1.0.0 88 9/25/2024

Reduce necessary packages.