I have a service that I am trying to pass a dictionary of objects through. All is well and works until I add a Dictionary of objects that then contains a Dictionary to yet another object. The dictionary causing the problem would be class A in the below example.
//example
public class A
{
public Dictionary<string,B> BValues { get; set; }
}
public class B
{
public Dictionary<string, C> CValues { get; set; }
}
public class C
{
int x;
int y;
}
I have tried making the the dictionaries knowntypes in my service, and also making sure the other classes are marked as a data contract, their properties marked with the DataMember attribute. I'm at a loss on what else I need 开发者_StackOverflowto or even can do in order to make this work.
Edit
The exception:
Element 'schemas.microsoft.com/2003/10/Serialization/Arrays:Value'; contains data of the 'schemas.microsoft.com/2003/10/Serialization/…; data contract. The deserializer has no knowledge of any type that maps to this contract. Add the type corresponding to 'ArrayOfKeyValueOfstringClassValuesNSpYOWsW' to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding it to the list of known types passed to DataContractSerializer.'
To try to work out where this is failing, I've set up a simple WCF service that returns a very basic nested Dictionary
which works perfectly well with a number of items added to it.
[OperationContract]
Dictionary<int, Dictionary<int, int>> GetDictionary();
Based on this, I am assuming it's a problem with your objects (& perhaps the fact that they are in Dictionary
collections). I suggest you do some tests to narrow down your problem.
Is your class serializable - can you deserialize a single instance of your type? (e.g. write an operation to return an object like MyClass GetOneInstance();
). Are you able to return a simple Dictionary
of your class?
There are a handful of naming clashes that WCF can throw up at you - if your classes have the same name as your service or your operation, for example an operation GetSomething
and an object called GetSomethingRequest
or GetSomethingResponse
will not work well together - essentially due to the request and response being put into automatically-named SOAP envelopes. If your classes are named using some reserved word you could try renaming them.
The exception suggests that you add the KnownType
attribute. This is usually applicable when you are returning a base type which could be an instance of an inheriting type - is this the case? Do your objects involve inheritance, and are you returning base types which could be instances of inheriting types? (e.g. A:B
and your operation returns a B
, but it may be an A
)
精彩评论