WCF Global(.asax) Behavior

Posted by Theofanis Pantelides on Stack Overflow See other posts from Stack Overflow or by Theofanis Pantelides
Published on 2010-05-25T10:01:15Z Indexed on 2010/05/25 10:21 UTC
Read the original article Hit count: 675

Filed under:
|
|

Hi,

I want to create a global option that when a REST call contains &format=json to output the response as a JSON string.

If I enter the following String in my method it works:

WebOperationContext.Current.OutgoingResponse.Format = WebMessageFormat.Json;

However, if I add this line, anywhere in my Global.asax file, I get a nullException for Current Context:

String format = "";

if (HttpContext.Current.Request.QueryString["format"] != null)
  format = HttpContext.Current.Request.QueryString["format"];

if (String.Equals("json", format, StringComparison.OrdinalIgnoreCase))
  System.ServiceModel.Web.WebOperationContext.Current.OutgoingResponse.Format = System.ServiceModel.Web.WebMessageFormat.Json;

The exception is triggered here:

System.ServiceModel.Web.WebOperationContext.Current

Anyone know how I can add this functionality globally (WCF)?

© Stack Overflow or respective owner

Related posts about wcf

Related posts about rest