1 year ago

#134683

test-img

Masteroxify

ASP.NET : how to make reliable long running event consumer

I am considering on the way to implement event consumer as long running task in ASP.NET. The common way to do this it use IHostedService and it implementation BackgroundService, but what exactly is the difference between class I show below when I set it as singleton?

One of advantage of this class is possibility to implement interface we want to use.

public class LongRunningTask
{
    private readonly Task _mainLopp;

    public LongRunningTask()
    {
        _mainLopp = Task.Factory.StartNew(MainLoopAsync);
    }

    public async Task MainLoopAsync()
    {
        while(true) //here we can use cancellation token or something else
        {
            await SomeAsyncMethod() //like client.ReceiveEventAsync()
        }
    }
}

I see this solution in RabbitMq library.

asp.net

task

event-bus

0 Answers

Your Answer

Accepted video resources