Class ServiceCollectionExtensions
- Namespace
- Savvyio.Extensions.DependencyInjection.Messaging
- Assembly
- Savvyio.Extensions.DependencyInjection.dll
Extension methods for the IServiceCollection interface.
public static class ServiceCollectionExtensions
- Inheritance
-
ServiceCollectionExtensions
Examples
Registering transport-agnostic messaging in Savvy I/O uses AddMessageQueue for point-to-point command delivery and AddMessageBus for publish-subscribe event delivery. Both methods accept the service interface and the concrete implementation type, enabling seamless transport swapping between in-memory, RabbitMQ, NATS, and cloud brokers. The example registers both channels with concrete implementations and resolves each through its abstract interface.
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Cuemon.Threading;
using Microsoft.Extensions.DependencyInjection;
using Savvyio.Commands;
using Savvyio.EventDriven;
using Savvyio.Extensions.DependencyInjection.Messaging;
using Savvyio.Messaging;
namespace ExampleApp;
public static class MessagingRegistration
{
public static IServiceCollection AddMessagingChannels(this IServiceCollection services)
{
services.AddMessageQueue<OrderCommandQueue, ICommand>();
services.AddMessageBus<OrderEventBus, IIntegrationEvent>();
return services;
}
}
public sealed class OrderCommandQueue : IPointToPointChannel<ICommand>
{
public async IAsyncEnumerable<IMessage<ICommand>> ReceiveAsync(Action<AsyncOptions> setup = null)
{
await Task.CompletedTask;
yield break;
}
public Task SendAsync(IEnumerable<IMessage<ICommand>> messages, Action<AsyncOptions> setup = null)
{
return Task.CompletedTask;
}
}
public sealed class OrderEventBus : IPublishSubscribeChannel<IIntegrationEvent>
{
public Task PublishAsync(IMessage<IIntegrationEvent> message, Action<AsyncOptions> setup = null)
{
return Task.CompletedTask;
}
public Task SubscribeAsync(Func<IMessage<IIntegrationEvent>, CancellationToken, Task> asyncHandler, Action<SubscribeAsyncOptions> setup = null)
{
return Task.CompletedTask;
}
}
Methods
AddMessageBus<TService, TRequest>(IServiceCollection, Action<ServiceOptions>)
Adds an implementation of IPublishSubscribeChannel<TRequest> to the specified IServiceCollection.
public static IServiceCollection AddMessageBus<TService, TRequest>(this IServiceCollection services, Action<ServiceOptions> setup = null) where TService : class, IPublishSubscribeChannel<TRequest> where TRequest : IRequest
Parameters
servicesIServiceCollectionThe IServiceCollection to add the service to.
setupAction<ServiceOptions>The ServiceOptions which may be configured.
Returns
- IServiceCollection
A reference to
servicesso that additional configuration calls can be chained.
Type Parameters
TServiceThe type of the IPublishSubscribeChannel<TRequest> to add.
TRequestThe type of the model to invoke on a handler.
Remarks
If the underlying type of TService implements IDependencyInjectionMarker<TMarker> interface then this is automatically handled. Also, the implementation will be type forwarded accordingly.
- See Also
-
IPublishSubscribeChannel<TRequest>IPublishSubscribeChannel<TRequest, TMarker>
AddMessageQueue<TService, TRequest>(IServiceCollection, Action<ServiceOptions>)
Adds an implementation of IPointToPointChannel<TRequest> to the specified IServiceCollection.
public static IServiceCollection AddMessageQueue<TService, TRequest>(this IServiceCollection services, Action<ServiceOptions> setup = null) where TService : class, IPointToPointChannel<TRequest> where TRequest : IRequest
Parameters
servicesIServiceCollectionThe IServiceCollection to add the service to.
setupAction<ServiceOptions>The ServiceOptions which may be configured.
Returns
- IServiceCollection
A reference to
servicesso that additional configuration calls can be chained.
Type Parameters
TServiceThe type of the IPointToPointChannel<TRequest> to add.
TRequestThe type of the model to invoke on a handler.
Remarks
If the underlying type of TService implements IDependencyInjectionMarker<TMarker> interface then this is automatically handled. Also, the implementation will be type forwarded accordingly.
- See Also
-
IPointToPointChannel<TRequest>IPointToPointChannel<TRequest, TMarker>