我看到很多文章建议不要使用默认的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 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/