c# - 如何摆脱警告 "Bot Framework State API is not recommended for production environments, and may be deprecated in a future release."

标签 c# botframework

我看到很多文章建议不要使用默认的bot状态数据存储,因为微软肯定会在2018年3月关闭这个服务。

我目前正在开发无状态机器人,不需要任何存储空间。

我试过造一个假的

internal class DummyDataStore : IBotDataStore<BotData>
{
    public DummyDataStore()
    {
    }

    public Task<bool> FlushAsync(IAddress key, CancellationToken cancellationToken)
    {
        return Task.FromResult(true);
    }

    public Task<BotData> LoadAsync(IAddress key, BotStoreType botStoreType, CancellationToken cancellationToken)
    {
        return Task.FromResult(new BotData());
    }

    public Task SaveAsync(IAddress key, BotStoreType botStoreType, BotData data, CancellationToken cancellationToken)
    {
        return Task.CompletedTask;
    }
}

并在global.asax中用autofac注册

        Conversation.UpdateContainer(
        builder =>
        {
            //Registration of message logger
            builder.RegisterType<BotMessageLogger>().AsImplementedInterfaces().InstancePerDependency();
            //Registration of dummy data storage
            var store = new DummyDataStore();
            builder.Register(c => store).As<IBotDataStore<BotData>>().AsSelf().SingleInstance();
        });

但它似乎与上下文混淆并且机器人不响应任何 context.wait() 方法

我也试过这个:

        Conversation.UpdateContainer(
        builder =>
        {
            var store = new InMemoryDataStore();
            builder.Register(c => store)
                               .Keyed<IBotDataStore<BotData>>(AzureModule.Key_DataStore)
                               .AsSelf()
                               .SingleInstance();
        });

还是有警告

Bot emulator

最佳答案

对话框会自动序列化到状态存储中。 Bot Builder SDK 本身是 Restful ,状态存储用于启用 .Wait(methodName) 在下次调用时在 methodName 处恢复。

如果您的机器人不关心在服务器重新启动时保持状态,您可以使用
InMemoryDataStore:https://github.com/Microsoft/BotBuilder/blob/db2b8f860a3d8f7744a378930f93c4b0baa97978/CSharp/Library/Microsoft.Bot.Builder/ConnectorEx/BotData.cs#L90

builder.RegisterModule(new AzureModule(Assembly.GetExecutingAssembly()));
var store = new InMemoryDataStore();

builder.Register(c => store)
                .Keyed<IBotDataStore<BotData>>(AzureModule.Key_DataStore)
                .AsSelf()
                .SingleInstance();

builder.Register(c => new CachingBotDataStore(store,
                      CachingBotDataStoreConsistencyPolicy.ETagBasedConsistency))
                    .As<IBotDataStore<BotData>>()
                    .AsSelf()
                    .InstancePerLifetimeScope();

编辑: 您还可以在不使用 AzureModule 的情况下注册 InMemoryDataStore:

var memorystore = new InMemoryDataStore();
    builder
         .RegisterType<InMemoryDataStore>()
         .Keyed<IBotDataStore<BotData>>(typeof(ConnectorStore));

    builder.Register(c => new CachingBotDataStore(memorystore, CachingBotDataStoreConsistencyPolicy.ETagBasedConsistency))
           .As<IBotDataStore<BotData>>()
           .AsSelf()
           .SingleInstance();

关于c# - 如何摆脱警告 "Bot Framework State API is not recommended for production environments, and may be deprecated in a future release.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48326504/

相关文章:

c# - 'object' 不包含 'Name' 的定义

c# - .Net 4.0 中的动态程序集加载

node.js - Microsoft bot 未从 Firebase 云功能做出响应

node.js - 使用 BotFramework (NodeJS) 将机器人交给人类

c# - 简单的内存消息队列

c# - 在 Windows Phone 中加载 xaml 页面后如何执行任务

botframework - 在 Teams 中使用 azure 机器人服务 : how to send files to bot

javascript - 允许链接在 Bot 的弹出窗口中打开

c# - 是否有任何通用方法可以满足 LUIS 对话框中的所有意图?

c# - 如何剪切包含 url 的字符串并将其添加到数组