I need to make a post request to a wcf rest service (4.0). This service will be called from both client-side apps(jquery) and server side apps (asp.net etc). The service is exposing a persistent store and provides CRUD operations.
I have an issue with serialzation.
I read a few articles related to this -> http://blogs.msdn.com/b/youssefm/archive/2009/04/21/understanding-known-types.aspx
public class CustomType
{
public Dictionary<string,object> CustomObjectCollection {get; set;}
public SavedBy {get; set;}
public SavedOn {get; set;}
public CustomType()
{
CustomObjectCollection = new Dictionary<string,object>();
}
}
public class State1
{
public prop1 {get; set;}
public prop2 {get; set;}
}
public class State2
{
public prop1 {get; set;}
public prop2 {get; set;}
}
Client consuming WCF :
public void save()
{
CustomType req = new CustomType();
req.CustomObjectCollection("State1", new State1() {prop1 = val1, prop2 = val2 } );
req.CustomObjectCollection("State2", new State2() {prop1 = val3, prop2 = val4 } );
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(CustomType));
MemoryStream ms = new MemoryStream();
serializer.WriteObject(ms, setting);
string json = Encoding.Default.GetString(ms.ToArray());
ms.Close();
Utility.HTTPRequest("http://localhost:2222/MyWCFRestSVC/save", "post", json);
}
WCF Method :
public bool Save(CustomType req)
{
/////////
}
I may not be able to use the "known types" work-around since every client that consumes this service will use its own custom types. Ideally the service should not worry about the types the clients wants to persist. Note that the service is exposing a persistent store which just holds generic state information.
I have tried Dictionary instead of Dictionary where I store json string representations os State1 & State2 as values. The problem I ran into was that when I used DataContractJsonSerializer to serialize the CustomType instance the json strings for State1 & State2 got backslashes(escape characters) embedded into them, due to double serialization I guess ?
I had thoughts about custom serialization instead of DataContractJsonSerializer.
Please let me know your thoughts.
Thanks for your time.