2
votes

I'm trying to pass state between different dialogs, and seem to be either a) not calling dialogs correctly or b) not using botstate correctly (or both).

Can anyone tell me what I am losing when I open the second dialog? Its opened using context.forward();

In messagescontroller I am easily able to set state;

StateClient stateClient = activity.GetStateClient();
BotData userData = await stateClient.BotState.GetUserDataAsync(activity.ChannelId, activity.From.Id);
userData.SetProperty<bool>("SessionActive", true);
await stateClient.BotState.SetUserDataAsync(activity.ChannelId, activity.From.Id, userData);

I then open a dialog which controls access to other dialogs;

await Conversation.SendAsync(activity, () => new DialogController());

This is a separate class;

public class DialogController : IDialog<object>

Within this dialog I can access the value I set as 'true' - this works;

StateClient stateClient = new StateClient(new Uri(message.ServiceUrl));
BotData userData = await stateClient.BotState.GetUserDataAsync(message.ChannelId, message.From.Id);
userData.GetProperty<bool>("SessionActive")

However, within this dialog i then go on to open a second dialog dependant on state;

await context.Forward(new SubDialog(), ThrowOutTask, message, cts.Token);

This is also a separate class;

public class SubDialog: IDialog<object>

However, when I try to retrieve the 'SessionActive' state, in exactly the same way as before, the value is false (i.e. its instantiated for the first time)..?

1

1 Answers

0
votes

When you are requesting the state from an Dialog class, you should do it using the context.

bool sessionActive = context.UserData.Get<bool>("SessionActive");