Warm tip: This article is reproduced from serverfault.com, please click

Botframework v4 preserve dialogSet after deployment

发布于 2020-11-30 10:59:39

I'm using Microsoft bot framework v4. Its DialogSet is always kept in memory, and will vanish after a code rebuild (i.e deployment), so existing conversation that happens to be in a dialog just before the rebuild, will get an error after deployment is done (the needed dialog is not found).

export class DialogBot extends ActivityHandler {


  private readonly conversationState: BotState;
  private readonly userState: BotState;
  private readonly dialogState: StatePropertyAccessor<DialogState>;
  private readonly nlpProcessor: NlpProcessor;
  private readonly dialogSet: DialogSet;
  private readonly cacheService: CacheService;
  private readonly mainProcessor: MainProcessors;

  constructor(conversationState: BotState, userState: BotState) {
    super();
    this.conversationState = conversationState;
    this.userState = userState;
    this.dialogState = this.conversationState.createProperty<DialogState>(
      'DialogState'
    );
    this.dialogSet = new DialogSet(this.dialogState);

You can see the dialogSet is always initiated new after a deployment

this.dialogSet = new DialogSet(this.dialogState);

Although the dialogStack is persisted into dialogState, its dialogs array isn't

export class DialogSet {
    private readonly dialogs: { [id: string]: Dialog } = {};
    private readonly dialogState: StatePropertyAccessor<DialogState>;
    private _telemetryClient: BotTelemetryClient;
    private _version: string;

The dialogs array only lives in memory, which holds respective Dialog objects the stack refers to. Once you rebuild code, it's gone.

I tried to extend DialogSet class, and save its dialogs property to Blob storage, however, Blob is recording only json, and doesnt reconstruct the dialogs array properly (which holds all Dialog objects).

Anyone has a solution to preserve DialogSet state through deployment ? Many thanks in advance.

Questioner
Chinh Le
Viewed
0
Kyle Delaney 2021-01-08 03:14:19

A dialog set is meant to be static and not related to state. A dialog set represents a set of dialogs that your bot can potentially begin, rather than the dialogs your bot has actually begun in a specific conversation. A dialog set should be the same every time your bot runs, and it should not change. A dialog stack stored in dialog state is the thing that changes based on specific conversations.

You should always add all possible dialogs into the dialog set at the point of initiation. This is how all Bot Framework dialogs are supposed to work, and any bot that does not do this is broken. You can see this pattern in every official sample (note that ComponentDialog.addDialog calls DialogSet.add internally). No dialog is ever called that wasn't added at the time of construction. This ensures that your bot works after restart.