locked
Problems with Await RRS feed

  • Question

  • User1122355199 posted

    Hello everyone and thanks for the help in advance.  I am trying to use an example located at:  https://www.twilio.com/docs/sms/tutorials/how-to-receive-and-download-images-incoming-mms-csharp.  I am having a problem with the code:

                for (var i = 0; i < numMedia; i++)
                {
                    var mediaUrl = Request.Form[$"MediaUrl{i}"];
                    //Trace.WriteLine(mediaUrl);
                    var contentType = Request.Form[$"MediaContentType{i}"];
    
                    var filePath = GetMediaFileName(mediaUrl, contentType);
                    await DownloadUrlToFileAsync(mediaUrl, filePath);
                }

    The line await DownloadUrlToFileAsync(mediaUrl, filePath); generates an error "The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type to 'Task<ActionResult>'.   The await function looks like:

            private static async Task DownloadUrlToFileAsync(string mediaUrl, string filePath)
            {
                using (var client = new HttpClient())
                {
                    var response = await client.GetAsync(mediaUrl);
                    var httpStream = await response.Content.ReadAsStreamAsync();
                    using (var fileStream = System.IO.File.Create(filePath))
                    {
                        await httpStream.CopyToAsync(fileStream);
                        await fileStream.FlushAsync();
                    }
                }
                
            }

    copied straight from the Twilio example.  I really don't have much experience using the Await function, so I'm not sure how to handle this.  Any help would be appreciated.

    "

    Monday, December 17, 2018 10:53 PM

Answers

  • User475983607 posted

    The method that has the for loop needs an async too.  The link illustrates this...

        public class MmsController : TwilioController
        {
            private const string SavePath = "~/App_Data/";
    
            [HttpPost]
            public async Task<TwiMLResult> Index(SmsRequest request, int numMedia)
            {
                for (var i = 0; i < numMedia; i++)
                {
                    var mediaUrl = Request.Form[$"MediaUrl{i}"];
                    Trace.WriteLine(mediaUrl);
                    var contentType = Request.Form[$"MediaContentType{i}"];
    
                    var filePath = GetMediaFileName(mediaUrl, contentType);
                    await DownloadUrlToFileAsync(mediaUrl, filePath);
                }
    
                var response = new MessagingResponse();
                var body = numMedia == 0 ? "Send us an image!" : 
                    $"Thanks for sending us {numMedia} file(s)!";
                response.Message(body);
                return TwiML(response);
            }

    Is your code from a Web Forms application?  Can you share your code?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 17, 2018 11:45 PM

All replies

  • User475983607 posted

    The method that has the for loop needs an async too.  The link illustrates this...

        public class MmsController : TwilioController
        {
            private const string SavePath = "~/App_Data/";
    
            [HttpPost]
            public async Task<TwiMLResult> Index(SmsRequest request, int numMedia)
            {
                for (var i = 0; i < numMedia; i++)
                {
                    var mediaUrl = Request.Form[$"MediaUrl{i}"];
                    Trace.WriteLine(mediaUrl);
                    var contentType = Request.Form[$"MediaContentType{i}"];
    
                    var filePath = GetMediaFileName(mediaUrl, contentType);
                    await DownloadUrlToFileAsync(mediaUrl, filePath);
                }
    
                var response = new MessagingResponse();
                var body = numMedia == 0 ? "Send us an image!" : 
                    $"Thanks for sending us {numMedia} file(s)!";
                response.Message(body);
                return TwiML(response);
            }

    Is your code from a Web Forms application?  Can you share your code?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 17, 2018 11:45 PM
  • User-552477072 posted

    Hi kmcnet,

    your writing of Task is not proper. It will be in format

    Task<string> DownloadUrlToFileAsync ( ....,....)

    Also, you can only use await in an async method.  Method what called DownloadUrlToFileAsync, cannot be async. 

    You'll have to use your own async-compatible context, call Wait on the returned Task in the Main method, or just ignore the returned Task and just block on the call to Read. Note that Wait will wrap any exceptions in an AggregateException.

    For more info you can see Async and Await

    Pls don't forget to mark as answer, when my suggestion helps you. Thanks

    Tuesday, December 18, 2018 2:21 AM
  • User1122355199 posted

    Thanks for the response.  That took care of it.  The application is MVC using controllers as endpoints.  I'm following the example pretty closely, but will be more than happy to post the working code.  I do have some other questions that I'm sure you can help with.  Let me get it working and I'll post the code sometime tomorrow.

    Tuesday, December 18, 2018 3:07 AM