WCF, .NET 3.5 and HTTP Request Content

The new Web Programming Model of WCF and the .NET Framework brings some very useful features such as the WebGet and the WebInvoke Attribute and the capability to deal directly with HTTP requests. But how do we access the HTTP request in these methods? Dealing with HTTP requests and responses using the System.Net namespace is quite straight forward. Therefore, we have a look in sending and receiving a simple HTTP request:

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(myUrl);byte[] a_dataBytes = Encoding.UTF8.GetBytes(data);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = a_dataBytes.Length;

Stream requestStream = request.GetRequestStream();
requestStream.Write(a_dataBytes, 0, a_dataBytes.Length);
WebResponse response = request.GetResponse(); StreamReader responseReader = new StreamReader(response.GetResponseStream(), Encoding.UTF8); var content = responseReader.ReadToEnd();

Now we want to access the HTTP context within a operation with the following signature:

[OperationContract]
[WebInvoke(Method = "PUT", UriTemplate = "{uri}")]
bool Put(string uri);

Accessing the context of the incoming request is straightforward using the WebOperationContext class.

IncomingWebRequestContext context =
WebOperationContext.Current.IncomingRequest;

var length = context.ContentLength;
var type = context.ContentType;
var headers = context.Headers;

Since the documentation does not provide many example code, and the ORCAS samples do not cover this yet, I had to figure out how to access the content of the incoming request. After spending hours on MSN Search, Google and the MSDN Library I finally tried out something. In some general examples, either a Message or a Stream is passed over as single parameter to the operation. Hence, I changed the contract as follows:

[OperationContract]
[WebInvoke(Method = "PUT", UriTemplate = "{uri}")]
bool Put(string uri, Stream stream);

Reading the content then is easy:

var reader = new StreamReader(stream);string content = reader.ReadToEnd();

This solution however comes up with one major drawback: If you try to access your foo.svc via browser you will end up some error telling you “For request in operation Post to be a stream the operation must have a single parameter whose type is Stream.”. Also calling foo.svc?wsdl will end up in some exception in the WSDL export extension.

Leave Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.