0

I am facing an issue with two different endpoints in my single asp.net app. Basically, the issue is that one of the endpoints does not allow asynchronous methods in the page and the other endpoint does. If I run the app one endpoint will ask me to have an asynchronous asp.net page, but the other one crashes and vice versa.

public async Task<AirtableListRecordsResponse> RetrieveRecord()
    {
        string MyProductID = ProductID;
        string baseId = "00000000000xxxx";
        string appKey = "00000000000xxxx";
        var records = new List<AirtableRecord>();
        using (AirtableBase airtableBase = new AirtableBase(appKey, baseId))
        {
            Task<AirtableListRecordsResponse> task = airtableBase.ListRecords(tableName: "efls", filterByFormula: ProductID);


            AirtableListRecordsResponse response = await task;
            if (!response.Success)
            {
                string errorMessage = null;
                if (response.AirtableApiError is AirtableApiException)
                {
                    errorMessage = response.AirtableApiError.ErrorMessage;
                }
                else
                {
                    errorMessage = "Unknown error";
                }
                // Report errorMessage
            }
            else
            {

                records.AddRange(response.Records.ToList());
                var record = response.Records;
                //offset = response.Offset;

                //var record = response.Record;
                foreach (var item in record)
                {
                    foreach (var Fields in item.Fields)
                    {
                        if (Fields.Key == "pdfUrl")
                        {
                            string link = Fields.Value.ToString();
                            MyLink = Fields.Value.ToString();
                        }

                    }
                }
                // Do something with your retrieved record.
                // Such as getting the attachmentList of the record if you
                // know the Attachment field name
                //var attachmentList = response.Record.GetAttachmentField(YOUR_ATTACHMENT_FIELD_NAME);
            }
            return response;
        }
    }

This is the asynchronous method which asks for an asynchronous page, the other contains a strong structure and it cannot be changed for any reason. Is there any way to make them work together?

I am using airtable.com api by the way.

Thanks in advance.

  • `airtableBase.ListRecords(tableName: "efls", filterByFormula: ProductID).Result;` Try this. Using it, you will not have to use async method. – soccer7 Oct 16 '18 at 18:13
  • Something weird happened, when I debug the method it simply does nothing, it seems it stays in a loop and it does nothing at all. – Lanshore Dev Oct 16 '18 at 19:47
  • Why can't you mark both pages as asynchronous? – mason Oct 16 '18 at 20:46
  • Possible duplicate of [Calling async method synchronously](https://stackoverflow.com/questions/22628087/calling-async-method-synchronously) – soccer7 Nov 28 '18 at 22:41

2 Answers2

0

I solved by my own,

The solution I found is the following:

When a page works with two different endpoints and one of them obligates the page to be asynchronous the best solution is to split the procedures into two different sections and/or pages, one of them will call the asynchronous methods and retrieves the info and other works without being asynchronous.

How can I pass the information between the sites?

Using session variables, there are endpoints which only needs to display simple data as in this case, so the session variables will be called in the page #2 which is the non-asynchronous page.

It is a simple solution but effective.

Thank you very much to all for you answers.

-2

Using Wait on Task, you can use synchronous method

Task<AirtableListRecordsResponse> task = Task.Run(() => airtableBase.ListRecords(tableName: "efls", filterByFormula: ProductID)); 
task.Wait();
AirtableListRecordsResponse response = task.Result;

Use it only when you cannot use async method.

This method is completely deadlock free as mentioned on msdn blog- https://blogs.msdn.microsoft.com/jpsanders/2017/08/28/asp-net-do-not-use-task-result-in-main-context/

soccer7
  • 3,547
  • 3
  • 29
  • 50
  • You need to read [Async/Await - Best Practices in Asynchronous Programming](https://msdn.microsoft.com/en-us/magazine/jj991977.aspx). This code can lead to deadlocks that are hard to debug. If you're not familiar with asynchronous coding, please be very cautious before trying to help others with it. – mason Oct 16 '18 at 20:47
  • 1
    dealocks occur when we use `.Result` directly without Wait(). Above approach is completely deadlock free and it is recommended by msdn as well. – soccer7 Oct 16 '18 at 20:52