Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In order to achieve my goal of having a service that can accept a completely arbitrary list of "key": "value" pairs as a raw JSON string and then decide what do do with them without having to know the "key" names beforehand, I combined casper and aaron's advice.</p> <p>1st, to access the raw JSON string, <a href="http://blogs.msdn.com/carlosfigueira/archive/2008/04/17/wcf-raw-programming-model-receiving-arbitrary-data.aspx" rel="nofollow noreferrer">this MSDN blog was very helpful.</a> </p> <p>I was unable to simply change the single method parameter to String and the <code>BodyStyle</code> to <code>WebMessageBodyStyle.Bare</code> without problems. When setting BodyStyle to Bare, make sure that the endpoint <code>behaviorConfiguration</code> is set to <code>&lt;webHttp/&gt;</code> and not <code>&lt;enableWebScript/&gt;</code>.</p> <p>The second note is that, as <a href="https://stackoverflow.com/questions/2297903/generic-wcf-json-deserialization/2298039#2298039">casperOne mentioned</a>, the method can only have 1 parameter. This parameter needs to be a <code>Stream</code> in order to access the raw text though (see MSDN blog above).</p> <p>Once you have the raw JSON string, it's just a matter of deserializing it into a StringDictionary. I chose JSON.Net for this and it works wonderfully. Here's a bare bones example to illustrate.</p> <pre><code>[OperationContract] [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)] public string Register(Stream rawJSON) { // Convert our raw JSON string into a key, value StreamReader sr = new StreamReader(rawJSON); Dictionary&lt;string, string&gt; registration = JsonConvert.DeserializeObject&lt;Dictionary&lt;string, string&gt;&gt;( sr.ReadToEnd()); // Loop through the fields we've been sent. foreach (KeyValuePair&lt;string, string&gt; pair in registration) { switch (pair.Key.ToLower()) { case "firstname": return pair.Value; break; } } } </code></pre> <p>This allows me to accept an arbitrary list of fields via JSON and for the field names to be case insensitive. I know it's not the strictest approach to data integrity or to how WCF services would ideally be structured, but, as far as I can tell, it's the simplest way to get where I want to go.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload