C#-Not able to get/set bot state value from MVC Application(Outside of Bot)
Bot Info
- SDK Platform: .NET
- SDK Version: Version=3.15.3.0
- Active Channels: WebChat
- Deployment Environment: Azure App Service
Issue Description
Our UI is hosted as a separate MVC application and passing userdata through BotState. in latest version of bot builder stateclient is not supported for getting/setting userdata. So we tried with this approach. But it failed during get/set.
Code Example
` startup.cs
Conversation.UpdateContainer(
builder =>
{
builder.RegisterModule(new AzureModule(Assembly.GetExecutingAssembly()));
var store = new TableBotDataStore(ConfigurationManager.ConnectionStrings["StorageConnectionString"].ConnectionString);
builder.Register(c => store)
.Keyed<IBotDataStore<BotData>>(AzureModule.Key_DataStore)
.AsSelf()
.SingleInstance();
builder.RegisterControllers(Assembly.GetExecutingAssembly());
builder.RegisterFilterProvider();
});
helper class
using (var scope = Conversation.Container.BeginLifetimeScope())
{
IBotDataStore<BotData> botDataStore = scope.Resolve<IBotDataStore<BotData>>();
var key = new AddressKey()
{
BotId = "",
ChannelId = "webchat",
UserId = "XXXX",
ConversationId = "",
ServiceUrl = ""
};
var userData = await botDataStore.LoadAsync(key, BotStoreType.BotUserData, CancellationToken.None);
BotData botData = new BotData(eTag:"*");
botData.SetProperty("user", "XXX");
await botDataStore.SaveAsync(key, BotStoreType.BotUserData, botData, CancellationToken.None);
await botDataStore.FlushAsync(key, CancellationToken.None);
}
`
Reproduction Steps
- add botstate registration code in startup.cs
- resolve the container in class
- it saves the data once and if i refresh the application it get fail to retrieve the data.
Expected Behavior
it should save and retrieve userdata.
Actual Results
it is not setting/getting userdata and no exception also.
You usually want to create Address Key from activity using Address.FromActivity(message);
You do not need to use BotData.
Instead try this.
var userData = await botDataStore.LoadAsync(key, BotStoreType.BotUserData, CancellationToken.None);
userData.SetProperty("user", "XXX");
await botDataStore.SaveAsync(key, BotStoreType.BotUserData, userData, CancellationToken.None);
Also, when you FlushAsync will delete up that object, so remove that.
@Kartik1607 Thanks for your prompt response. I have been using your suggestion in bot application. But this issue happening from MVC web application(here no activity) which i used for UI. Need help for botstate implementation in MVC application.
Hi @SathishNeel
What are you trying to accomplish exactly?
It is entirely possible to load state data outside of a dialog, but you need to provide a valid IMessageActivity to DialogModule.BeginLifetimeScope
One way to do this is to keep a cache of ConversationReference objects. For demonstration purposes, let's say it is just a static ConcurrentDictionary:
public static class ConversationReferenceCache
{
public static ConcurrentDictionary<string, ConversationReference> Cache = new ConcurrentDictionary<string, ConversationReference>();
}
The bot code would add ConversationReference objects to this cache as users communicate with the bot. Then, in your MVC Controller you can access the cache, and convert the conversation reference objects to a message and use that in the call to DialogModule.BeginLifetimeScope:
foreach (var reference in ConversationReferenceCache.Cache.Values)
{
IMessageActivity message = reference.GetPostToBotMessage();
using (var scope = DialogModule.BeginLifetimeScope(Conversation.Container, message))
{
var botDataStore = scope.Resolve<IBotDataStore<BotData>>();
var key = Address.FromActivity(message);
var userData = await botDataStore.LoadAsync(key, BotStoreType.BotUserData, CancellationToken.None);
userData.SetProperty("key 1", "value1");
var propertyValue = userData.GetProperty<string>("key 1");
await botDataStore.SaveAsync(key, BotStoreType.BotUserData, userData, CancellationToken.None);
await botDataStore.FlushAsync(key, CancellationToken.None);
}
}
However, there are other options for accessing the state data outside of a dialog. If you are using the TableBotDataStore, the backend is Azure Table Storage and you can use Microsoft Azure Storage Client Library for .NET and access the data directly. This method is explained here: https://stackoverflow.com/questions/48374471/how-to-retrieve-saved-conversation-data-in-azure-tablelogger
@Kartik1607 I'm not sure what you mean by
FlushAsync will delete up that object
DocumentDbBotDataStore, SqlBotDataStore and TableBotDataStore all do nothing in .FlushAsync: https://github.com/Microsoft/BotBuilder-Azure/blob/master/CSharp/Library/Microsoft.Bot.Builder.Azure/TableBotDataStore.cs#L168 https://github.com/Microsoft/BotBuilder-Azure/blob/master/CSharp/Library/Microsoft.Bot.Builder.Azure/SqlBotDataStore.cs#L156 https://github.com/Microsoft/BotBuilder-Azure/blob/master/CSharp/Library/Microsoft.Bot.Builder.Azure/DocumentDbBotDataStore.cs#L201
@EricDahlvang Hmm.. weird. I had some code which had FlushAsync at the end. The bot state was not getting saved, and user was shown the greeting message again and again.
After removing FlushAsync the bot worked perfectly, and I was also able to store custom user data.
Closing due to age and inactivity. Please open a new issue, or ask a question on Stack Overflow if you require further assistance.