Web API controller with async/await in Sitecore 8

So you want to create Web API controllers in a Sitecore 8 solution? Use Sitecore.Services.Infrastructure.Web.Http.ServicesApiController and add Sitecore.Services.Core.ServicesControllerAttribute to namespace your controller. Read Developer’s Guide to
Sitecore.Services.Client (PDF)
from page 61 and onward. Controllers will be reachable at:

/sitecore/api/ssc/{namespace}/{controller}/{id}/{action}

That’s it. No routing setup, no pipeline fiddling. Quick and easy! Just note that Sitecore does not support dependency injection in the controller – at least out of the box.

Below is a full example that shows async/await in a Web API controller – note the aspnet:UseTaskFriendlySynchronizationContext appSetting in Web.config.

You might also want to check my post on disabling of Sitecore Web API filters.

Resize media image stream in Sitecore

When working with MediaItems in Sitecore you might want to direct access to a stream behind the media item. On the a Sitecore.Data.Items.MediaItem you can call GetMediaStream() to accomplish that. It will return a stream to the raw data which might not be ideal if you are working with images. Often you would want a resized image, and fortunately you can use the Sitecore.Resources.Media.ResizeProcessor pipeline processor.