[Feature Request] Use Task-based Asynchronous Pattern (TAP) consistently across SDK especially PromptDialog
PromptDialog use asynchronous callback pattern instead of TAP (async and await). This complicates logic implementation and is inconsistent with the rest of the SDK.
@DL-LiveTiles are you referring to the C# or Node SDK?
@nwhitmont C#
Hi @DL-LiveTiles,
The async methods in PromptDialog.cs seem to me to be consistent with the rest of the SDK. Would please elaborate on what you mean exactly?
Thanks.
Hi @EricDahlvang,
Implementing a separate call-back method for each prompt makes the bot code messy and long.
Currently the code would be:
...
PromptDialog.Text(context, TextProvided, "Enter text value.");
}
async Task TextProvided(IDialogContext context, IAwaitable<string> argument)
{
string value = await argument;
}
I would simply like to do:
string value = await PromptDialog.Text(context, "Enter text value.");
This would make the bot code much easier to follow and simpler to implement. I believe with some changes to the SDK this would be possible using TaskCompletionSource
Similar to what is suggested here: Best way to convert callback-based async method to awaitable task.
Regards,
Douw
Thanks for your suggestion. I agree, it would be more simple for devs to be able use async/await. I'm not knowledgeable regarding what the implications would be if we made this change. I've discussed it with the engineers and:
async/await is for the “task augmented value”, not the “wait for user input augmented value”.
Nonetheless, if you feel up to it, you can make the change and submit a pull request. Microsoft.BotBuilder is open source.
Again, thanks for your suggestion.
@DL-LiveTiles I tried to do like you say but message doesn't send
Did you have success with it?
var text = await PromptDialogExt.TextAsync(context, "Enter some text");
......
public static class PromptDialogExt
{
public static Task<string> TextAsync(IDialogContext context, string text)
{
var tcs = new TaskCompletionSource<string>();
PromptDialog.Text(context, async (c, r) =>
{
try
{
var result = await r;
tcs.TrySetResult(result);
}
catch (Exception ex)
{
tcs.TrySetException(ex);
}
}
, text);
return tcs.Task;
}
}
@willportnoy Could you please check if async dialog prompts can be achieved with current codebase? Thanks.
@Ma3yTa that code won't work because the closure is capturing the TaskCompletionSource and that TaskCompletionSource won't useful be persisted until the next turn of conversation (e.g. which could run on another machine entirely).
@willportnoy Thanks for your answer. Does it mean that Task-based Asynchronous Pattern can't be implemented for PromptDialog methods?
I think it can be possible if IDialogStack.Call method return IAwaitableIWaitable<R> CallAsync<R>(IDialog<R> child) ?
@Ma3yTa the difficulty is that when the next message in the conversation is sent to the bot, that code that is [efficiently, asynchronously] waiting on the next message may not be the machine that receives the message because it is behind a load balancer.