1
votes

I am trying to understand the Proactive bot sample, where we create a scope, load the dialog stack, interrupt it in between and execute the interrupted one. Can somebody explain me what are scopes in Dependency Injection. I am new to dependency injection and Autofac in C#.

1) What does DialogModule.BeginLifetimeScope(Conversation.Container do?

2) What does var stack = scope.Resolve<IDialogStack>(); do?

3) What does await stack.PollAsync(CancellationToken.None); do?

4) What does await botData.FlushAsync(CancellationToken.None); do?

// Create a scope that can be used to work with state from bot framework.
    using (var scope = DialogModule.BeginLifetimeScope(Conversation.Container, message))
    {
        var botData = scope.Resolve<IBotData>();
        await botData.LoadAsync(CancellationToken.None);

        // This is the dialog stack.
        var stack = scope.Resolve<IDialogStack>();

        // Create the new dialog and add it to the stack.
        var dialog =new SurveyDialog();
        stack.Call(dialog.Void<object, IMessageActivity>(), null);
        await stack.PollAsync(CancellationToken.None);

        // Flush the dialog stack back to its state store.
        await botData.FlushAsync(CancellationToken.None);        
    }
1
Did you read this? - Steven
I went through this, but didn't get that. Can you please explain my 4 questions @Steven - Kunal Mukherjee
I'm sorry, but I'm not familiar with Bot framework. - Steven
No problem @Steven - Kunal Mukherjee

1 Answers

1
votes

Scope relates to visibility and lifetime. In a multi-threaded application, there are some objects that can be used across threads and others that should be disposed of when processing completes. The bot framework sdk allows for processing multiple messages at once. Some objects used by the bot builder to process messages are scoped to conversation id, and only one activity keyed on conversation id can be processed at a time.

1) What does DialogModule.BeginLifetimeScope(Conversation.Container do?

BeginSlifetimeScope can be found here: github:/Microsoft.Bot.Builder.Autofac/Dialogs/DialogModule.cs#L62 Basically, this line is using autofac to control the creation, scope, and lifetime of services used by the Bot Framework. Please see here: http://autofaccn.readthedocs.io/en/latest/lifetime/ for more information on autofac lifetime.

2) What does var stack = scope.Resolve<IDialogStack>(); do?

Autofac will resolve this to the registered IDialogTaskManager.DialogTasks[0] see here: github:/Microsoft.Bot.Builder.Autofac/Dialogs/DialogModule.cs#L223

3) What does await stack.PollAsync(CancellationToken.None); do?

This is an internal BotFramework method on IEventLoop. This implementation (on DialogTask) can be found here: github:/Microsoft.Bot.Builder/Dialogs/DialogTask.cs#L308 This method relates to processing work within the bot framework's internal event system.

4) What does await botData.FlushAsync(CancellationToken.None); do?

IBotData implementations are responsible for ensuring data is persisted to the store when .FlushAsync is called.