I'm putting together a document-management service using ASP.NET Web API, and performance is of some concern.
Based on this article by Nathanael Jones, author of the IIS / ASP.NET ImageResizer module, I've developed a few preconceptions on what's necessary for "optimal" performance of serving static files. The precis of that being:
HttpModule
because it's very early in the ASP.NET pipeline (less pipeline = more optimised) and turns out to be easier to deal with this sort of thing than a HttpHandler
.Response.WriteFile(filename)
is better than Response.Write(memBuffer)
where memBuffer is an in-memory C# buffer.Context.RewritePath(virtualPath)
) is better than Response.WriteFile(filename)
, because it will use the IIS static file handler, which is well optimised.Trouble is, thanks to some bizarre caching issue I still can't get to the bottom of (here), my URL rewrite technique isn't behaving itself.
So now I'm left wondering about a more Web API-centric implementation like the following:
public Task<HttpResponseMessage> DoTheFoo()
{
return Task<HttpResponseMessage>.Factory.StartNew(() =>
{
var response = new HttpResponseMessage();
response.Headers.Add("Content-Disposition", "inline; filename=\"" + attachmentFileName + "\"");
response.Headers.Add("content-type", mimeType);
response.Content = new StreamContent(File.OpenRead("somefile.doc"));
return response;
});
}
For a Web API solution this obviously makes things a bit neater, since the file-serving portions of the service can go right alongside the other actions in controllers, but how well is it going to perform and scale? The factors I ponder:
I'm not in a position to put together a server-farm to test this to get real performance numbers, and I have my doubts that factoring up small-scale test results will give anything accurate. So from those in the know, what sort of differences could I expect? I'm almost ready to abandon the URL rewrite implementation, but if it's going to give me a noticeable performance hit, I'll persevere.
Not sure if you are planning to host in Azure, but if you are you can use a dedicated public blob storage container for your images. Then use the CDN service to serve (and cache) the static files for you. This will move the problem off your API and into a CDN, which is designed to handle static resources.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With