RestCode / WebApiProxy

MIT License
199 stars 91 forks source link

ResponseType void invalid code generated #111

Closed silentsouls closed 8 years ago

silentsouls commented 8 years ago

When using the api below, some invalid code will be generated.

API method:

[ResponseType(typeof(void))]
public async Task<IHttpActionResult> PutUser(int id, UserModel model)
{
    // Do stuff 
    return StatusCode(HttpStatusCode.NoContent);
}

what is generated is:

public virtual void PutUser(Int32 id,UserModel model)
{
    var result = Task.Run(() => PutUserAsyncMsg(id, model)).Result;      
    EnsureSuccess(result);
    return result.Content.ReadAsAsync<void>().Result;
}

And that code will not build. Basically this line "return result.Content.ReadAsAsync().Result;" should not be generated. The result however should be validated, to ensure success "EnsureSuccess(result);"

faniereynders commented 8 years ago

Rather use ResponseType(typeof(HttpResponseMessage)) when actually returning something, like StatusCode(...) in your case

silentsouls commented 8 years ago

While that works for now, it still is generating code that does not build. And when crating a new service this is the way VS2015 generates the attribute.

faniereynders commented 8 years ago

Can you please elaborate on what code it generated that does not build now?

silentsouls commented 8 years ago
public virtual void PutUser(Int32 id,UserModel model)
{
    var result = Task.Run(() => PutUserAsyncMsg(id, model)).Result;      
    EnsureSuccess(result);
    return result.Content.ReadAsAsync<void>().Result;
}

.ReadAsAsync<void>(). is invalid c# code the generic void is not allowed

faniereynders commented 8 years ago

As a best practice, one should always return something in a RESTful service. In the case where there is no content, there's stil a header present.

Use HttpResponseMessage in this scenario