Bot Framework Maintaining User State

戏子无情 提交于 2020-08-09 09:46:28

问题


Previously i was using Microsoft state service to maintain user state in my bot. Now that Microsoft has stopped supporting state service, i have written a static object to store user data as shown below. Just wanted to know is this a right approach to maintain user state because at once around 8000 user might be using the application. I am just worried if it will cause some concurrent. Please suggest if there is a better way to maintain user state in bot framework

    private static object objectLock = new object();
    public void SetBotCache(T CaceData, string userID)
    {

  _sessionData.AddOrUpdate(userID, CaceData, (key, oldValue) => CaceData);

    }

    public T GetBotCache(string userID)
    {
        lock (objectLock)
        {
            if (_sessionData.Count > 0)
            {
                return _sessionData.First(a => a.Key == userID).Value;
            }
            else
            {
               return default(T);
            }
        }
    }

    public void RemoveCache(string userID)
    {
        lock (objectLock)
        {
            T res;
            _sessionData.TryRemove(userID, out res);
        }
    }

回答1:


The Microsoft Bot Framework engineers have provided BotBuilder-Azure specifically for this purpose: https://github.com/Microsoft/BotBuilder-Azure https://www.nuget.org/packages/Microsoft.Bot.Builder.Azure/

The Bot Framework blog also provides instructions for how to setup Azure Table Storage and DocumentDb (now CosmosDb): https://blog.botframework.com/2017/07/18/saving-state-azure-extensions/




回答2:


i have written a static object to store user data as shown below. Just wanted to know is this a right approach to maintain user state because at once around 8000 user might be using the application.

It seems that you implement in-memory cache for storing bot user data by yourself, the approach is ok for testing purposes. As we know, the data stored with in-memory is non-persistent, if the bot application restart or crash, the data been cached will be lost, so it is not recommended for production environments.

You said your bot application will published on production server for about 8000 users using. As Eric Dahlvang mentioned, in production environments, you can use Azure Extensions to store your bot's state data in either Table Storage, CosmosDB, or SQL.



来源:https://stackoverflow.com/questions/49079751/bot-framework-maintaining-user-state

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