Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>That's an interesting question. I'll try to do my best to give some general pointers.</p> <p>Few things to consider:</p> <p><strong>1)</strong> Web API by default buffers requests so your fear that the memory footprint might be considerable is definitely justified. You can force Web API to work with requests in a streamed mode:</p> <pre><code> public class NoBufferPolicySelector : WebHostBufferPolicySelector { public override bool UseBufferedInputStream(object hostContext) { var context = hostContext as HttpContextBase; if (context != null) { if (string.Equals(context.Request.RequestContext.RouteData.Values["controller"].ToString(), "uploading", StringComparison.InvariantCultureIgnoreCase)) return false; } return true; } public override bool UseBufferedOutputStream(HttpResponseMessage response) { return base.UseBufferedOutputStream(response); } } </code></pre> <p>And then replace the service:</p> <pre><code>GlobalConfiguration.Configuration.Services.Replace(typeof(IHostBufferPolicySelector), new NoBufferPolicySelector()); </code></pre> <p>Please note that due to differences between WebHost and SelfHost at this point, such change is only possible in WebHost. If your endpoint is selfHosted, you would have to set the streaming mode at the GlobalConfig level:</p> <pre><code>//requests only selfHostConf.TransferMode = TransferMode.StreamedRequest; //responses only selfHostConf.TransferMode = TransferMode.StreamedResponse; //both selfHostConf.TransferMode = TransferMode.Streamed; </code></pre> <p>I have blogged about dealing with large files in Web API in more details before - <a href="http://www.strathweb.com/2012/09/dealing-with-large-files-in-asp-net-web-api/" rel="noreferrer">http://www.strathweb.com/2012/09/dealing-with-large-files-in-asp-net-web-api/</a> so hopefully you'll find that useful.</p> <p><strong>2)</strong> Secondly, if you use <code>HttpClient</code>, in .NET 4 it buffers the requests body by default, so you should really use .NEt 4.5. </p> <p>If you have to use .NET 4 you have to work with <code>HttWebRequest</code> directly: - <a href="http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.allowreadstreambuffering.aspx" rel="noreferrer">http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.allowreadstreambuffering.aspx</a> - <a href="http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.allowwritestreambuffering.aspx" rel="noreferrer">http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.allowwritestreambuffering.aspx</a></p> <p><strong>3)</strong> As far as pushing the data to the client that's definitely possible if you want to do that, with <code>PushStreamContent</code>. Henrik has a short introductory post here - <a href="http://blogs.msdn.com/b/henrikn/archive/2012/04/23/using-cookies-with-asp-net-web-api.aspx" rel="noreferrer">http://blogs.msdn.com/b/henrikn/archive/2012/04/23/using-cookies-with-asp-net-web-api.aspx</a> (it's based on Web API RC bits so you might need to adjust some signatures etc.) I also blogged about pushing chunks of stream data here - <a href="http://www.strathweb.com/2013/01/asynchronously-streaming-video-with-asp-net-web-api/" rel="noreferrer">http://www.strathweb.com/2013/01/asynchronously-streaming-video-with-asp-net-web-api/</a></p> <p><strong>EDIT:</strong> To see an example if <code>PushStreamContent</code> in the request, you can have a look at this sample solution - <a href="http://aspnet.codeplex.com/SourceControl/changeset/view/bb167f0b0013#Samples/Net45/CS/WebApi/UploadXDocumentSample/ReadMe.txt" rel="noreferrer">http://aspnet.codeplex.com/SourceControl/changeset/view/bb167f0b0013#Samples/Net45/CS/WebApi/UploadXDocumentSample/ReadMe.txt</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload