Search code examples
c#botframework

Are those errors related with bad syntax or updated package issues?


Please, I am trying to figure out why still receiving these errors on my code below. I updated some packages before do some changes. I would like to know if new packages can cause errors or if it is just bad syntax. It is a simple bot with dialogs, using Bot Framework V4 and Visual Studio 15.8.8. The list of errors are:

  • The modifiers private not valid for this item
  • The modifier private not valid for this item
  • Names NameStepAsync and NameConfirmStepAsync doesn't exist for the current context.
  • A local or parameter name cancellationToken can not be declared.
  • A package restore failed.
  • A detected package downgrade.

All errors codes links to different examples. Other errors I could solve by myself. I will appreciate all feedback and can provide more info, but please help me.

using System.Threading;
using System.Threading.Tasks;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Schema;
using Microsoft.Extensions.Logging;
using Microsoft.Bot.Builder.Dialogs;

namespace IcidBotOne
{

    public class EchoWithCounterBot : IBot
    {

        private DialogSet _dialogs;
        private readonly EchoBotAccessors _accessors;
        private readonly ILogger _logger;


        public EchoWithCounterBot(EchoBotAccessors accessors)


        {
            // Set the _accessors
            _accessors = accessors ?? throw new System.ArgumentNullException(nameof(accessors));
            // The DialogSet needs a DialogState accessor, it will call it when it has a turn context.
            _dialogs = new DialogSet(accessors.ConversationDialogState);
            // This array defines how the Waterfall will execute.
            var waterfallSteps = new WaterfallStep[]
            {
                NameStepAsync,
                NameConfirmStepAsync,
            };
            // Add named dialogs to the DialogSet. These names are saved in the dialog state.
            _dialogs.Add(new WaterfallDialog("details", waterfallSteps));
            _dialogs.Add(new TextPrompt("name"));
        }

        /// <summary>
        /// Every conversation turn for our Echo Bot will call this method.
        /// There are no dialogs used, since it's "single turn" processing, meaning a single
        /// request and response.
        /// </summary>
        /// <param name="turnContext">A <see cref="ITurnContext"/> containing all the data needed
        /// for processing this conversation turn. </param>
        /// <param name="cancellationToken">(Optional) A <see cref="CancellationToken"/> that can be used by other objects
        /// or threads to receive notice of cancellation.</param>
        /// <returns>A <see cref="Task"/> that represents the work queued to execute.</returns>
        /// <seealso cref="BotStateSet"/>
        /// <seealso cref="ConversationState"/>
        /// <seealso cref="IMiddleware"/>
        public async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default(CancellationToken))
        {
            // Handle Message activity type, which is the main activity type for shown within a conversational interface
            // Message activities may contain text, speech, interactive cards, and binary or unknown attachments.
            // see https://aka.ms/about-bot-activity-message to learn more about the message and other activity types
            if (turnContext.Activity.Type == ActivityTypes.Message)
            {
                // Get the conversation state from the turn context.
                var state = await _accessors.CounterState.GetAsync(turnContext, () => new CounterState());

                // Bump the turn count for this conversation.
                state.TurnCount++;
                if (!state.SaidHello)
                {
                    // MARCUS: Handlle the Greeting
                    string strMessage = $"Hello World! {System.Environment.NewLine}";
                    strMessage += "Talk to me and I will repeat it back!";
                    await turnContext.SendActivityAsync(strMessage);

                    // MARCUS: Set SaidHello
                    state.SaidHello = true;
                }

                // Run the DialogSet - let the framework identify the current state of the dialog from
                // the dialog stack and figure out what (if any) is the active dialog.
                var dialogContext = await _dialogs.CreateContextAsync(turnContext, cancellationToken);
                var results = await dialogContext.ContinueDialogAsync(cancellationToken);
                // If the DialogTurnStatus is Empty we should start a new dialog.
                if (results.Status == DialogTurnStatus.Empty)
                {
                    await dialogContext.BeginDialogAsync("details", null, cancellationToken);
                }

                // Set the property using the accessor. OK
                await _accessors.CounterState.SetAsync(turnContext, state);

                // Save the new turn count into the conversation state. OK
                await _accessors.ConversationState.SaveChangesAsync(turnContext);
            }

            private static async Task<DialogTurnResult> NameStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
            {
            // Running a prompt here means the next Waterfall
            // will be run when the user response is received.
                    return await stepContext.PromptAsync("name", new PromptOptions { Prompt = MessageFactory.Text("What is your name?") }, cancellationToken);
            }

            private async Task<DialogTurnResult> NameConfirmStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
            {
                    // We can send messages to the user at any point in the WaterfallStep.
                    await stepContext.Context.SendActivityAsync(MessageFactory.Text($"Hello {stepContext.Result}!"), cancellationToken);
                    // WaterfallStep always finishes with the end of the Waterfall or with another dialog,
                    // here it is the end.
                    return await stepContext.EndDialogAsync(cancellationToken: cancellationToken);
            }
        }
    }
}

Solution

  • You're defining NameStepAsync and NameConfirmStepAsync inside of OnTurnAsync. Go ahead and move their definitions outside of OnTurnAsync onto the class level.