Support both GET and HEAD requests on the same method with WCF REST

A while ago i had to modify an existing WCF REST service which was being consumed by BITS. Apparently the implementation has changed in Windows7 in such a way that the BITS client first makes a HEAD request to discover the file size.

The following attempts did not work:

// A method can not have both WebGet and WebInvoke attributes
[OperationContract]
[WebGet]
[WebInvoke(Method="HEAD")]
public Stream Download(string token) { }


// A method can not have multiple WebInvoke attributes
[OperationContract]
[WebInvoke(Method="GET")]
[WebInvoke("HEAD")]
public Stream Download(string token) { }

The trick is to use * as Method and handle the method related logic in your code:

[OperationContract]
[WebInvoke(Method="*")]
public Stream Download(string token)
{
 var method = WebOperationContext.Current.IncomingRequest.Method;
 if (method == "HEAD") return ProcessHead();
 if (method == "GET") return ProcessGet();
 throw new ArgumentException(method + " is not supported.");
}

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>