System.ArgumentNullException using botframework vision api

时间:2016-11-26 18:39:52

标签: c# botframework

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--- ~

}

I'm getting the error message above when I try to test my bot online @ dev.botframework.com.

Below is some of the related codes in 'MessagesController.cs' that is giving me the error message, line 51 starts with the if statement, my bot test just fine without the block of code that starts at line 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
                }
            }
    }

I've tried debugging it but it doesn't give me an error message locally on my PC, only when I am testing it online. I do not know why I am getting this error message.

1 个答案:

答案 0 :(得分:1)

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

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

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