mark
mark

Reputation: 619

microsoft bot Framework giving error-'await' cannot be used as an identifier within an async method or lambda expression

I am trying to create a chatbot and tumbled upon this tutorial. Followed the instruction and it gave an error

'await' cannot be used as an identifier within an async method or lambda expression

My complete .cs code :

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

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

        return Task.CompletedTask;
    }

    private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)
    {
        var activity = await result as Activity;

        // calculate something for us to return
        int length = (activity.Text ?? string.Empty).Length;

        // return our reply to the user
        await context.PostAsync($"You sent {activity.Text} which was {length} characters");

        context.Wait(MessageReceivedAsync);
    }
}
}

I am new to this Bot application , so if I miss anything do let me know.

Thanks

Upvotes: 1

Views: 627

Answers (2)

Gary Liu
Gary Liu

Reputation: 13918

It looks prefect on your code, I don't think the issue was raised by your code. And I found that the tutorial you referred was post 2016 and which may be out of date. Maybe the structure/ code of the template has changed which raised your issue.

Please refer to https://learn.microsoft.com/en-us/bot-framework/dotnet/bot-builder-dotnet-quickstart for the official quick started for .Net, and which will be updated in time.

And currently, all the referencs in the sample:

  <package id="Autofac" version="3.5.2" targetFramework="net46" />
  <package id="Chronic.Signed" version="0.3.2" targetFramework="net46" />
  <package id="Microsoft.AspNet.WebApi" version="5.2.3" targetFramework="net46" />
  <package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net46" />
  <package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net46" />
  <package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.3" targetFramework="net46" />
  <package id="Microsoft.Bot.Builder" version="3.11.0" targetFramework="net46" />
  <package id="Microsoft.Bot.Builder.CognitiveServices" version="1.1.1" targetFramework="net46" />
  <package id="Microsoft.Bot.Connector" version="3.11.1" targetFramework="net46" />
  <package id="Microsoft.IdentityModel.Protocol.Extensions" version="1.0.4.403061554" targetFramework="net46" />
  <package id="Microsoft.Rest.ClientRuntime" version="2.3.2" targetFramework="net46" />
  <package id="Microsoft.WindowsAzure.ConfigurationManager" version="3.1.0" targetFramework="net46" />
  <package id="Newtonsoft.Json" version="8.0.3" targetFramework="net46" />
  <package id="System.IdentityModel.Tokens.Jwt" version="4.0.4.403061554" targetFramework="net46" />

Upvotes: 1

Manoj Bhardwaj
Manoj Bhardwaj

Reputation: 260

you need to put:

public async Task StartAsync(IDialogContext context)

in place of

 public Task StartAsync(IDialogContext context)

and replace

return Task.CompletedTask;

it will look like:

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

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


        }

        private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)
        {
            var activity = await result as Activity;

            // calculate something for us to return
            int length = (activity.Text ?? string.Empty).Length;

            // return our reply to the user
            await context.PostAsync($"You sent {activity.Text} which was {length} characters");

            context.Wait(MessageReceivedAsync);
        }
    }
}

feel free to revert in case of more help required

Upvotes: 1

Related Questions