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.