3

I'm a bit confused with HttpResponseMessage and Task<HttpResponseMessage>.

If I'm using the HttpClient method PostAsync() to post data I need to give the Web Service method the Task<HttpResponseMessage> instead of HttpResponseMessage as return value as far as I understood things.

If I use Request.CreateResponse(HttpStatusCode.Forbidden, myError.ToString()); then I'm only getting the Response message object but not the Task object.

So my question here is how do I have to create the Fitting return for async calls to web api methods? (thus are my understandings there correct and if so how to best transform the message object int a Task<HttpResponseMessage> object)

The original code:

public HttpResponseMessage DeviceLogin(MyDevice device)
{
    EnummyError myError = EnummyError.None;

    // Authenticate Device.
    myError = this.Authenticate(device);

    if (myError != EnummyError.None)
    {
        return Request.CreateResponse(HttpStatusCode.Forbidden, myError.ToString());
    }
}

The updated Method header:

public Task<HttpResponseMessage> DeviceLogin(MyDevice device)
3
  • I’m not following your question. Do you have some code to show that would explain the issue? You don’t really do anything with tasks yourself, the framework handles that and you just return the type you’d return anyway. Commented Dec 1, 2017 at 8:37
  • Are you using Web Api version 1 or 2? Commented Dec 1, 2017 at 8:40
  • @Baksteen visual Studio 2017 so v2 updating in a second with example code Commented Dec 1, 2017 at 8:41

1 Answer 1

3

Web Api 2 has these abstraction classes which are now recommended to use. You can still use HttpResponseMessage (easier to follow for beginners, in my opinion), but Web Api 2 recommends using IHttpActionResult.

As for the return type, just did what you did before. Task<T> works automagically that way.

You also might want to check if this.Authenticate() has an async variant.

public async Task<IHttpActionResult> DeviceLogin(MyDevice device)
{
    EnummyError myError = EnummyError.None;

    // Authenticate Device.
    myError = this.Authenticate(device);

    // Perhaps Authenticate has an async method like this.
    // myError = await this.AuthenticateAsync(device);


    if (myError != EnummyError.None)
    {
        return ResponseMessage(Request.CreateResponse(Request.CreateResponse(HttpStatusCode.Forbidden, myError.ToString()));
    }
}

The ResponseMessage() method creates a ResponseMessageResult under water. This class derives from IHttpActionResult and accepts a HttpResponseMessage as a parameter in the constructor (which is made by Request.CreateResponse()).

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

5 Comments

Does Authenticate NEED an async method?
hmmm Compiler says ResponseMessage can't be converted into Task<IHttpActionResult>
works that way. question still though: do I have to use await inside? or is it a "you better should",... ?
Ah! My bad. I forgot to add async to the method declaration. I've updated my answer. As for Authenticate, no it does not. But if you don't use any await operators for async method calls, there is no use of making this method asynchronous, as it would just run synchronously
Will have to think it over and possibly redesign / make a new question there, but this current question is solved so thnx.

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.