locked
Is using WebOperationContext supported in WCF Data Services ? RRS feed

  • Question

  • Is using WebOperationContext supported in WCF Data Services ( ON THE SERVER SIDE : SERVICE OPERATIONS) ?

    I am using the follow code to return back HTTP response codes to conform to OData protocol.  This code lives inside a Service Operation, decorated with a WebInvoke .

     

     var ctx = WebOperationContext.Current;
      var rootUri =
       
      if (ctx != null)
      {   
       ctx.OutgoingResponse.SetStatusAsCreated(
       new Uri(myUri));
      }
    

     

    While, this does set the Location header correctly in the response, it does NOT return a 201 like I want, it returns back 204 instead.  I have the same thing happen when I explicitly set the response like so:

     

      var ctx = WebOperationContext.Current;
      if (ctx != null) ctx.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.Created;
    

     

    Any ideas ?

     

    • Edited by Rybolt Wednesday, December 22, 2010 3:13 PM clarification
    Tuesday, December 21, 2010 7:23 PM

Answers

  • For others that are curious.  In Service Operations you can not control the response code as mentioned previously.

    200 for non-void method sig

    204 for void method sig

    It seems , WCF Data Services determines the response code by the method signature of your Service Operation and sets the response code on the context.  Thus, you have no chance to control this, unless in future release they allow an 'override' mechanism.

     

    I'd love to hear officially from the team though, if this is accurate. 

    • Marked as answer by Rybolt Wednesday, December 22, 2010 5:29 PM
    Wednesday, December 22, 2010 4:40 PM

All replies

  • For others that are curious.  In Service Operations you can not control the response code as mentioned previously.

    200 for non-void method sig

    204 for void method sig

    It seems , WCF Data Services determines the response code by the method signature of your Service Operation and sets the response code on the context.  Thus, you have no chance to control this, unless in future release they allow an 'override' mechanism.

     

    I'd love to hear officially from the team though, if this is accurate. 

    • Marked as answer by Rybolt Wednesday, December 22, 2010 5:29 PM
    Wednesday, December 22, 2010 4:40 PM
  • Yes, you're correct that the service operations (as well as normal queries or PUT/POST/MERGE/DELETE operations) always use status code determined by the WCF DS runtime.

    For service operations:

    200 is used when the response has some content and the request was successfull.

    204 is used when the response has no content and the request was successfull.

    Note that for errors you can specify the status code by throwing DataServiceException which takes the status code as its parameter in a constructor.

    Thanks,


    Vitek Karas [MSFT]
    • Proposed as answer by Mahesh Dedhia Monday, April 4, 2011 2:51 PM
    Wednesday, December 22, 2010 5:32 PM
    Moderator