简体   繁体   中英

Bot Framework: Method exits after attempting to await value

I seem to be having some issues with IAwaitable. I have the following dialog

[Serializable]
public class SearchDialog : IDialog<object>
{
    private SpotifyClient _client;

    public SearchDialog()
    {
        _client = new SpotifyClient();
    }

    public async Task StartAsync(IDialogContext context)
    {
        await context.PostAsync("Please enter the name of the artist");
        context.Wait<string>(MessageReceived);
    }

    public async Task MessageReceived(IDialogContext context, IAwaitable<string> activity)
    {
        var message = await activity;

        var results = await _client.SearchForArtistAsync(message);

        if(results != null && results.Artists.Results.Any())
        {
            var artists = results.Artists.Results.Select(x => x.Name); // List<string>

            PromptDialog.Choice(context,
                ChoiceSelectAsync,
                artists,
                string.Empty,
                "Didn't get that",
                3,
                PromptStyle.PerLine);
        }
    }

    public async Task ChoiceSelectAsync(IDialogContext context, IAwaitable<string> choice)
    {
        var chosenArtist = await choice;

        await context.PostAsync($"You have chosen {chosenArtist}");

        context.Wait<string>(ChoiceSelectAsync);
    }
}

The problem is, the prompt dialog is never displayed. The MessageReceived method seems to tries to await activity, but then exits the method. Why would it not await the result if i'm using the await keyword? Any help would be greatly appreciated.

The signature of the MessageReceived method is wrong. It should be an IAwaitable<MessageActivity> instead of IAwaitable<string>

public async Task MessageReceived(IDialogContext context, IAwaitable<IMessageActivity> activity)

After doing that, you need to also update the context.Wait call to context.Wait(MessageReceived);

Then, to access to the text sent by the user just check the Text property of the awaited message:

  var message = await activity;

  var results = await _client.SearchForArtistAsync(message.Text);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM