System.ArgumentNullException 使用 botframework 视觉 api

System.ArgumentNullException using botframework vision api

InternalServerError
{

  "message": "An error has occurred.",

  "exceptionMessage": "Value cannot be null.\r\nParameter name: source",

  "exceptionType": "System.ArgumentNullException",

  "stackTrace": "   at System.Linq.Enumerable.Any[TSource](IEnumerable`1 source)\r\n   
at Bot0.MessagesController.<Post>d__0.MoveNext() in D:\~\MessagesController.cs:line 51\r\n--- 
End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   
at System.Threading.Tasks.TaskHelpersExtensions.<CastToObject>d__3`1.MoveNext()\r\n--- ~

}

当我尝试在线测试我的机器人时收到上述错误消息@dev.botframework.com。

下面是 'MessagesController.cs' 中的一些相关代码,它给我错误消息,第 51 行以 if 语句开头,我的机器人测试很好,没有从第 51 行开始的代码块:

ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));
const string visionApiKey = "mykey123";

//Vision SDK classes
VisionServiceClient visionClient = new VisionServiceClient(visionApiKey);
VisualFeature[] visualFeatures = new VisualFeature[] {
                            VisualFeature.Adult, //recognize adult content
                            VisualFeature.Categories, //recognize image features
                            VisualFeature.Description //generate image caption
                            };
AnalysisResult analysisResult = null;

ConnectorClient connector1 = new ConnectorClient(new Uri(activity.ServiceUrl));
const string emotionApiKey = "mykey456";

//Emotion SDK objects that take care of the hard work
EmotionServiceClient emotionServiceClient = new EmotionServiceClient(emotionApiKey);
Emotion[] emotionResult = null;

if (activity.Attachments.Any() && activity.Attachments.First().ContentType.Contains("image"))
     {
            //stores image url (parsed from attachment or message)
            string uploadedImageUrl = activity.Attachments.First().ContentUrl; ;
            uploadedImageUrl = HttpUtility.UrlDecode(uploadedImageUrl.Substring(uploadedImageUrl.IndexOf("file=") + 5));

            using (Stream imageFileStream = File.OpenRead(uploadedImageUrl))
            {
                try
                {
                    analysisResult = await visionClient.AnalyzeImageAsync(imageFileStream, visualFeatures);
                }
                catch (Exception e)
                {
                    analysisResult = null; //on error, reset analysis result to null
                }
            }

            using (Stream imageFileStream1 = File.OpenRead(uploadedImageUrl))
            {
                try
                {
                    emotionResult = await emotionServiceClient.RecognizeAsync(imageFileStream1);
                }
                catch (Exception e)
                {
                    emotionResult = null; //on error, reset analysis result to null
                }
            }
    }

我试过调试它,但它没有在我的 PC 上本地给我错误消息,只有当我在线测试它时才出现。我不知道为什么会收到此错误消息。

根据异常消息和示例代码,在尝试调用 .Any() 扩展方法时,activity.Attachments 看起来像是 null。在尝试访问之前检查以确保 activity.Attachments 不是 null

if (activity.Attachments != null && 
    activity.Attachments.Any() && 
    activity.Attachments.First().ContentType.Contains("image")) {
    //...
}

每个 object.Something 都有可能筹集 NRE,因此您在进行这些调用时应考虑到这一点。