1

In one controller (MVC ASP.NET) I need to download some web pages, and analyze them. Currently I'm using Visual Studio 2019, and .NET Framework 4.8.

My code is like this (simplified):

public async void GetHtmlStream(Uri urlAddr)
{
    HttpClient client = new HttpClient();
    using (HttpResponseMessage Resp = client.GetAsync(urlAddr).Result)
    {
        using (HttpContent content = Resp.Content)
        {
            Stream stream = await content.ReadAsStreamAsync().Result;                
        }
    }
}

The line Stream stream = await content.ReadAsStreamAsync().Result; does not compile: "Error CS1061 'Stream' does not contain definition for 'GetAwaiter' ..."

In a test program, avoiding async and await, i have a line like this: string result = content.ReadAsStringAsync().Result; and all works fine. In the controller, of course nothing works.

I saw a lot of similar issues but I don't understand what should I do to solve the problem.

Peter B
  • 22,460
  • 5
  • 32
  • 69
Giorgio Forti
  • 131
  • 2
  • 14

1 Answers1

1

Try

Stream stream = await content.ReadAsStreamAsync();

instead.

xavier
  • 1,860
  • 4
  • 18
  • 46
  • A little better: now the compiler tells me "Error CS1061 'Stream' does not contain a definition for 'Result' ..." in streamTask.Result. – Giorgio Forti Jan 11 '21 at 16:19
  • I think the `ReasAsStreamAsync` already returns a Stream. You don't need the result. See my updated answer. – xavier Jan 11 '21 at 16:21
  • 1
    With the updatewd answer I compile. Now I'm testing. – Giorgio Forti Jan 11 '21 at 16:24
  • Ok, now i compile, but nothing works. Now the problem is different: running i have an exception here: Resp = client.GetAsync(urlAddr).Result I'm tryin' to simplify and isolate the problem. – Giorgio Forti Jan 11 '21 at 16:52
  • Well, that's already a different question. The `GetAwaiter` error was because you were trying to await on something that was not a task. Now you got the Stream you were looking for. Now: perhaps this is not the way you should use it, but I don't know it. I know the "await" problems, not the stream ones. What do you do with the stream? Have to tried to debug it? Have you tried to use a string instead of a stream? I don't know, there could be multiple issues... – xavier Jan 11 '21 at 16:56
  • What do you want to do with the Stream? Do you want to read it? copy in in a file...? If you want to copy it in a file, for example, you could do the following after the `ReasAsStreamAsync`, inside the `using`: `Stream fileStream = File.Open(filePath, FileMode.Create);` and then `await stream.CopyToAsync(fileStream);`, for example... – xavier Jan 11 '21 at 17:01
  • I have the old versions of the pages i'm tryin to download, and i want to compare to see if something is changed ... but i DON'T have the stream ... running the code stops before the stream. This damn thing is driving me crazy, there are hours i'm fightin', without nothing in my hands. – Giorgio Forti Jan 11 '21 at 17:07
  • Well, if the code stops before getting the stream, then the problem is probably somewhere else...? :-( – xavier Jan 11 '21 at 17:11
  • Of course. The problem you solved was a compiling problem. Now i compile but i can't execute. Tomorrow I will simplify the code and then repost the new code, maybe in a new question (the problem in this question is solved, thank you), asking a different help. I will place here a link to the new question. – Giorgio Forti Jan 11 '21 at 22:07
  • Great, I'll have a look at it then! – xavier Jan 11 '21 at 22:29
  • 1
    In truth, the problem is completely solved: I had not changed packages.config (my fault), thus the application still used an old package, of course not working! Updating the packages.config finally it WORKS! – Giorgio Forti Jan 13 '21 at 07:02