Gabriel Piffaretti
Gabriel Piffaretti

Reputation: 832

Inconsistent state after trying to save object in Bot Framework State Service

I was building a bot where I wanted to save and retrieve some state in the Bot State Service (using .NET Bot Builder SDK). As saving in PrivateConversationData or UserData properties of IDialogContext wasn't working (it wasn't saving anything), what I did was to write a piece of code similar to the following inside a dialog:

List<Value> valuesToStore = GetSomeValuesToStore();
StateClient stateClient = ((Activity)context.Activity).GetStateClient();
BotData userData = await stateClient.BotState.GetUserDataAsync(context.Activity.ChannelId, context.Activity.Conversation.Id);

if (userData != null && userData.GetProperty<List<Value>>(VALUES_NAME) != null)
{
    DoSomethingWithUserData(userData);
}
else
{
    //save values in state
    userData.SetProperty(VALUES_NAME, valuesToStore);
    await stateClient.BotState.SetUserDataAsync(context.Activity.ChannelId, context.Activity.Conversation.Id, userData);
}

And after that code being executed only once, I started getting the following Exception whenever a new message arrived to my bot:

"Settings must be of the form \"name=value"\."

Which it seemed to be something related to Azure Storage connection (what Bot Framework uses to store state I guess). Somehow I got the bot in an inconsistent state, the only solution I found for this was creating a new bot in Bot Framework Developer Portal, and saving state on my own.

And some time ago, a similar I got a similar error:

"Null properties cannot be encrypted. Please assign a default value to the property if you wish to encrypt it.";

Which obviously was because the objects I was trying to store contained properties with null values. But same thing, I couldn't make the bot return to the normal state; I had to create a new one because every new message resulted into an exception whenever the bot tried to retrieve the state.

Any thoughts on this?

Thanks!

Upvotes: 1

Views: 415

Answers (1)

Eric Dahlvang
Eric Dahlvang

Reputation: 8292

When the state is broken, you can send the bot a " /deleteProfile" message and the bot will clear out state for that user.

It is not recommended to use the default state client for production bots. Please review this blog post https://blog.botframework.com/2017/07/18/saving-state-azure-extensions/

activity.GetStateClient() is the default state client. You should use the IDialogContext methods to interact with state. You mention that "it wasn't saving anything". Why not?

Null properties can be saved into state. That should not be an issue. The error you saw regarding null properties in state was a bug that has been fixed.

Upvotes: 1

Related Questions