WCF + REST: Where is the request data?

前端 未结 5 1585
有刺的猬
有刺的猬 2020-12-01 11:23

I\'m currently developing a WCF RESTful service. Within the validation of the POST data, I am throwing exceptions if the request XML does not conform to our business rules.

5条回答
  •  隐瞒了意图╮
    2020-12-01 11:59

    Here's how you do it without reflection:

    using (var reader = OperationContext.Current.RequestContext.RequestMessage.GetReaderAtBodyContents ()) {
        if (reader.Read ())
            return new string (Encoding.ASCII.GetChars (reader.ReadContentAsBase64 ()));
                    return result;
        }
    }
    

    If the reader is a HttpStreamXmlDictionaryReader (as it was in my case), the class's implementation of the method ReadContentAsBase64(byte[] buffer, int index, int count) simply passes these parameters to the Stream.Read method.

    Once I have the byte[] I convert the bytes to a string via ASCII encoding. For a proper implementation, you could use the content type & encoding from the message's headers to do per HTTP spec.

提交回复
热议问题