加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 如何解决Bot Framework MessagesController中的当前对话框

发布时间:2020-12-15 04:23:13 所属栏目:百科 来源:网络整理
导读:在我的Messages控制器中,我想检查传入消息的堆栈中是否有某个对话框,然后再将其分配给对话框,以便我可以抑制某些条件行为.我尝试按照 this answer解析IDialogStack,如下所示: public async TaskHttpResponseMessage Post([FromBody] Activity incomingMessa
在我的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);
    }

但是,我得到以下例外:

{“An error occurred during the activation of a particular registration. See the inner exception for details. Registration: 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 —> Object reference not set to an instance of an object. (See inner exception for details.)”

内部异常:

“Object reference not set to an instance of an object.”

” at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable1 parameters)rn at Autofac.Core.Resolving.InstanceLookup.<Execute>b__5_0()rn at Autofac.Core.Lifetime.LifetimeScope.GetOrCreateAndShare(Guid id,Func1 creator)rn at Autofac.Core.Resolving.InstanceLookup.Execute()rn at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope,IComponentRegistration registration,IEnumerable1 parameters)rn at Autofac.Core.Resolving.ResolveOperation.Execute(IComponentRegistration registration,IEnumerable1 parameters)rn at Autofac.Core.Lifetime.LifetimeScope.ResolveComponent(IComponentRegistration registration,IEnumerable1 parameters)rn at Autofac.ResolutionExtensions.TryResolveService(IComponentContext context,Service service,IEnumerable1 parameters,Object& instance)rn at Autofac.ResolutionExtensions.ResolveService(IComponentContext context,IEnumerable1 parameters)rn at Autofac.ResolutionExtensions.Resolve[TService](IComponentContext context,IEnumerable1 parameters)rn at Autofac.ResolutionExtensions.Resolve[TService](IComponentContext context)rn at Progressive.CQBot.Controllers.MessagesController.d__0.MoveNext() in D:SourceReposQUO_Cognitive_QuotingSrcCQBotControllersMessagesController.cs:line 33″

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

解决方法

在解析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>();
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读