ho le seguenti ServiceContract e DataContract classi:Lista WCF errore <string > di serializzazione/deserializzazione
[ServiceContract]
public interface IWcfService
{
[OperationContract]
Response GetData();
}
[DataContract]
public class Response
{
[DataMember]
public Dictionary<string, object> Data { get; set; }
}
Quando il valore di dizionario Response.Data è di tipo int, string, doppia o altri tipi primitivi 'semplici' , WCF può serializzare correttamente l'oggetto. Ma quando il valore del dizionario Response.Data è di tipo List < string>, il cliente ha gettato seguente eccezione quando itreceived i dati e ha cercato di deserializzare esso:
Message=The formatter threw an exception while trying to deserialize the message:
There was an error while trying to deserialize parameter http://tempuri.org/:GetDataResult.
The InnerException message was 'Error in line 1 position 990.
Element 'http://schemas.microsoft.com/2003/10/Serialization/Arrays:Value' contains data from a type
that maps to the name 'http://schemas.microsoft.com/2003/10/Serialization/Arrays:ArrayOfstring'.
The deserializer has no knowledge of any type that maps to this name.
Consider using a DataContractResolver or add the type corresponding to 'ArrayOfstring'
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.'.
Ho anche provato ad aggiungere l'attributo al KnownType ServiceContract e DataContract come segue:
[ServiceContract]
[ServiceKnownType(typeof(List<string>))]
[ServiceKnownType(typeof(Dictionary<string, string>))]
[ServiceKnownType(typeof(Dictionary<string, List<string>>))]
public interface IWcfService
{
[OperationContract]
[ServiceKnownType(typeof(List<string>))]
[ServiceKnownType(typeof(Dictionary<string, string>))]
[ServiceKnownType(typeof(Dictionary<string, List<string>>))]
Response GetData();
}
[DataContract]
[ServiceKnownType(typeof(List<string>))]
[ServiceKnownType(typeof(Dictionary<string, string>))]
[ServiceKnownType(typeof(Dictionary<string, List<string>>))]
[KnownType(typeof(List<string>))]
[KnownType(typeof(Dictionary<string, string>))]
[KnownType(typeof(Dictionary<string, List<string>>))]
public class Response
{
[DataMember]
public Dictionary<string, object> Data { get; set; }
}
Ma niente di tutto questo ha aiutato. Qualcuno ha qualche idea su questo?
Aggiornato
I Dati sarà simile:
Data = new new DIctionary<string, object>
{
{"_id", 12344},
{"names", new List<string>{ "John", "Peter", "Jack"}},
{"time", DateTime.Now}
}
Il motivo per cui abbiamo usato Dizionario < string, object>: server deve inviare al cliente un dizionario di 'dinamica' dati, che possono essere int, List, DataTime, ecc. Aiuterà a risolvere questo problema usando Dictionary, ma anche a perdere le informazioni sul tipo originale. Ad esempio, il client ha bisogno di Elenco e crea un certo legame con i dati per visualizzare la raccolta, quindi List.ToString() non sarà utile in questo caso.
Presumibilmente uno dei valori nel dizionario è 'string []' o 'Lista'? Devo dire: l'uso di 'object' *** in qualsiasi punto *** in un contratto di dati dovrebbe immediatamente attivare campane d'allarme. 'object' * non è un contratto * - è l'assenza totale * di un contratto. Però! Hai provato a includere '[KnownType (typeof (string []))]// [ServiceKnownType (typeof (string []))]' (nota: aggiungere attributi casuali non è il modo migliore per "aggiustare" qualcosa) –
When i valori nel dizionario sono Lista, causa il problema. Ho anche provato a usare [KnownType (typeof (string []))]/[ServiceKnownType (typeof (string []))], ma non ha aiutato –
wd113
Stai usando Aggiungi riferimento di servizio per creare un client che chiama il servizio? –