-4

My update Update.Check() method throws this error: 'string' does not contain a definition for 'Get'.

Googled for hours, but couldn't figure it out.

using System;
using System.Net.Http;
using System.Threading.Tasks;

namespace MyTestProgram
{
    public class Update
    {
        public static void Check()
        {
            string result = Task.FromResult(string.Get.Result);
            Console.WriteLine(result);
        }
        private static async Task<string> Get()
        {
            var client = new HttpClient();
            return await client.GetStringAsync("http://localhost/?update=val");
        }
    }
}

Using .NET Framework 4.5

Alex G
  • 3,048
  • 10
  • 39
  • 78
  • 3
    It refers to this part: `(string.Get.Result)` – Christopher May 15 '20 at 03:32
  • The problem is exactly what the error message says. What's the question? – EJoshuaS - Stand with Ukraine May 15 '20 at 03:36
  • Also, please don't use HttpClient like that in production code - you could run into port exhaustion problems. – EJoshuaS - Stand with Ukraine May 15 '20 at 03:37
  • @EJoshuaS-ReinstateMonica: do you mean that HttpClient should only be "instantiated once per application" as well as try{} catch{}? – Alex G May 15 '20 at 03:42
  • @AlexG Nope, not only one instace. But either a try finally or a using, to actually dispose of it after use. Networking stuff on principle contains unamanged resources. – Christopher May 15 '20 at 03:44
  • Not according to this answer https://stackoverflow.com/questions/15705092/do-httpclient-and-httpclienthandler-have-to-be-disposed-between-requests – Alex G May 15 '20 at 03:47
  • @Christopher No, you shouldn't dispose of HttpClient instance, you should use a single instance (except in ASP.NET Core, which has a factory for that purpose). Even disposing of it doesn't completely protect you from port exhaustion. – EJoshuaS - Stand with Ukraine May 15 '20 at 03:57
  • @Christopher Please see [this article](https://visualstudiomagazine.com/blogs/tool-tracker/2019/09/using-http.aspx) as well as [this very thorough Microsoft-endorsed blog post](https://aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/) on why you should never use `HttpClient` in a `using` block. The short answer is that calling `Dispose()` will *not* actually clean up all of its unmanaged resources (see the blog post for more details on that point), so if you're using `HttpClient` heavily even a `using` block won't prevent port exhaustion. – EJoshuaS - Stand with Ukraine May 15 '20 at 13:56

1 Answers1

1

This is the line:

(string.Get.Result)

string has no property called Get. It has a bunch of functions starting with Get, but not a pure get. I honestly I have no idea why you wrote the string type in that place.

My best guess is that you wanted (Get().Result)? Task<T> has a Result property and Get() returns a Task<string>. The closest thing to something sensible.

Task.FromResult() is a generic function. So you would have to call it with Task.FromResult<string>(). But that would mean you take a Task<string>, extract it's result, then put that result back into another, unrelated Task<string>.

Wich brings me to maybe this being the intention:

string result = Get().Result;
Christopher
  • 9,634
  • 2
  • 17
  • 31