-1

I am trying to send a string from my C# app to my php server (first time using async). When I try to write out my response to the console, I just get this: System.Threading.Tasks.Task'1[System.String]

C# Code

private HttpClient request;
public async Task<string> licenseCheck(HttpClient client, string email){
var payload = new Dictionary<string, string>
{
    { "email", email }
};

var content = new FormUrlEncodedContent(payload);           
var response = await client.PostAsync("https://example.io/checkin.php", content);

return await response.Content.ReadAsStringAsync();
}

request = new HttpClient();
Console.WriteLine(licenseCheck(request,"joe@example.com").ToString());

PHP Code - checkin.php

<?php
    $email = trim(strtolower($_POST['email']));
    header('Content-Type: application/x-www-form-urlencoded');
    echo $email;
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Shawn Cooke
  • 907
  • 1
  • 11
  • 28
  • Why do you await `ReadAsStringAsync` but you don't await `licenseCheck`? In short: you're not waiting for the request to finish, and you're calling `.ToString()` on the `Task` object. – ProgrammingLlama Apr 20 '19 at 03:53
  • As I said, I am new to `async` and having a hard time understanding it. Most of this code is from another SO post. – Shawn Cooke Apr 20 '19 at 03:54
  • Well, to get the result from an `async` method, you have to `await` it, otherwise you get the `Task`. `await` will automatically unwrap the result, and unbox errors, etc. for you. – ProgrammingLlama Apr 20 '19 at 03:55
  • 1
    See [this question](https://stackoverflow.com/questions/40291526/why-async-function-returning-system-threading-tasks-task1system-string), but bear in mind that you really use async/await all the way down from the top (to an async method call) if you can. Note that as of C#7, you can have [async Task Main](https://stackoverflow.com/questions/46219114/what-is-the-point-of-having-async-main) as the entrypoint to applications. I recommend [Stephen Cleary's blog](https://blog.stephencleary.com/2012/02/async-and-await.html) (and numerous [SO] answers) if you want to learn async/await. – ProgrammingLlama Apr 20 '19 at 03:57

1 Answers1

1

The object that you're calling ToString() on in the last line is the Task that performs the license check. You should be awaiting the call to licenseCheck, or using the Task.Result property to synchronously wait on the task and get the result if your request is running synchronously:

// This allows the runtime to use this thread to do other work while it waits for the license check to finish, when it will then resume running your code
Console.WriteLine(await licenseCheck(request,"joe@example.com"));
// This causes the thread to twiddle its thumbs and wait until the license check finishes, then continue
Console.WriteLine(licenseCheck(request,"joe@example.com").Result);

Also consider using HttpClientFactory if you're running on .NET Core:

https://learn.microsoft.com/en-us/dotnet/standard/microservices-architecture/implement-resilient-applications/use-httpclientfactory-to-implement-resilient-http-requests

James
  • 119
  • 4
  • Just changing my `Console.WriteLine` by adding await seems so trivial. Would my function change at all? I just tried `Console.WriteLine(await licenseCheck(request,"joe@example.com"));` and ended up with a bunch of other errors. I think the issue is in my function. Been working on this for about 9 hours :-) – Shawn Cooke Apr 20 '19 at 04:02
  • 2
    You can only use the await keyword inside an async function. If you're trying to do this from your Main() method, then you can only declare an async Main if you're using c# 8 or above. Check the configuration of your project. If for some reason the function that calls licenseCheck *cannot* be async, then you need to use Task.Result – James Apr 20 '19 at 04:04
  • @James You can do it in C# 7. – ProgrammingLlama Apr 20 '19 at 04:09
  • 1
    My mistake; I usually remember it being "the latest version of C#, which VS doesn't choose by default." Now that the latest version is 8.0 I got mixed up. Seems the feature was actually introduced in C# 7.1 – James Apr 20 '19 at 04:16