Microsoft Bot Framework: Exception: The data is changed

淺唱寂寞╮ 提交于 2019-11-29 16:14:19

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!