gpt4 book ai didi

azure-sql-database - 机器人模拟器 "ExceptionMessage": "Object reference not set to an instance of an object.", "exceptionType": "System.NullReferenceException",

转载 作者:行者123 更新时间:2023-12-02 02:56:45 25 4
gpt4 key购买 nike

我想将对话存储在聊天机器人上并为此使用 IActivityLogger。代码如下:

public class EntityFrameworkActivityLogger : IActivityLogger
{
Task IActivityLogger.LogAsync(IActivity activity)
{
IMessageActivity msg = activity.AsMessageActivity();
using (BotApplicationSqlData.ConversationDataContext dataContext = new BotApplicationSqlData.ConversationDataContext())
{
var newActivity = Mapper.Map<IMessageActivity, BotApplicationSqlData.Activity>(msg);
if (string.IsNullOrEmpty(newActivity.Id))
newActivity.Id = Guid.NewGuid().ToString();
dataContext.Activities.Add(newActivity);
dataContext.SaveChanges();
}
return null;
}
}

我收到请求错误

'https://botapplicationsqlexample.azurewebsites.net/api/messages' failed: [500] Internal Server Error .

在调试时,它显示了一个Null Reference ExceptionObject reference not set to an instance of an object

对话正在存储在 Azure 数据库中,但机器人端没有回复。谁能帮我解决这个问题?

最佳答案

我运行了这段代码,发现如果 ActivityTypes.Typing 类型的事件通过,activity.AsMessageActivity() 会返回一个空对象。要解决这个问题,只需用空检查包围保存代码:

    public async Task LogAsync(IActivity activity)
{
IMessageActivity msg = activity.AsMessageActivity();

if (msg != null)
{
using (Data.ConversationDataContext dataContext = new Data.ConversationDataContext())
{
var newActivity = Mapper.Map<IMessageActivity, Data.Activity>(msg);
if (string.IsNullOrEmpty(newActivity.Id))
newActivity.Id = Guid.NewGuid().ToString();

dataContext.Activities.Add(newActivity);
dataContext.SaveChanges();
}
}
}

关于azure-sql-database - 机器人模拟器 "ExceptionMessage": "Object reference not set to an instance of an object.", "exceptionType": "System.NullReferenceException",,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48943226/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com