2017-09-12 25 views
2

在我的消息控制器中,我想檢查一個特定的對話框是否在傳入消息的堆棧中,以便我可以禁止某些條件行爲。我試圖解決IDialogStackthis answer,就像這樣:如何解決我的Bot Framework MessagesController中的當前對話框堆棧?

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.B uilder.Dialogs.Internals.IDialogStack,Microsoft.Bot.Builder.Dialogs.Internals.IDialogTask],Lifetime = Autofac.Core.Lifetime.CurrentScopeLifetime,Sharing = Shared,Ownership = OwnedByLifetimeScope --->對象引用未設置爲一個東西。 (詳情請參見內部異常。) 「

隨着內異常:

」對象引用不設置爲一個對象的一個​​實例「

」 在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 creator)\ r \ n at Autofac.Core.Resolving.InstanceLookup.Execute()\ r \ n at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope,IComponentRegistration registration,IEnumerable 1 parameters)\r\n at Autofac.Core.Resolving.ResolveOperation.Execute(IComponentRegistration registration, IEnumerable 1 parameters )\ r \ n在Autofac.Core.Lifetime.Lifeti在Autofac.ResolutionExtensions.ResolveService(IComponentContext上下文,服務服務,IEnumerable 1 parameters)\r\n at Autofac.ResolutionExtensions.Resolve[TService](IComponentContext context, IEnumerable 1個參數)上\ r \ n \ Autofac.ResolutionExtensions.Resolve \ TService \ r \ n下的\ r \ n 1個參數,對象&實例) ](IComponentContext上下文)\ r \ n在D:\ Source \ Repos \ QUO_Cognitive_Quoting \ Src \ CQBot \ Controllers \ MessagesController.cs中的Progressive.CQBot.Controllers.MessagesController.d__0.MoveNext()中:第33行「

鏈接的答案中的建議是否已棄用?有沒有我缺少的模塊註冊?我會感謝任何方向!

回答

5

BotData需要在解析IDialogStack之前加載。

請嘗試以下方法:

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>(); 
} 
+1

這奏效了!非常感謝您承認丟失的鏈接。 –

相關問題