Microsoft Bot Framework: Exception: The data is changed

随声附和 提交于 2019-12-18 09:21:36

问题


I have a bot with the following conversation scenario:

  1. Send text to LUIS
  2. LUIS intent calls context.Call(...) to launch a Dialog
  3. This dialog terminates, save some info in the userData:

    private static async Task storeBotData(IDialogContext context, BotData userData) { Activity activity = (Activity)context.Activity; StateClient sc = activity.GetStateClient(); await sc.BotState.SetUserDataAsync(activity.ChannelId, activity.From.Id, userData); }

    And after it call another dialog, again with context.Call(...).

  4. Then the last dialog runs and terminates.

My problem is that when updating the user data at the end of the first dialog (step 3), I have the following exception in the Bot Framework Channel Emulator:

         `Exception: The data is changed [File of type 'text/plain']`...

What happens here ? I think that when a dialog terminates, it call setUserData by itself, but I don't understand why I can't update userData anywhere in the code...

I have tried to catch the exception, but nothing is catched.. But I know that the userData is updated, because when I try to retrieve it back, it is updated...

Any help is welcome :)

Thanks


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/42860020/microsoft-bot-framework-exception-the-data-is-changed

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