Reputation: 651
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.
Upvotes: 1
Views: 2793
Reputation: 3413
When I have needed to transport Dictionary over WCF / web services in the past, I have generally subclassed from Dictionary and then implemented the IXmlSerializable interface - i.e. ReadXml(), WriteXml()
This allows control over the format of the message over the wire and, if you have knowledge of the type of data you will be transmitting, can result in more efficient means of serialization.
Upvotes: 0
Reputation: 42510
I asked a similar question a while ago. No direct answer, but we came up with a workaround:
Serializing IDictionary<string, object> in WCF
Upvotes: 0
Reputation: 9799
Since the service is obviously not using the Dictionary in a way that it was intended maybe you should look at a different data structure?
By "in a way that it was intended", what I mean is for quick look ups. That is a hash table. You're simply collecting a bunch of information into it only to push it out to your client applications. So what you need is
List<string, List<object>>
Also make sure your "object" is serializeable as well.
Upvotes: 1