Tim Van Wassenhove

Passionate geek, interested in Technology. Proud father of two

28 May 2010

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) { }

and

// 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.");
}