r3plica
r3plica

Reputation: 13387

How to use context.Forward with autofac

I am trying to build a bot using Microsoft Bot Framework and Autofac. I have a service which is shared between 2 Dialogs and the first Dialog instantiates the other based on a user response. Inside my Dialog I invoke this line:

await context.Forward(new StepDialog(_productProvider, _groups, _products,  0), ResumeAfter, new Activity { Text = category }, CancellationToken.None);

But I don't think this is right. In my controller, I do this:

// Create our scope
using (var scope = DialogModule.BeginLifetimeScope(Conversation.Container, model))
{

    // Create our conversation
    await Conversation.SendAsync(model, () => scope.Resolve<IDialog<object>>());
};

So I assume that I have to do something similar to get the instance of my StepDialog while maintaining the reference to the ProductProvider (which is my service).

I thought about passing the scope into the constructor of my initial Dialog but I have read that is a bad idea, so how can I go about this?

Upvotes: 1

Views: 337

Answers (1)

Ezequiel Jadib
Ezequiel Jadib

Reputation: 14787

One idea that I have seen working pretty well is to use a Dialog Factory; so you inject that into your dialog and use it to resolve other dialogs.

In the ContosoFlowers sample you will find a basic implementation of a Dialog Factory. Below some reference links:

  • The base DialogFactory
  • The ContosoFlowersDialogFactory which is just inheriting from the base DialogFactory and adding a new method for a case not covered by the base factory.
  • Here is how the factory and dialogs are being registered in AutoFac
  • Here is how the dialog factory is being used

Upvotes: 3

Related Questions