我正在尝试对用户进行身份验证,然后再继续执行其余的漫游器功能,并使用此代码确保用户存在
[Serializable]
public class ModifyLicenceDialog: IDialog<object>
{
private const string CreateLicence = "Create a new licence";
private const string ModifyEndDate = "Modify end date";
private const string CancelLicence = "Cancel a licence";
public async Task StartAsync(IDialogContext context)
{
if (!CommonConversation.User.IsAuthorized)
{
context.Call(new AuthDialog(), ResumeAfterAuth);
}
}
private async Task ResumeAfterAuth(IDialogContext context, IAwaitable<object> result)
{
await result;
PromptDialog.Choice(context, ResumeAfterChoice, new[] { CreateLicence, ModifyEndDate, CancelLicence },
"What licence function would you like?",
"I am sorry i did not understand that, please select one of the below options");
}
private async Task ResumeAfterChoice(IDialogContext context, IAwaitable<string> result)
{
string selected = await result;
switch (selected)
{
case CreateLicence:
context.Call(new CreateLicenseDialog(), AfterLicenseDialog(CreateLicence));
break;
case ModifyEndDate:
break;
case CancelLicence:
break;
}
}
private ResumeAfter<object> AfterLicenseDialog(IDialogContext context, string licenseEvent)
{
switch (licenseEvent)
{
case CancelLicence:
await context.PostAsync("Auth done");
context.Done(licenseEvent);
break;
}
}
然而,当我的代码进入AuthDialog时,当它尝试调用api时,如果带有提供的电子邮件的用户存在(specifically at res = await client.GetAsync(uri)
),一旦api返回结果,bot会返回到父对话框(ModifyLicenceDialog) )并执行ResumeAfterAuth
[Serializable]
public class AuthDialog: IDialog<object>
{
private const string InputEmail = "Please input your email address to continue.";
private int _attempts = 3;
public async Task StartAsync(IDialogContext context)
{
await context.PostAsync(InputEmail);
context.Wait(MessageReceivedAsync);
}
private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> result)
{
IMessageActivity activity = await result;
string email = activity.Text;
await context.PostAsync("Please wait while we verify this...");
HttpResponseMessage res = new HttpResponseMessage();
HttpClient client = AuthFunctions.Client;
string uri = $"api/v1/auth/licenses/{email}";
res = await client.GetAsync(uri);
if (res.IsSuccessStatusCode)
{
CommonConversation.User = JsonConvert.DeserializeObject<CustomerUser>(await res.Content.ReadAsStringAsync());
await context.PostAsync($"Welcome {CommonConversation.User.FirstName}!");
context.Done(email);
}
else if (_attempts > 0)
{
_attempts--;
await context.PostAsync("I am sorry we do not recognize that email address, lets try again. Please input your email address");
context.Wait(MessageReceivedAsync);
}
else
{
context.Fail(new Exception("I am afraid you are not an authorized user"));
}
}
}
}
经过Nicolas R的一些指导后,更新结果证明此时会抛出异常 例外情况如下
{"Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host."}
" at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\r\n at SupportHelpdeskBot.Dialogs.AuthDialog.<MessageReceivedAsync>d__3.MoveNext() in C:\\Workspace\\SupportBot\\SupportHelpdeskBot\\SupportHelpdeskBot\\Dialogs\\AuthDialog.cs:line 41"
我已经尝试过System.Net.ServicePointManager.Expect100Continue = false;
来阻止这种情况发生,但是这没有任何建议吗?
答案 0 :(得分:1)
您的实施中没有错误:您正在使用AuthDialog
完成context.Done(email)
:
if (res.IsSuccessStatusCode)
{
CommonConversation.User = JsonConvert.DeserializeObject<CustomerUser>(await res.Content.ReadAsStringAsync());
await context.PostAsync($"Welcome {CommonConversation.User.FirstName}!");
context.Done(email);
}
这是正常行为。你想要实现什么?我认为你的问题不完整