0

I am working on a MVC project that submits a request via a third party.

In my controller, I have a SubmitClaims() action that receive ajax request and then calls RunAsync(). RunAsync submits a request by using HttpClient. I am not sure if I did a right thing here.

Also I have two version of SubmitClaims(), both work. But I don't know which version is better.

version 1

    [HttpPost]
    public async Task<string> SubmitClaims()
    {
        string result = "";
        result = await RunAsync();
        return result;
    }

version 2 learn from Cannot implicitly convert type 'string' to 'System.Threading.Tasks.Task<string>'

    [HttpPost]
    public async Task<string> SubmitClaims()
    {
        return await Task.Run(() =>
        {
            return RunAsync();
        });
    }


    static async Task<string> RunAsync()
    {
        string result = "Failed.";
        using (var client = new HttpClient())
        {
            try
            {
                client.BaseAddress = new Uri("http://peter:8001/internal/uickpost");
                client.DefaultRequestHeaders.Add("contenttype", "application/xml");
                client.DefaultRequestHeaders.Add("hiconline.protocol.content.role", "REQUEST");
                client.DefaultRequestHeaders.Add("hiconline.protocol.content.transactionid", "asdfsdf");
                client.DefaultRequestHeaders.Add("hiconline.protocol.remote.contenttype", "TestDataType");
                client.DefaultRequestHeaders.Add("hiconline.protocol.remote.mode", "P");
                client.DefaultRequestHeaders.Host = "peter:8001";
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/plain"));

                string opv = "Test Data";

                HttpContent _content = new StringContent(opv);

                _content.Headers.ContentType = new MediaTypeHeaderValue("application/xml");
                _content.Headers.Add("contenttype", "TestDataType");

                HttpResponseMessage response1 = await client.PostAsync(client.BaseAddress, _content);


                if (response1.IsSuccessStatusCode)
                {
                    Uri gizmoUrl = response1.Headers.Location;
                    result = response1.Content.ReadAsStringAsync().Result;
                }
            }
            catch (Exception ex)
            {
        result = ex.Message;
            }
            return result;
        }
    }
Community
  • 1
  • 1
peter
  • 1,009
  • 3
  • 15
  • 23

1 Answers1

4

Option 1 is better. RunAsync() already returns a task, so why create another one?

Even better would be return await RunAsync();. Even better would just be calling RunAsync directly, since the wrapper doesn't add anything.

recursive
  • 83,943
  • 34
  • 151
  • 241
  • Hi recursive, Thank you for help. Just return RunAsync(); causes an error: Since this is an async method, the return expression must be of type 'string' rather than 'Task' – peter Dec 17 '14 at 22:43
  • Hi recursive, I changed public async Task SubmitClaims() to public string SubmitClaims() {return RunAsync().Result;} compiling is not a problem, but result is [object object] after stopping debugging. Why I have to use async Task in SubmitClaims() even I already used it in RunAsync()? – peter Dec 17 '14 at 22:57
  • You have to use `Task<>` in the return type because you are using `await` in the body of the method. If you await, you must return a task. – recursive Dec 17 '14 at 23:20