title: ASP.NET Web Api: Delegate after Request
body: <p>I have a problem with streams and the web api.</p>
  
  <p>I return the stream which is consumed by the web api. Currently, i put the socket into a pool after getting the stream. but this cause some errors.</p>
  
  <p>Now, I must putthe socket into the pool AFTER the request ended. (The stream was consumed and is now closed).</p>
  
  <p>Is there a delegate for this or some other best practises?</p>
  
  <p>Example code:</p>
  
  <pre><code>public HttpResponseMessage Get(int fileId)
  {
  HttpResponseMessage response = null;
  response = new HttpResponseMessage(HttpStatusCode.OK);
  
  Stream s = GetFile(id);
  
  response.Content = new StreamContent(fileStream);
  
  }
  
  GetFile(int id)
  {
  FSClient fs = GetFSClient();
  Stream s = fs.GetFileStream(id);
  AddFSToPool(fs);
  
  return s;
  }
  </code></pre>
  
  <p>GetFile uses a self-programmed FileServer-Client.
  It has an option to reuse FileServer-Connections. This connections will be stored in a pool. (In the pool are only unused FileServer-connections). If the next request calls GetFSClient() it gets an connected one from the pool (and removes it from the pool).</p>
  
  <p>But if another requests comes in and uses a FileServer-Connection which is in the pool (because unused), there is still the problem, that the Stream is possibly in use.</p>
  
  <p>Now I want to do the "put the FSClint into the pool" after the request ended and the stream is fully consumed.</p>
  
  <p>Is there an entry point for that?</p>