C# 如何在启动中间件中获取turncontext
我正在使用bot框架v4。是否有任何方法可以在startup.cs中获取iTunesContext,以设置用于保存bot状态的transcriptlogger中间件 下面给出了我尝试过的示例解决方案C# 如何在启动中间件中获取turncontext,c#,asp.net-core,botframework,middleware,chatbot,C#,Asp.net Core,Botframework,Middleware,Chatbot,我正在使用bot框架v4。是否有任何方法可以在startup.cs中获取iTunesContext,以设置用于保存bot状态的transcriptlogger中间件 下面给出了我尝试过的示例解决方案 IStorage userDataStore = new Microsoft.Bot.Builder.Azure.AzureBlobStorage(Configuration["AzureTableStorageConnectionString"], Configuration[
IStorage userDataStore = new Microsoft.Bot.Builder.Azure.AzureBlobStorage(Configuration["AzureTableStorageConnectionString"], Configuration["AzureBlobStorageContainerName"]);
var userstate = new UserState(userDataStore);
var myPropertyAccessor = userstate.CreateProperty<UserProfile>(nameof(UserProfile));
services.AddSingleton<IStatePropertyAccessor<UserProfile>>(myPropertyAccessor);
var logger = new TranscriptLogger(Configuration);
services.AddTransient<ITranscriptLogger, TranscriptLogger>();
services.AddBot<DialogAndWelcomeBot<Dialog>>(options =>
{
options.CredentialProvider = new ConfigurationCredentialProvider(Configuration);
options.Middleware.Add(new TranscriptLoggerMiddleware(logger));
});
我非常感谢您的帮助:)我已经在我的自定义转录本记录器中添加了IMidware实现,我可以访问iTunesContext
public class TranscriptLogger : IMiddleware
{
public IConfiguration _configuration;
private readonly IStatePropertyAccessor<UserProfile> _greetingStateAccessor;
public TranscriptLogger(IConfiguration configuration, UserState userState)
{
_configuration = configuration;
_greetingStateAccessor = userState.CreateProperty<UserProfile>
(nameof(UserProfile));
}
public async Task OnTurnAsync(ITurnContext turnContext, NextDelegate next,
CancellationToken cancellationToken = default)
{
var userStateData = await _greetingStateAccessor.GetAsync(turnContext, ()
=> new UserProfile());
turnContext.OnSendActivities(async (newContext, activities, nextSend) =>
{
foreach (Microsoft.Bot.Schema.Activity currentActivity in
activities.Where(a => a.Type == ActivityTypes.Message))
{
if(!string.IsNullOrEmpty(currentActivity.AsMessageActivity().Text))
// Save conversation data...
}
return await nextSend();
});
}
}
public类记录程序:imidware
{
公共IConfiguration\u配置;
专用只读IStatePropertyAccessor\u greetingStateAccessor;
公共转录记录器(IConfiguration配置,UserState UserState)
{
_配置=配置;
_greetingStateAccessor=userState.CreateProperty
(用户名(UserProfile));
}
公共异步任务OnTurnAsync(ITurnContext turnContext,NextDelegate next,
CancellationToken CancellationToken=默认值)
{
var userStateData=await\u greetingStateAccessor.GetAsync(turnContext,()
=>newuserprofile());
turnContext.OnSendActivities(异步(newContext、activities、nextSend)=>
{
foreach(Microsoft.Bot.Schema.Activity中的currentActivity
activities.Where(a=>a.Type==ActivityTypes.Message))
{
如果(!string.IsNullOrEmpty(currentActivity.AsMessageActivity().Text))
//保存会话数据。。。
}
return wait nextSend();
});
}
}
到目前为止您尝试了什么?@KyleDelaney,请查看下面的评论您刚刚发布了答案,而不是评论。请通过编辑问题本身而不是发布答案来提供所有需要的信息。如果评论中不包含任何图片或代码,您可以在评论中提供一些附加信息,但这些信息仍然不应作为答案发布。在任何情况下,您发布的示例代码的相关性是什么?我在其中没有看到任何对转录本记录器中间件的引用。@KyleDelaney,我已经编辑了PostOk。从您的代码中不清楚为什么要在启动时使用iTunesContext。与其问如何在启动时获得一个转折点上下文,您可能是想问如何让转录本记录器中间件工作吗?如果是这样的话,你能解释一下你想如何记录这些活动,以及为什么这需要一个turn上下文吗?
public class TranscriptLogger : IMiddleware
{
public IConfiguration _configuration;
private readonly IStatePropertyAccessor<UserProfile> _greetingStateAccessor;
public TranscriptLogger(IConfiguration configuration, UserState userState)
{
_configuration = configuration;
_greetingStateAccessor = userState.CreateProperty<UserProfile>
(nameof(UserProfile));
}
public async Task OnTurnAsync(ITurnContext turnContext, NextDelegate next,
CancellationToken cancellationToken = default)
{
var userStateData = await _greetingStateAccessor.GetAsync(turnContext, ()
=> new UserProfile());
turnContext.OnSendActivities(async (newContext, activities, nextSend) =>
{
foreach (Microsoft.Bot.Schema.Activity currentActivity in
activities.Where(a => a.Type == ActivityTypes.Message))
{
if(!string.IsNullOrEmpty(currentActivity.AsMessageActivity().Text))
// Save conversation data...
}
return await nextSend();
});
}
}