locked
How to get object back from OperationContext.Current.RequestContext.RequestMessage RRS feed

  • Question

  • User-1574460096 posted

    Hello,

    I'm receiving json data in request body and I see it in RequestMessage:

     public String Report_personnel_record(string signa, string lt, string serial, string timestamp, string maca, string keya) {

    …. using (var reader = OperationContext.Current.RequestContext.RequestMessage.GetReaderAtBodyContents()) ...

    That's the result in XML. I dont know why since I've setup WebMessageFormat.Json for request format … by the way how can I get back the object values below ?

    OperationContext.Current.RequestContext.RequestMessage = {<root type="object">
      <capture_time type="number">1609153374526</capture_time>
      <face type="array">
        <item type="number">-1</item>
        <item type="number">-40</item>
        <item type="number">-1</item>
        <item type="number">-32</item>
        <item...

    Here my OperationContract confinguration:

    [OperationContract]
            //[WebInvoke(Method = "POST", UriTemplate = "/report_personnel_record?id={id}&pid={pid}&name={name}&face={face}&temperature={temperature}&capture_time={capture_time}", BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
            [WebInvoke(Method = "POST", UriTemplate = "/report_personnel_record?sign={signa}&limit={lt}&sn={serial}&time={timestamp}&mac={maca}&key={keya}", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
            string Report_personnel_record(string signa, string lt, string serial, string timestamp, string maca, string keya);

    Thanks.

    BR.

    Marco.

    Monday, December 28, 2020 1:25 PM

All replies

  • User475983607 posted

    The code you've shown passes data in the URL. There is no indication that data is passed in the message body.  You did not share the client code so we cannot see what you're actually doing on the client.  Is the client C# code?  JavaScript?  You did not share the data contract either. 

    Keep in mind returning a string from a web method generally points to a poor design where the developer manually serializes a result set.  Basically serializing twice.  You did not share the method body either so there is not way to provide a meaningful suggestion.

    IMHO, WCF is not a good choice for REST services.  You should implement a REST framework like Web API because Web API is designed for REST services.  WCF is designed for SOAP.  Plus, WCF is not part of the latest .net 5 framework.  New REST development should be with Web API.

    Monday, December 28, 2020 2:01 PM
  • User-1574460096 posted

    Hi,

    it has been solved with the following code:

    DeviceUpdate dataObject = OperationContext.Current.RequestContext.RequestMessage.GetBody<DeviceUpdate>(new DataContractJsonSerializer(typeof(DeviceUpdate)));

    Interesting what are you sayng here :  "You should implement a REST framework like Web API because Web API is designed for REST services.  WCF is designed for SOAP … "I think I'm a little bit out of date :-).

    Thanks. Br.

    Marco.

    Saturday, January 2, 2021 10:01 AM