public async Task AutoSaveStateMiddleware_Chain() { var storage = new MemoryStorage(); // setup userstate var userState = new UserState(storage); var userProperty = userState.CreateProperty <int>("userCount"); // setup convState var convState = new ConversationState(storage); var convProperty = convState.CreateProperty <int>("convCount"); var bss = new AutoSaveStateMiddleware() .Add(userState) .Add(convState); var adapter = new TestAdapter() .Use(bss); const int USER_INITITAL_COUNT = 100; const int CONVERSATION_INITIAL_COUNT = 10; BotCallbackHandler botLogic = async(context, cancellationToken) => { // get userCount and convCount from botStateSet var userCount = await userProperty.GetAsync(context, () => USER_INITITAL_COUNT).ConfigureAwait(false); var convCount = await convProperty.GetAsync(context, () => CONVERSATION_INITIAL_COUNT).ConfigureAwait(false); if (context.Activity.Type == ActivityTypes.Message) { if (context.Activity.Text == "get userCount") { await context.SendActivityAsync(context.Activity.CreateReply($"{userCount}")); } else if (context.Activity.Text == "get convCount") { await context.SendActivityAsync(context.Activity.CreateReply($"{convCount}")); } } // increment userCount and set property using accessor. To be saved later by AutoSaveStateMiddleware userCount++; await userProperty.SetAsync(context, userCount); // increment convCount and set property using accessor. To be saved later by AutoSaveStateMiddleware convCount++; await convProperty.SetAsync(context, convCount); }; await new TestFlow(adapter, botLogic) .Send("test1") .Send("get userCount") .AssertReply((USER_INITITAL_COUNT + 1).ToString()) .Send("get userCount") .AssertReply((USER_INITITAL_COUNT + 2).ToString()) .Send("get convCount") .AssertReply((CONVERSATION_INITIAL_COUNT + 3).ToString()) .StartTestAsync(); // new adapter on new conversation var bss2 = new AutoSaveStateMiddleware() .Add(userState) .Add(convState); adapter = new TestAdapter(new ConversationReference { ChannelId = "test", ServiceUrl = "https://test.com", User = new ChannelAccount("user1", "User1"), Bot = new ChannelAccount("bot", "Bot"), Conversation = new ConversationAccount(false, "convo2", "Conversation2"), }) .Use(bss2); await new TestFlow(adapter, botLogic) .Send("get userCount") .AssertReply((USER_INITITAL_COUNT + 4).ToString(), "user count should continue on new conversation") .Send("get convCount") .AssertReply((CONVERSATION_INITIAL_COUNT + 1).ToString(), "conversationCount for conversation2 should be reset") .StartTestAsync(); }
public AdapterWithErrorHandler(IConfiguration configuration, ILogger <BotFrameworkHttpAdapter> logger, TelemetryInitializerMiddleware telemetryInitializerMiddleware, AutoSaveStateMiddleware autoSaveStateMiddleware) : base(configuration, logger) { Use(telemetryInitializerMiddleware); Use(autoSaveStateMiddleware); OnTurnError = async(turnContext, exception) => { // Log any leaked exception from the application. logger.LogError(exception, $"Exception caught : {exception.Message}"); // Send a catch-all apology to the user. await turnContext.SendActivityAsync("Sorry, it looks like something went wrong."); }; }