从Message Controller c#访问会话数据 [英] Accessing Conversation data from Message Controller c#

查看:107
本文介绍了从Message Controller c#访问会话数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用context.setvalue();和context.TryGetvalue();在Bot Framework中将数据存储和接收到不同的存储中.

I have been using the context.setvalue(); and context.TryGetvalue(); to store and receive data to different storages in Bot Framework.

我想知道我们如何从MessageController.cs访问此值

I want to know how we can access this values from MessageController.cs

已经尝试创建一个新对象,它对我不起作用.

Already tried creating a New object, it don't work for me.

推荐答案

我在寻找我发布的另一个答案时发现了这篇文章.我希望以后再来的人都知道正确的方法.

I found this post when searching for another answer I posted. I wanted anyone that comes here in the future to know the correct way to do this.

StateClient stateClient = activity.GetStateClient();仅获取默认状态客户端,该客户端自2018年3月31日起已弃用.它已由内存状态存储代替.如果您实现了自己的状态客户端(即cosmosDB,Azure表存储,SQL等),则在消息控制器中访问状态的正确方法如下:

StateClient stateClient = activity.GetStateClient(); gets the Default state client only which is deprecated as of March 31st 2018. It has been replaced by an in-memory state store. If you have implemented your own state client (i.e. cosmosDB, Azure table storage, SQL, etc etc) the proper way to access state in the messages controller is something along the lines of this:

if (activity.Type == ActivityTypes.Message)
{

    var message = activity as IMessageActivity;
    using (var scope = DialogModule.BeginLifetimeScope(Conversation.Container, message))
    {
        var botDataStore = scope.Resolve<IBotDataStore<BotData>>();
        var key = Address.FromActivity(message);

        ConversationReference r = new ConversationReference();
        var userData = await botDataStore.LoadAsync(key, BotStoreType.BotUserData, CancellationToken.None);

        //set state data
        userData.SetProperty("key 1", "value1");
        userData.SetProperty("key 2", "value2");
        //get state data
        userData.GetProperty<string>("key 1");
        userData.GetProperty<string>("key 2");

        await botDataStore.SaveAsync(key, BotStoreType.BotUserData, userData, CancellationToken.None);
        await botDataStore.FlushAsync(key, CancellationToken.None);
    }
    await Conversation.SendAsync(activity, () => new Dialogs.RootDialog());
}

这篇关于从Message Controller c#访问会话数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆