首页 > 解决方案 > 为什么 GetAsync 方法会抛出 'System.Collection.KeyNotFoundException' ?(v4)

问题描述

我在整个对话过程中不断收到此异常:

System.Collections.Concurrent.dll 中的“System.Collections.Generic.KeyNotFoundException”

我发现这是由GetAsync方法引起的,但我不知道为什么。如果有人能给我解释为什么抛出这个异常,以及避免它的可能方法,我将不胜感激。

我的代码:

protected override async Task OnMessageActivityAsync(
    ITurnContext<IMessageActivity> turnContext, 
    CancellationToken cancellationToken = default(CancellationToken))
{
    // ...

    var uAccessor = _userState.CreateProperty<UserProfile>(nameof(UserProfile));
    var userProfile = await uAccessor.GetAsync(turnContext, () => 
        new UserProfile(), cancellationToken); //throws an exception

    userProfile.UserInputs.Add(turnContext.Activity.Text?.Trim());
    await uAccessor.SetAsync(turnContext, userProfile, cancellationToken);

    // ...
}

标签: c#botframework

解决方案


推荐阅读