C#C
C#4y ago
Arkobat

❔ Callback consumer to async Task

Hello

I'm trying to convert a callback based method to an async task.
The basic idea is I have a message bus where I can send messages.
Some of these messages require a response from the other end.
I can easily do this with a callback based system, but I would like to convert it to a async/await.

I have created this example (I know the code is invalid, it just serves as a sketch).
Is there a standardised way to do this? I have not been able to find it on stackoverflow.
public class MessageService
{

    private readonly IMessageProvider_messageProvider;
    private readonly Dictionary<Guid, Action<TResponse>> _dictionary = new();

    public TResponse Send<TMessage, TResponse>(TMessage payload, Action<TResponse> func) where TMessage : class
    {
        var message = new Message<TMessage>
        {
            ResponseChannel = "my-channel",
            Payload = payload
        };
        
        _dictionary[message.MessageId] = func;
        _messageProvider.Publish(message);
    }
    
    public void OnMessage<T>(Response<T> response) where T : class
    {
        if (!_dictionary.TryGetValue(response.MessageId, out var action)) return;

        action!.Invoke(response.Payload);
    }

    public async Task<TResponse> SendAsync<TMessage, TResponse>(TMessage payload, CancellationToken cancellationToken)
    {
        // TODO
        throw new NotImplementedException();
    }
    
}
Was this page helpful?