Microsoft Bot Framework: Exception: The data is changed

后端 未结 2 1109
有刺的猬
有刺的猬 2020-12-21 15:56

I have a bot with the following conversation scenario:

  1. Send text to LUIS
  2. LUIS intent calls context.Call(...) to launch a Dialog
相关标签:
2条回答
  • 2020-12-21 16:41

    I agree with @Artem (this solved my issue too, thanks!). I would just add the following guideline.

    Use

    var data = context.UserData;
    data.SetValue("field_name", false);
    

    whenever you have a IDialogContext object available, so you let the Bot Framework commit changes.

    Use instead

    StateClient sc = activity.GetStateClient();
    await sc.BotState.SetUserDataAsync(activity.ChannelId, activity.From.Id, userData);
    

    when you don't have an IDialogContext object, e.g. in the MessageController class.

    0 讨论(0)
  • 2020-12-21 16:48

    Botframework restores/saves state of conversation after each act of activity, so under the covers typical flow looks like the following:

    [23:15:40] <- GET 200 getUserData 
    [23:15:47] <- GET 200 getConversationData 
    [23:15:47] <- GET 200 getPrivateConversationData 
    ...
    [23:16:42] <- POST 200 setConversationData 
    [23:16:42] <- POST 200 setUserData 
    [23:16:42] <- POST 200 setPrivateConversationData 
    

    As it is mentioned here : These botData objects will fail to be stored if another instance of your bot has changed the object already. So in your case the exception occurs at the termination of dialog, when framework calls setUserData by himself and figures out that the BotData has been changed already (by your explicit call of BotState.SetUserDataAsync). I suppose that's why you were not able to catch the exception.

    Solution: I used the following code and it fixed the issue:

    private static void storeBotData(IDialogContext context, BotData userData)
    {
            var data = context.UserData;
            data.SetValue("field_name", false);            
    }
    

    The reason it works is that we modify the object of UserData but allow botFramework to "commit" it himself, so there is no conflict

    0 讨论(0)
提交回复
热议问题