Microsoft bot - 为什么某些 bot 示例会禁用警告 1998

Microsoft bot - Why does some bot example disable warning 1998

我是微软bot开发的新手,所以开始使用公司在Github上发布的例子。 我注意到某些示例在对话框 类:

中禁用了警告
#pragma warning disable 1998

如果我删除该行,我会收到预期的消息 "The async method lacks 'await' operators and will run synchronously..." 这很正常,因为示例通常包含方法调用,例如 context.Wait(this.MessageReceivedAsync); 其中 MessageReceivedAsync() 是一个异步方法。

我了解到抑制警告不是一个好方法,解决警告的原因会更好。我觉得我不知何故错过了这里的要点。我想了解开发人员为什么选择在此处使用禁用警告而不是找到其他实现方式的概念?如果我开发类似的应用程序,我应该做同样的事情吗?

示例:https://github.com/Microsoft/BotBuilder-Samples/blob/master/CSharp/capability-SimpleTaskAutomation/Dialogs/RootDialog.cs

之所以发出此警告,是因为您的示例中此方法会引发警告:

public async Task StartAsync(IDialogContext context)
{
    context.Wait(this.MessageReceivedAsync);
}

如您所见,此方法是 async 但内部没有等待调用,因此出现警告。

当您尝试解决这个问题时,这个 StartAsync 是 Bot Framework 的 IDialog 接口的实现,即 here:

public interface IDialog<out TResult>
{
    /// <summary>
    /// The start of the code that represents the conversational dialog.
    /// </summary>
    /// <param name="context">The dialog context.</param>
    /// <returns>A task that represents the dialog start.</returns>
    Task StartAsync(IDialogContext context);
}

如您所见,StartAsync 方法声明为异步行为。