如何摆脱警告"不建议将Bot Framework State API用于生产环境,并且可能在将来的版本中弃用."

B. *_*Lec 7 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;
    }
}
Run Code Online (Sandbox Code Playgroud)

并在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();
        });
Run Code Online (Sandbox Code Playgroud)

但它似乎搞乱了上下文而bot并没有响应任何context.wait()方法

我也试过这个:

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

还有警告

Bot模拟器

Eri*_*ang 7

对话框会自动序列化到状态存储中.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();
Run Code Online (Sandbox Code Playgroud)

编辑:您也可以在不使用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();
Run Code Online (Sandbox Code Playgroud)