antonyvorontsov / RabbitMQ.Client.Core.DependencyInjection

.Net Core library-wrapper of RabbitMQ.Client for Dependency Injection.
MIT License
111 stars 36 forks source link

IConsumingService lifetime / why not IHostedService #63

Closed JanEggers closed 3 years ago

JanEggers commented 3 years ago
    /// <summary>
    /// Start consuming (getting messages).
    /// </summary>
    void StartConsuming();

    /// <summary>
    /// Stop consuming (getting messages).
    /// </summary>
    void StopConsuming();

in our current project we wrap the IConsumingService because it is no IHostedService. so it does not start automaticly.

why has the api been designed that way?

why no cancellation token support?

another issue I have is that the connections are established by the ctor. I would really prefere that this would be done on first usage.

antonyvorontsov commented 3 years ago

I have checked rabbitmq-dotnet-client project and It seems like there is no cancellation token support at all 😞

As for other suggestions, in that pull request I add hosted service

public class ConsumingHostedService : IHostedService
{
    private readonly IConsumingService _consumingService;

    public ConsumingHostedService(IConsumingService consumingService)
    {
        _consumingService = consumingService;
    }

    public Task StartAsync(CancellationToken cancellationToken)
    {
        _consumingService.StartConsuming();
        return Task.CompletedTask;
    }

    public Task StopAsync(CancellationToken cancellationToken)
    {
        return Task.CompletedTask;
    }
}

and it don't have to explain the code.