LightMediator 0.5.1.4

dotnet add package LightMediator --version 0.5.1.4
                    
NuGet\Install-Package LightMediator -Version 0.5.1.4
                    
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="LightMediator" Version="0.5.1.4" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="LightMediator" Version="0.5.1.4" />
                    
Directory.Packages.props
<PackageReference Include="LightMediator" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add LightMediator --version 0.5.1.4
                    
#r "nuget: LightMediator, 0.5.1.4"
                    
#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.
#:package LightMediator@0.5.1.4
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=LightMediator&version=0.5.1.4
                    
Install as a Cake Addin
#tool nuget:?package=LightMediator&version=0.5.1.4
                    
Install as a Cake Tool

LightMediator

NuGet License

LightMediator is a lightweight library designed to simplify decoupled communication in distributed Windows services. It works with multiple notification types and supports services across different namespaces.

Features

  • Lightweight and efficient.
  • Supports publish-subscribe, request-response, and one-way notifications.
  • Simplifies working with decoupled services in distributed systems.
  • Easy to integrate with existing applications.
  • Supports commands and command handlers for request-response communication.

Installation

You can install the LightMediator NuGet package using the following command:

dotnet add package LightMediator

Usage

Publish Notifications

You can publish notifications to subscribers using the LightMediator instance:

  private readonly IMediator _mediator;
  public SampleService(IMediator mediator)
  {
      _deviceService = deviceService;
  }
.
.
    NewAppInfoNotification newAppInfo = new NewAppInfoNotification()
    {
        Title = "NewApp",
        Description = "Des"
    };
    await mediator.Publish(newAppInfo);

Handle Notifications

To handle notifications, create a class that implements the INotificationHandler<T> interface. Ensure that Notification implements the INotification interface:

public class NewAppInfoNotification : INotification
{
    public string Title { get; set; }
    public string? Description { get; set; }
}


public class NewAppInfoNotificationHandler : NotificationHandler<NewAppInfoNotification>
{ 
    public override async Task Handle(NewAppInfoNotification notification, CancellationToken? cancellationToken)
    {
           // Handle the recieved notification
    }
}

Use Commands and Command Handlers

LightMediator also supports commands and command handlers to facilitate request-response communication.

Define a Command

A command is a class that implements the IRequest<TResponse> interface, where TResponse is the type of the response the command expects.


public class TestCommandWithResponse : IRequest<TestCommandResponse>
{
    public string Title { get; set; }
    public string Description { get; set; }
}

public class TestCommandResponse
{
    public string Message { get; set; }
}
Define a Command Handler

A command handler is a class that implements the IRequestHandler<TRequest, TResponse> interface. The handler contains the logic for processing the command.


public class TestCommandWithResponseHandler : RequestHandler<TestCommandWithResponse, TestCommandResponse>
{
    private readonly ILogger<TestCommandWithResponseHandler> _logger;
    public TestCommandWithResponseHandler(ILogger<TestCommandWithResponseHandler> logger) 
    {
        _logger = logger;
    }

    public override async Task<TestCommandResponse> Handle(TestCommandWithResponse request, CancellationToken? cancellationToken)
    {
        await Task.CompletedTask; 
        return new TestCommandResponse
        {
            Message = "command executed"
        };
    }
}


Sending a Command

You can send a command and get a response using the Send method from LightMediator.

 public class Worker : BackgroundService
 {
     private readonly ILogger<Worker> _logger;
     private readonly IMediator _mediator;
     public Worker(ILogger<Worker> logger,IMediator mediator)
     {
         _mediator = mediator;
         _logger = logger;
     }

     protected override async Task ExecuteAsync(CancellationToken stoppingToken)
     {
         while (!stoppingToken.IsCancellationRequested)
         {
             var res = await _mediator.Send<bool>(new TestCommandResponse()
             {
                 Title = "Test",
                 Description = "Test",
             });

             if (_logger.IsEnabled(LogLevel.Information))
             {
                 _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
             }
             await Task.Delay(1000, stoppingToken);
         }
     }
 }

Registering Commands , Notifications and Handlers

You can register handlers with the dependency injection container in your application startup:

// Add Mediator to the Dependency Injection container
builder.Services.AddLightMediator(options =>
{
    // Configure options for the mediator
    options.IgnoreNamespaceInAssemblies = true;
    options.IgnoreNotificationDifferences = true;
    options.RegisterNotificationsByAssembly = true;
    options.RegisterRequestsByAssembly = true;

    //Specify the assemblies to scan for notification and request
    options.Assemblies = new[]
    {
        Assembly.GetExecutingAssembly(),
        ServiceAExtensions.GetServiceAssembly(),
        ServiceBExtensions.GetServiceAssembly(),
        ServiceCExtensions.GetServiceAssembly()
    };
});

License

This project is licensed under the MIT License. See the LICENSE file for details.


Created with ❤️ by @omidmloo

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on LightMediator:

Package Downloads
LightMediator.EventBus

EventBus capability to publish events between applications using different messaging systems

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.5.1.4 196 5/23/2025
0.5.1.3 170 5/21/2025
0.5.1.2 152 5/21/2025
0.5.1.1 145 5/20/2025
0.5.1 337 5/13/2025
0.5.0 228 5/12/2025
0.4.8 212 2/26/2025
0.4.7 117 2/26/2025
0.4.6 124 2/11/2025
0.4.5 108 2/11/2025
0.4.4 113 2/11/2025
0.4.3 110 2/11/2025
0.4.2 181 2/3/2025
0.4.1 115 2/3/2025
0.4.0 140 2/2/2025
0.3.0 108 1/25/2025
0.2.0 97 1/24/2025
0.1.0 95 1/24/2025

- fix reference package versions