我有一个根对话框,可以像这样创建子对话框...
private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> result)
{
var message = await result;
string userName = context.Activity?.From.Name;
var customerForm = new FormDialog<CarValuationDialog>(
new CarValuationDialog(userName),
() => CarValuationDialog.BuildForm(),
FormOptions.PromptInStart);
context.Call(customerForm, FormSubmitted);
}
FormSubmitted
方法看起来像......
public async Task FormSubmitted(IDialogContext context, IAwaitable<CarValuationDialog> result)
{
try
{
var form = await result;
}
catch (FormCanceledException<CarValuationDialog> e)
{
string reply;
if (e.InnerException == null)
{
reply = e.Message;
}
else
{
reply = e.InnerException.Message;
}
context.Reset();
await context.PostAsync(reply);
}
}
当子对话框中发生异常时,方法FormSubmitted
将被执行并进入catch块。但是,当该方法完成后,用户仍然会看到“抱歉我的机器人有问题”类型的消息。
我如何告诉机器人代码不要触发未经处理的异常代码(我相信是在PostUnhandledExceptionToUser
中)?是否需要将标志类型属性设置为true或其他?
答案 0 :(得分:2)
在POST方法的消息控制器中,使用defaultifexception
await Conversation.SendAsync(activity, () => new Dialogs.DialogLUIS().DefaultIfException());
答案 1 :(得分:2)
出现异常时,您似乎使对话框堆栈为空:您不应该在下面使用此context.Reset()
:
public async Task FormSubmitted(IDialogContext context, IAwaitable<CarValuationDialog> result)
{
try
{
var form = await result;
}
catch (FormCanceledException<CarValuationDialog> e)
{
string reply;
if (e.InnerException == null)
{
reply = e.Message;
}
else
{
reply = e.InnerException.Message;
}
context.Reset();
await context.PostAsync(reply);
}
}
删除此行,下一条消息将由您的根对话框处理