WebClient.DownloadData throws an exception in Bot Framework

余生长醉 提交于 2019-12-11 14:45:20

问题


I am uploading an image and using WebClient with DownloadData to get the byte array, but now it throws me an exception:

Exception: State size exceeded configured limit.
[File of type 'text/plain']

I haven't noticed such behaviour before with the exact same image. What might have gone wrong?

private async Task SendPhoto_ActivityReceivedAsync(IDialogContext context, IAwaitable<object> result)
    {
        Activity activity = await result as Activity;
        Activity activityReply = activity.CreateReply();

        if (0 < activity.Attachments?.Count)
        {
            if (activity.Attachments.FirstOrDefault().ContentType.Equals("image/jpg") ||
                activity.Attachments.FirstOrDefault().ContentType.Equals("image/jpeg") ||
                activity.Attachments.FirstOrDefault().ContentType.Equals("image/png") ||
                activity.Attachments.FirstOrDefault().ContentType.Equals("image/tiff"))
            {
                picImage = activity.Attachments.FirstOrDefault().Name;

                HttpClient httpClient= new HttpClient();
                picImageFile = await httpClient.GetByteArrayAsync(activity.Attachments.FirstOrDefault().ContentUrl);

                await context.PostAsync("Want to send another?");

                context.Wait(SendAnotherImage_ActivityReceivedAsync);
            }
        }
    }

UPDATE: Changed the WebClient to HttpClient.GetByteArrayAsync and the problem remains. The exception is shown in the Bot Emulator right after the PostAsync is executed


回答1:


The problem is that picImageFile is a variable inside of your dialog, and so it's being saved in the state because the dialog is serialized. Two options

  1. Download the image and process it in that method, without saving it in memory
  2. Save the byte array in an external storage.


来源:https://stackoverflow.com/questions/46645806/webclient-downloaddata-throws-an-exception-in-bot-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!