Rest – Using ASP.NET Web API as image service

asp.netasp.net-web-apiimagerestweb services

I have an application running on both web and mobile platforms and its users can upload photos using the app. I need a web service to handle uploading and displaying images on both web and mobile applications.

Is it a good idea to use ASP.NET Web API as an image service and make POST requests on it for uploading photos? What are some pros and cons of this approach? Is using ASP.NET Web API overhead for a service like this?

Best Answer

ASP.NET Web API works great with async I/O -- it uses the new Task-based async model in .NET 4 (and particular .Net 4.5) which makes it straight forward to build async controllers and coordinate multiple asynchronous operations. If this model is new to you then I would recommend this presentation on channel9.

For uploads, ASP.NET Web API supports completely async MIME multipart file upload based on the common HTML file upload model from a form. You can see an example of this in the FileUploadSample available as source.

It is also straight forward to serve files asynchronously using the StreamContent. Here you (in the controller) open a file, create an HttpResponseMessage and attach an HttpContent in the form of a StreamContent wrapping the file and that's it.

This all happens without blocking any threads on I/O.

Hope this helps,

Henrik