Supporting different data and serialization formats in WCF

Example showing how WCF message formatters can be used to support different serialization formats (in this case, the JSON.NET serializer) for operation parameters.

C# (208.3 KB)
 
 
 
 
 
(3)
3,143 times
Add to favorites
5/16/2012
E-mail Twitter del.icio.us Digg Facebook
Sign in to ask a question


  • Need clarification about UriTemplate.
    1 Posts | Last post September 20, 2013
    • Hello, Carlos!
      
      Thank you very much for your solution, it is solved my problem related to serializing objects with circular references to json.
      
      Can you explain what does it mean that  it doesn’t support UriTemplates? I'm having methods with UriTemplate like "users/print?name={userName}&surname={userSername} and it is working... What corner cases and situations I've missed where your solution won't work?
      Currently I commented out logic relate to UriTemplate checking...
      
      Also, you mentioned that it is not fully integrated with the WCF REST pipeline. Can you elaborate a liitle bit more about it?
      
      Once again, thank you very much!
      
  • What extra bits I need to do to make your code support UriTemplates? Is it possible?
    1 Posts | Last post January 14, 2013
    • I have successfully got your code working for one of my scenarios and lot of thanks for that but I need to get the UriTemplates working as well. I am stuck to use .NET 3.5. I wasn't sure if this is possible, if so please point me.
  • Does not work with ASP.NET AJAX Extensions
    1 Posts | Last post November 30, 2012
    • The ASP.NET AJAX script library will actually wrap single parameters, however WCF will set this.parameterNames to null.  You'll need to slightly rework the DeserializeRequest logic if you're using the script proxies:
      
      reader.Read();
      while (reader.TokenType == Newtonsoft.Json.JsonToken.PropertyName)
      {
         string parameterName = reader.Value as string;
         reader.Read();
         
         if(this.parameterNames == null) //Just set parameter 0 and exit
         {
            parameters[0] = serializer.Deserialize(reader, this.operation.Messages[0].Body.Parts[0].Type);
            break;
         }
      
         if (this.parameterNames.ContainsKey(parameterName))
         {
            int parameterIndex = this.parameterNames[parameterName];
            parameters[parameterIndex] = serializer.Deserialize(reader, this.operation.Messages[0].Body.Parts[parameterIndex].Type);
         }
         else
         {
            reader.Skip();
         }
      
         reader.Read();
      }
      
  • Thre is a typo in the code
    2 Posts | Last post May 16, 2012
    • The line "serializer.Serialize(writer, parameters[0]);" from NewtonsoftJsonClientFormatter\SerializeRequest() should be serializer.Serialize(writer, parameters[i]);
      
      (Instead of 0 there should be i as the parameters index).
      
      Thanks for this great sample. It really helped a lot!
    • Great catch, thanks. I'll fix it right away.