Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# How to await a method that returns Task<dynamic>?

I have a method that is parsing the results from a JSON object. The method returns a Task object but when I run the code, I am getting the following error:

'System.Collections.Generic.Dictionary<string,object>' does not contain a definition for 'GetAwaiter'

The object returned from the dynamic method is an array of objects of type System.Collections.Generic.Dictionary and System.Collections.Generic.KeyValuePair. Here is the code:

private static async Task<dynamic> GetReslutstAsync(string url)
    {
        WebRequest request;
        WebResponse response = null;
        try
        {
            request = WebRequest.Create(url);
            request.Credentials = new NetworkCredential("username", "password", "company");
            //request.Credentials = CredentialCache.DefaultNetworkCredentials;
            response = await request.GetResponseAsync();
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }

        using (var reader = new StreamReader(response.GetResponseStream()))
        {
            try
            {
                JavaScriptSerializer js = new JavaScriptSerializer();
                var objects = js.Deserialize<dynamic>(reader.ReadToEnd());
                return objects;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return null;
            }
        }
    }

The exception occurs here:

private static async Task<dynamic> MakeRequest(string url)
    {
        try
        {
            return await GetReslutstAsync(url).Result;  //<---- This is where I get the error!
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
            return null;
        }
    }

I believe this has something to do with the fact that I am returning a dynamic type object. How can I get around this? I need to await this task inside the "MakeRequest" method so that it does not continue until the request is completed.

EDIT:

My "MakeRequest" method is in a loop like this:

while (true)
        {
            ClockTimer timer = new ClockTimer();
            timer.StartTimer(); //<---This does not stop untill 5 sec has passed
            MakeRequest("www.someurl.com"); //<--- This just skips into the next loop even if not complete.
        }

My issue is that MakeRequest is being run asynchronously so basically, it just skips over this and goes right into the next loop. I need "MakeRequest" to HALT until the request is finished. I have tried removing all async/await keywords but this just results in "Result was not calculated".

like image 454
Hooplator15 Avatar asked May 16 '16 17:05

Hooplator15


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


1 Answers

This is what you need:

return await GetReslutstAsync(url);

You are not awaiting on the dynamic, only the task that returns a dynamic.

like image 80
n8wrl Avatar answered Sep 18 '22 01:09

n8wrl