如何在Bot Framework MessagesController中解析当前对话框堆栈?

时间:2017-09-12 20:48:34

标签: c# botframework

在我的Messages控制器中,我想检查传入消息的堆栈中是否有某个对话框,然后再将其发送到对话框,以便我可以抑制某些条件行为。我按照this answer尝试解析IDialogStack,如下所示:

public async Task<HttpResponseMessage> Post([FromBody] Activity incomingMessage)
    {
        try
        {
            if (incomingMessage.Type == ActivityTypes.Message)
            {
                using (var scope = DialogModule.BeginLifetimeScope(Conversation.Container, activity))
                {
                    var stack = scope.Resolve<IDialogStack>();
                }
                ...

以下是我在Global.asax中注册的模块:

    private void RegisterBotModules()
    {
        var builder = new ContainerBuilder();

        builder.RegisterModule(new DialogModule());
        builder.RegisterModule(new ReflectionSurrogateModule());
        builder.RegisterModule(new DialogModule_MakeRoot());

        builder.RegisterModule<GlobalMessageHandler>();

        builder.RegisterModule(new AzureModule(Assembly.GetExecutingAssembly()));

        var store = new TableBotDataStore(/*connection string*/);
        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();

        builder.Update(Conversation.Container);
        var config = GlobalConfiguration.Configuration;
        config.DependencyResolver = new AutofacWebApiDependencyResolver(Conversation.Container);
    }

但是,我得到以下例外:

  

{“在激活特定注册期间发生错误。有关详细信息,请参阅内部异常。注册:Activator = IDialogTask(DelegateActivator),Services = [Microsoft.Bot.Builder.Dialogs.Internals.IDialogStack,Microsoft.Bot .Builder.Dialogs.Internals.IDialogTask],Lifetime = Autofac.Core.Lifetime.CurrentScopeLifetime,Sharing = Shared,Ownership = OwnedByLifetimeScope ---&gt;对象引用未设置为对象的实例。(有关详细信息,请参阅内部异常。 )“

内部异常:

  

“对象引用未设置为对象的实例。”

      Autofac.Core.Resolving,Autofac.Core.Resolving.InstanceLookup.Execute()\ r \ n在Autofac.Core.Resolving上的Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable 1 parameters)\r\n at Autofac.Core.Resolving.InstanceLookup.<Execute>b__5_0()\r\n at Autofac.Core.Lifetime.LifetimeScope.GetOrCreateAndShare(Guid id, Func 1个创建者)\ r \ n .ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope,IComponentRegistration注册,IEnumerable 1 parameters)\r\n at Autofac.Core.Resolving.ResolveOperation.Execute(IComponentRegistration registration, IEnumerable 1个参数)\ r \ n在Autofac.Core.Lifetime.LifetimeScope.ResolveComponent(IComponentRegistration注册,IEnumerable 1 parameters)\r\n at Autofac.ResolutionExtensions.TryResolveService(IComponentContext context, Service service, IEnumerable 1个参数,Object&amp; instance )\ r \ n在Autofac.ResolutionExtensions.ResolveService(IComponentContext上下文,服务服务,IEnumerable 1 parameters)\r\n at Autofac.ResolutionExtensions.Resolve[TService](IComponentContext context, IEnumerable 1参数)\ r \ n at Autofac.ResolutionExtensions.Resolve [TService](IComponentContext context)\ r \ n在Progressive .CQBot.Controllers.MessagesController.d__0.MoveNext()在D:\ Source \ Repos \ QUO_Cognitive_Quoting \ Src \ CQBot \ Controllers \ MessagesController.cs:第33行“

链接答案中的建议是否已弃用?我缺少一些模块注册吗?我会很感激任何方向!

1 个答案:

答案 0 :(得分:9)

在解析 IDialogStack 之前,需要在范围内加载 BotData

请尝试以下方法:

using (var scope = DialogModule.BeginLifetimeScope(Conversation.Container, activity))
{
    var botData = scope.Resolve<IBotData>();
    await botData.LoadAsync(new System.Threading.CancellationToken());

    var stack = scope.Resolve<IDialogStack>();
}