c# – How to return a string from a Task<string> ?

Question:

I am performing an HttpRequest. It turns out that when returning the answer and showing it to work it, it shows me this:

System.Threading.Tasks.Task`1[System.String]

My code is:

using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace MLWPF.HttpRequest
{
    class Client
    {
        private static readonly HttpClient httpClient = new HttpClient();
        private FormUrlEncodedContent content;

        public int Id { get; set; }
        public int Uid { get; set; }
        public string Name { get; set; }
        public string Address { get; set; }
        public double Phone { get; set; }

        public Client()
        {

        }

        public Client(int id, int uid, string name, string address, double phone)
        {
            Id = id;
            Uid = uid;
            Name = name;
            Address = address;
            Phone = phone;
        }

        public async Task<string> ReadClients(int uid)
        {
            var values = new Dictionary<string, string>()
            {
                {"uid", uid.ToString()}
            };

            content = new FormUrlEncodedContent(values);
            HttpResponseMessage response = await httpClient.PostAsync(new UrlHttpRequest().UrlReadClients, content);
            string responseString =  await response.Content.ReadAsStringAsync();
            return responseString;
        }

    }
}

It turns out that if I show the console before returning the responseString , the information obtained is correctly displayed.

I am calling it from a method in the MainWindow.xaml.cs connected to a button.

private void FAB_Tab1_Button_Click(object sender, RoutedEventArgs e) 
{ 
    Client client = new Client(); 
    client.ReadClients(1); 
    Console.WriteLine(client.Name);
}

How can I get the information I need when I return?

Answer:

One option is to use the Task.FromResult<TResult> method, where you would only change return responseString; by return Task.FromResult<responseString>; , leaving your code as follows:

private static readonly HttpClient HttpClient = new HttpClient();
private FormUrlEncodedContent content;

public async Task<string> ReadClients(int uid)
{
    var values = new Dictionary<string, string>()
    {
        {"uid", uid.ToString()}
    };

    content = new FormUrlEncodedContent(values);
    HttpResponseMessage response = await httpClient.PostAsync(new UrlHttpRequest().UrlReadClients, content);
    string responseString =  await response.Content.ReadAsStringAsync();
    return Task.FromResult<responseString>;
}

O en su caso, cambiar la línea del HttpResponseMessag response = ... por HttpResponseMessage response = await Task.FromResult<string>(httpClient.PostAsync(new UrlHttpRequest().UrlReadClients, content));

private static readonly HttpClient HttpClient = new HttpClient();
private FormUrlEncodedContent content;

public async Task<string> ReadClients(int uid)
{
    var values = new Dictionary<string, string>()
    {
        {"uid", uid.ToString()}
    };

    content = new FormUrlEncodedContent(values);
    HttpResponseMessage response = await Task.FromResult<string>(httpClient.PostAsync(new UrlHttpRequest().UrlReadClients, content));
    string responseString =  await response.Content.ReadAsStringAsync();
    return responseString;
}
Scroll to Top