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


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


对于对话框中的所有呼叫(例如context.UserData.SetValue("myKey", "myValue");),此方法都很好。

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

以前,我在做:

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表存储:没有IDialogContext,是否有可能访问UserData?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47543122/

10-13 02:34