gpt4 book ai didi

c# - 将 BotState 迁移到 Azure 表存储 : no more possibility to access UserData without IDialogContext?

转载 作者:太空宇宙 更新时间:2023-11-03 14:58:35 26 4
gpt4 key购买 nike

我最近使用 Microsoft documentation 将自定义状态数据从现已弃用的 StateClient 切换到 Azure 表存储,请执行以下步骤:

  • 已创建存储
  • 已添加到连接字符串
  • 已添加到 Global.asax.cs 中的 Autofac 注册

这对于对话框内的所有调用(例如 context.UserData.SetValue("myKey", "myValue");)都效果很好。

但是当我们没有 IDialogContext 时,似乎不再有可能直接从 Activity 对象获取 UserData,例如,如果您想使用 MessageController 中的这些数据。

以前,我在做:

var botState = activity.GetStateClient().BotState;
var userData = await botState.GetUserDataAsync(activity.ChannelId, activity.From.Id, token);

这些操作在最新的 BotBuilder 版本中已被宣布为已弃用。有什么解决方案可以做同样的事情吗?

编辑:

添加我的 global.asax.cs:

public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
GlobalConfiguration.Configuration.Filters.Add(new ExceptionFilter());

var builder = new ContainerBuilder();

//register the bot builder module
builder.RegisterModule(new DialogModule());

//register project dependencies
builder.RegisterModule(new BotModule());

//Http config
var config = GlobalConfiguration.Configuration;

//register controller
builder.RegisterApiControllers(Assembly.GetExecutingAssembly());

//create container
var container = builder.Build();
config.DependencyResolver = new AutofacWebApiDependencyResolver(container);

GlobalConfiguration.Configure(WebApiConfig.Register);
}
}

以及关联的 BotModule:

public class BotModule : Module
{
protected override void Load(ContainerBuilder builder)
{
base.Load(builder);

// Scorable pour l'interruption de dialogue
builder.Register(c => new InterruptScorable(c.Resolve<IDialogTask>())).As<IScorable<IActivity, double>>().InstancePerLifetimeScope();

// Manage BotData in Sql Datatable
var store = new TableBotDataStore(ConfigurationHelper.XXXXX, "BotData");
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();

// Logger de conversation
builder.Register(c => new ActivityLogger(c.Resolve<IBotData>())).As<IActivityLogger>().InstancePerLifetimeScope();

// Dialogue de base
builder.RegisterType<RootDialog>().As<IDialog<object>>().InstancePerDependency();
}
}

最佳答案

我对您的代码进行了一些修改,以删除已弃用的 .Build() 方法。

public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
Conversation.UpdateContainer(builder =>
{
builder.RegisterModule(new DialogModule());
builder.RegisterModule<BotModule>();
builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
});

GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(Conversation.Container);

GlobalConfiguration.Configure(WebApiConfig.Register);
}
}

以这种方式注册模块后,Jason Sowers 在此问题上分享的代码应该会有所帮助:How can I access Bot Framework ConversationData outside of a dialog?

关于c# - 将 BotState 迁移到 Azure 表存储 : no more possibility to access UserData without IDialogContext?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47543122/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com