2011-01-20 22 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;} 
} 
関連する問題