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;
        }
    }

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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<string>'
Hi recursive, I changed public async Task<string> 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<string> in SubmitClaims() even I already used it in RunAsync()?
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.