1

I have recently been working on a project learning how to create bots using the Microsoft Bot Framework! Just recently, I decided to package my dependencies in a container, specifically using UnityContainer. In doing so, I've run into a bug where my dialog.RunAsync() method is throwing a NullReferenceException.

Here is the line of code that throws the error:

await _dialog.RunAync(turnContext, _botStateService.DialogStateAccessor, cancellationToken);. 

The BotStateService is a service which handles the bot's state, and DialogStateAccessor is a property in the service. _dialog has been resolved to a MainDialog (which extends a ComponentDialog).

Here is how I am registering my dependencies with Unity - I create a container, register dependencies I need to it, and then register the container instance to my services.

container = new UnityContainer();
container.RegisterType<IStorage, MemoryStorage>(TypeLifetime.Singleton);
container.RegisterType<UserState>(TypeLifetime.Singleton);
container.RegisterType<ConversationState>(TypeLifetime.Singleton);
container.RegisterType<IBotStateService, BotStateService>(TypeLifetime.Singleton);
container.RegisterType<ComponentDialog, PrimeDialog>("prime", TypeLifetime.Singleton);
container.RegisterType<ComponentDialog, FibonacciDialog>("fibonacci", TypeLifetime.Singleton);
container.RegisterType<ComponentDialog, GreetingDialog>("greeting", TypeLifetime.Singleton);

services.AddSingleton<IUnityContainer>(container);

I already check if _dialog, turnContext, _botStateService, and cancellationToken are null (they're not). I believe that the error happens somewhere in the BotSDK, but even after building the library from source I still can't find the problem line of code. Here is the stack trace:

at Microsoft.Bot.Builder.Dialogs.ComponentDialog.BuildDialogState(DialogInstance instance)  

Does anyone have an idea as to why this might be happening?

Also please let me know if I need to include anything else! I'm new to Bot Framework, .NET and UnityContainer as a whole.

Nkosi
  • 191,971
  • 29
  • 311
  • 378
TimHan
  • 11
  • 1
  • The exception is highlighting the `_botStateService.DialogStateAccessor` part, so if you are 100% sure that `_botStateService` isn't null then `DialogStateAccessor` must be null... Add a breakpoint there and check the values. – Gusman Jun 11 '20 at 03:58
  • Why are you using multiple containers? The container used by the bot framework is unaware of the type to be injected, which is why it is being injected as null; – Nkosi Jun 11 '20 at 04:05
  • @Gusman I did, and the DialogStateAccessor has values – TimHan Jun 11 '20 at 04:42
  • @Nkosi I'm using multiple containers because I want to be able to pass around the UnityContainer to my bot and not have to list all the dependencies in my constructor. Can you explain more about what you mean by the bot framework container being unaware of the injected type? I register all types to the UnityContainer and resolve dependencies from there. – TimHan Jun 11 '20 at 04:46

0 Answers0