0
votes

I have a service that has a method like this

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

I call my method from my code like this:

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 with this error:

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;
            }

what did I did wrong? how can I fix this?

1

1 Answers

0
votes

The myArray parameter and the return value needs to be a strongly typed and attributed with DataContract and DataMember attributes. The myArray should be collection like IEnumerable<Item> that can be serialized:

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

    [DataMember]
    public double Cost {get;set;}
}