C
C#3y 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();
}

}
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();
}

}
4 Replies
mtreit
mtreit3y ago
When you ask: "is there a standardized way to do this?" I'm not really sure what "this" refers to. Looking at your code my first reaction is that it's not thread safe.
Anton
Anton3y ago
you're looking for TaskCompletionSource @Arkobat
Arkobat
ArkobatOP3y ago
Yeah, this seems to be exactly what I’m looking for. Thanks🙏🏼
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server