简体   繁体   中英

I couldn't write context.Forward within my async method in my bot application

I create a bot application using C# .

I want to design my bot using multiple dialogs. In RootDialog.cs , I have written code to navigate different dialogs according to the user's input.

I am using context.Forward for navigating to next dialog. But it is showing error when using it.Here I have attached my RootDialog.cs .

Can anyone help me in solving the problem?

using System;
using System.Threading.Tasks;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Connector;
using System.Threading;

namespace WebService.Dialogs
{
    [Serializable]
    public class RootDialog : IDialog<object>
    {
        public Task StartAsync(IDialogContext context)
        {
            context.Wait(MessageReceivedAsync);

            return Task.CompletedTask;
        }

       public async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> result)
        {
            var message = await result;
            if (message.Text.ToLower().Contains("name")) {
            await context.Forward(new NameDialog(), this.DetailDialog);
                context.Wait(this.MessageReceivedAsync);
            }

            // calculate something for us to return


            // return our reply to the user

            context.Wait(MessageReceivedAsync);
        }
        private async Task DetailDialog(IDialogContext context, IAwaitable<IMessageActivity> result)
        {
            var message = await result;
            if (message.Text.ToLower().Contains("price"))
            {

             context.Forward(new PriceDialog(), this.QuantityDialog, message, CancellationToken.None);
                context.Wait(this.MessageReceivedAsync);

            }
        }
            private async Task QuantityDialog(IDialogContext context, IAwaitable<IMessageActivity> result) {
            var mess = await result;
            if (mess.Text.ToLower().Contains("Discount"))
            {
                await context.PostAsync("50% discount per each");

            }
        }
        }
    }

The error is to call a child dialog and add it to the top of the stack showed in "context.Forward(new PriceDialog(), this.QuantityDialog, message, CancellationToken.None)" line.

The context.Forward is a task like this:

Task Forward<R, T>(IDialog<R> child, ResumeAfter<R> resume, T item, CancellationToken token);

The first parameter child means the child dialog you want to call, not a task. The second one indicates the method to resume when the child dialog has completed, this method can be implemented inside of the parent dialog. The item parameter should be the message you want to send to the child dialog. And the last one is the cancellation token. And type parameter R represents the type of result expected from the child dialog and in the meanwhile T represents the type of the item sent to child dialog.

So basically, you should be able to call this Forward task exactly according to its constructor. Your code await context.Forward(new NameDialog(), this.DetailDialog); is not right. You may try to modify it like this:

context.Forward(new NameDialog(), this.QuantityDialog, message, CancellationToken.None);

By the way, if you want to call your task DetailDialog inside of MessageReceivedAsync , you can directly call it like this:

await DetailDialog(context, result);  

For more information, you can refer to Invoke the 'New Order' dialog .

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