This is a mirror of official site: http://jasper-net.blogspot.com/

Allowing Access to HttpContext in WCF REST Services

| Tuesday, January 11, 2011
If you’re building WCF REST Services you may find that WCF’s OperationContext, which provides some amount of access to Http headers on inbound and outbound messages, is pretty limited in that it doesn’t provide access to everything and sometimes in a not so convenient manner. For example accessing query string parameters explicitly is pretty painful:

[OperationContract]
[WebGet]
public string HelloWorld()
{
   var properties = OperationContext.Current.IncomingMessageProperties;
   var property = properties[HttpRequestMessageProperty.Name] as HttpRequestMessageProperty;
   string queryString = property.QueryString;
   var name = StringUtils.GetUrlEncodedKey(queryString,"Name");

   return "Hello World " + name;
}
And that doesn’t account for the logic in GetUrlEncodedKey to retrieve the querystring value.

It’s a heck of a lot easier to just do this:

[OperationContract]
[WebGet]
public string HelloWorld()
{
   var name = HttpContext.Current.Request.QueryString["Name"] ?? string.Empty;
   return "Hello World " + name;      
}

Ok, so if you follow the REST guidelines for WCF REST you shouldn’t have to rely on reading query string parameters manually but instead rely on routing logic, but you know what: WCF REST is a PITA anyway and anything to make things a little easier is welcome.

Read more: Rick Strahl's blog

Posted via email from .NET Info

0 comments: