2011-01-20 60 views
0

我有了這樣WCF字典<串,對象>序列

[ServiceContract(Namespace = "")] 
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 
    public class MyService 
    { 
     [OperationContract] 
     public object MyMethod(string param1, string param2, object[] myarray) 
     { 
      //do stuff 
      return result; 
     } 
    } 

我打電話給我的方法從我的這樣的代碼的方法的服務:

public Dictionary<string, object> MyDictionary{ get; set; } 
serv.MyMethodCompleted += new EventHandler<MyServiceReference.MyMethodCompletedEventArgs>(serv_MyMethodCompleted); 
serv.MyMethodAsync("param1","param2", new ObservableCollection<object>(){MyDictionary}); 


void serv_MyMethodCompleted(object sender, MyServiceReference.MyMethodCompletedEventArgs e) 
{ 
    //Happy happy joy joy 
} 

Everithing與此錯誤craches :

There was an error while trying to serialize parameter :myarray. The InnerException message was 'Type 'System.Collections.Generic.Dictionary`2[[System.String, mscorlib, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Object, mscorlib, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]' with data contract name 'ArrayOfKeyValueOfstringanyType:http://schemas.microsoft.com/2003/10/Serialization/Arrays' is not expected. Add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.'. Please see InnerException for more details.

public System.IAsyncResult BeginCallMethod(string param1, string param2, System.Collections.ObjectModel.ObservableCollection<object> myarray, System.AsyncCallback callback, object asyncState) { 
       object[] _args = new object[3]; 
       _args[0] = param1; 
       _args[1] = param2; 
       _args[2] = myarray; 
       System.IAsyncResult _result = base.BeginInvoke("MyMethod", _args, callback, asyncState); <--here it craches 
       return _result; 
      } 

WH我做錯了嗎?我怎樣才能解決這個問題?

回答

0

myArray參數和返回值需要是強類型的,並用DataContract和DataMember屬性進行歸因。 myArray應該像IEnumerable集合<可以序列化的項目>:

[DataContract] 
class Item 
{ 
    [DataMember] 
    public string Name {get;set;} 

    [DataMember] 
    public double Cost {get;set;} 
} 
相關問題