Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在不使用TurnContext对象的情况下管理和存储TurnState_C#_.net Core_Botframework - Fatal编程技术网

C# 如何在不使用TurnContext对象的情况下管理和存储TurnState

C# 如何在不使用TurnContext对象的情况下管理和存储TurnState,c#,.net-core,botframework,C#,.net Core,Botframework,在下面的代码段中,需要TurnContext对象从Cosmos获取底层Bot状态并将其保存回去- //Get the TurnContext from the Dictionary TurnContextReferences.TryGetValue(sessionStateChangedEventData.SessionId, out ITurnContext turnContext); if (turnContext != null) { var conversationData =

在下面的代码段中,需要TurnContext对象从Cosmos获取底层Bot状态并将其保存回去-

//Get the TurnContext from the Dictionary
TurnContextReferences.TryGetValue(sessionStateChangedEventData.SessionId, out ITurnContext turnContext);
if (turnContext != null)
{
    var conversationData = await BotStateAccessors
                      .ConversationStateAccessor
                      .GetAsync(turnContext, () => new ConversationStateDataModel());
    if (!conversationData.LiveAgentChatClosed)
    {
        conversationData.LiveAgentChatClosed = true;
        await BotStateAccessors.ConversationStateAccessor.SetAsync(turnContext, conversationData);
        await BotConversationState.SaveChangesAsync(turnContext);
    }
}

如果不直接使用TurnContext,是否有可能实现同样的效果?

访问机器人状态和向用户发送消息所需的所有信息都在对话引用中。您可以使用
ContinueConversationAsync
方法从保存的会话引用构建turn上下文。您可以在中看到如何执行此操作:


回合上下文不应存在于其关联回合之外。您应该保存对话引用,而不是转换上下文。

谢谢您的提问。因此,要明确的是,您有一个回合上下文字典,用于加载对话状态,然后在不处理实际用户发起的回合时再次保存它。对吗?没错,这是对的。此代码是NotificationController的一部分,它从第三方系统接收一些事件,并基于此需要更新状态并将消息传递回用户(如果有)。我的答案可以接受吗?@KyleDelaney,我想我已经明白你的意思了,但我想重申一下你的建议,即使用ContinueConversationAsync回调或在其内部,我们应该获取TurnContext,然后获取属性访问器和更新状态。但这也意味着在更新状态的地方使用ContinueConversationAsync代码。如果是推荐的,我确实注意到在我将活动发送回用户的地方,我已经使用了这种模式。所以,这只是找出在控制器的其他位置使用ContinueConversationAsync是否合适的问题。@KyleDelaney,我看到另一个问题,即在ContinueConversationAsync回调中获得的turnContext的大多数属性都为null。而我维护的turnContext中有区域设置、活动时间戳、消息等。但是这段代码给了我null。在dictionary-await((BotFrameworkAdapter)HttpAdapter)中添加conversationReference之前,我可以通过某种方式分配这些属性吗?ContinueConversationAsync(MSAppId,conversationReferenceItem.conversationReference,async(上下文,令牌)=>{var locale=context?.Activity?.locale}
await ((BotAdapter)_adapter).ContinueConversationAsync(_appId, conversationReference, BotCallback, default(CancellationToken));