Upload a file asynchronously I need your opinion about my practice. I want to improve it. 

The requirements are the following:


RESTFUL + OWIN (SelfHosted)
Endpoint - Upload a file asynchronously
Make a request to the endpoint from Postman


Well, This is what I got

For the OWIN selfhosted I followed the Microsoft Documentation

OWIN Self-host

Endpoint:

FileController.cs

    [HttpPost]
    [Route("api/v1/upload")]
    public HttpResponseMessage Post([FromUri]string filename)
    {
        var task = this.Request.Content.ReadAsStreamAsync();
        task.Wait();
        Stream requestStream = task.Result;

        try
        {
            Stream fileStream = File.Create("./" + filename);
            requestStream.CopyTo(fileStream);
            fileStream.Close();
            requestStream.Close();
        }
        catch (IOException)
        {
            throw new HttpResponseException( HttpStatusCode.InternalServerError);
        }

        HttpResponseMessage response = new HttpResponseMessage();
        response.StatusCode = HttpStatusCode.Created;
        return response;
    }


Postman:

The postman returns Status Code 201
Seems a bit needlessly complicated and not exactly async at all. You're just forcing an async call to be synchronous with .Wait(). Proper way is to use async "all the way down":

[HttpPost]
[Route("api/v1/upload")]
public async Task&lt;HttpResponseMessage&gt; Post([FromUri]string filename)
{
    try
    {
        using (Stream requestStream = await this.Request.Content.ReadAsStreamAsync())
        using (Stream fileStream = File.Create("./" + filename))
        {
            await requestStream.CopyToAsync(fileStream);
        }

        return new HttpResponseMessage { StatusCode = HttpStatusCode.Created };
    }
    catch (IOException)
    {
        throw new HttpResponseException(HttpStatusCode.InternalServerError);
    }
}