Fabien
Fabien

Reputation: 1085

Return Web API headers earlier

Is there a way, when using ASP.NET Web API, to return the response headers earlier?

An example: Let's say I have an action in my controller which returns all companies for a filter

    // GET api/companies/filter
    public Companies Get(string someFilter)
    {
        // some long operation (10 seconds)
    }

I would like to return the headers ASAP and while doing that, the long operation should take place, and then return the data of the long operation.

Is something like this possible?

Upvotes: 0

Views: 115

Answers (1)

Darrel Miller
Darrel Miller

Reputation: 142014

You need to use PushStreamContent to do this

// GET api/companies/filter
public HttpResponseMessage Get(string someFilter)
{
    // some long operation (10 seconds)

    var pushContent = new PushStreamContent( (stream, content, ctx) =>
        {
            // Do long running thing here, writing to stream
        });


   return new HttpResponseMessage() {
      Content = pushContent
   }
}

Upvotes: 1

Related Questions