快速连续发送到机器人的多条消息使其崩溃

时间:2017-09-05 07:52:43

标签: c# bots botframework facebook-messenger-bot

设置

我有一个运行在.NET + Bot Framework + Azure + Facebook Messenger上的机器人。

初始问题

我在向bot发送多条消息时尝试解决问题会触发异常和HTTP错误412. Microsoft在此处描述了此问题:https://docs.microsoft.com/en-us/bot-framework/troubleshoot-general-problems#what-causes-an-error-with-http-status-code-412-precondition-failed-or-http-status-code-409-conflict

第一个解决方案

在上面的页面中,Microsoft提供了过时的示例代码来解决此问题。在this github issue中,有一个应该有效的代码的修订版本。我把它放在MessageController的构造函数中:

    static MessagesController()
    {
        // Prevent exception in the bot and HTTP error 412 when the user
        // sends multiple messages in quick succession. This may cause 
        // potential problems with consistency of getting/setting user
        // properties. 
        // See https://docs.microsoft.com/en-us/bot-framework/troubleshoot-general-problems#what-causes-an-error-with-http-status-code-412-precondition-failed-or-http-status-code-409-conflict
        // for details. The above link contains wrong code sample, revised
        // code is from here: https://github.com/Microsoft/BotBuilder/issues/2345
        var builder = new ContainerBuilder();
        builder
            .Register(c => new CachingBotDataStore(c.ResolveKeyed<IBotDataStore<BotData>>(typeof(ConnectorStore)), CachingBotDataStoreConsistencyPolicy.LastWriteWins))
            .As<IBotDataStore<BotData>>()
            .AsSelf()
            .InstancePerLifetimeScope();
        builder.Update(Conversation.Container);
    } 

第二个问题

现在,当我快速连续向机器人发送几条消息时,异常仍然存在。但是,它从HTTP错误412更改为其他内容:

  

发生了一个或多个错误。在System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)处于System.Threading.Tasks.Task1.GetResultCore(Boolean waitCompletionNotification)的System.Threading.Tasks.Task1.get_Result()at MyBot.SetUserDataProperty(Activity activity,String PropertyName) ,String ValueToSet)在C:\ Users \ xxx.cs:第230行

更新:我检查了上面的InnerException,结果是旧的HTTP错误412:

  

远程服务器返回错误:(412)Precondition Failed。

违规代码是写入机器人存储的功能。上面引用的第230行是该函数的最后一行:

    public static void SetUserDataProperty(Activity activity, string PropertyName, string ValueToSet)
    {
        StateClient client = activity.GetStateClient();
        BotData userData = client.BotState.GetUserData(activity.ChannelId, activity.From.Id);
        userData.SetProperty<string>(PropertyName, ValueToSet);

        //client.BotState.SetUserDataAsync(activity.ChannelId, activity.From.Id, userData);
        // Await async call without making the function asynchronous:
        var temp = Task.Run(() => client.BotState.SetUserDataAsync(activity.ChannelId, activity.From.Id, userData)).Result;
    }

问题

我还能做些什么来确保用户能够快速连续发送多条消息,而不会在写入BotState存储时触发异常?

1 个答案:

答案 0 :(得分:3)

我认为这里有一些问题

您尝试执行此操作的方式activity.GetStateClient();仅用于原型设计。我们不会为生产级代码推荐这种方法。您可以在对话框中设置context.UserData.SetValue("food", "Nachos" );之类的用户数据,并在序列化对话框时自动保存值。

您很可能从对话框中调用此方法SetUserDataProperty,因此当您执行此操作var temp = Task.Run(() => client.BotState.SetUserDataAsync(activity.ChannelId, activity.From.Id, userData)).Result;时,它会发生冲突并导致错误。

请查看此blog post了解详情

以下是如何实施您的跟进问题:

        if (activity.Type == ActivityTypes.Message)
        {

            var message = activity as IMessageActivity;
            using (var scope = DialogModule.BeginLifetimeScope(Conversation.Container, message))
            {
                var botDataStore = scope.Resolve<IBotDataStore<BotData>>();
                var key = new AddressKey()
                {
                    BotId = message.Recipient.Id,
                    ChannelId = message.ChannelId,
                    UserId = message.From.Id,
                    ConversationId = message.Conversation.Id,
                    ServiceUrl = message.ServiceUrl
                };
                ConversationReference r = new ConversationReference();
                var userData = await botDataStore.LoadAsync(key, BotStoreType.BotUserData, CancellationToken.None);

                userData.SetProperty("key 1", "value1");
                userData.SetProperty("key 2", "value2");

                await botDataStore.SaveAsync(key, BotStoreType.BotUserData, userData, CancellationToken.None);
                await botDataStore.FlushAsync(key, CancellationToken.None);
            }
            await Conversation.SendAsync(activity, () => new Dialogs.RootDialog());
        }

你需要实现这个类或类似的东西:

public class AddressKey : IAddress
{
    public string BotId { get; set; }
    public string ChannelId { get; set; }
    public string ConversationId { get; set; }
    public string ServiceUrl { get; set; }
    public string UserId { get; set; }
}