0

我在monotouch(6.0.2)中使用带有BasicHttpBinding的WCF来使用.net WCF服务,我传递了3个类型为int,int,DishOrder(用户定义类型)的参数,在服务器站点中,它可以读取int 参数正确,但是用户定义的对象(dishOrder)总是为空,为什么?

我已经在用户自定义类型类中使用了[DataContract]属性标记。

在monotouch(客户端代码:)

[DataContract]
public class DishOrder2
{
    [DataMember]
    public int BillID { get; set; }
    [DataMember]
    public int TableID { get; set; }
}

public partial interface IMenuContract
{
    [OperationContract]
    int OpenTable(int tableID, int lastVerNo,DishOrder2 order);
}

public partial class MyServiceClient : ClientBase<IMenuContract>,IMenuContract  {
    public int OpenTable(int tableID, int lastVerNo,DishOrder2 order)
    {
        return Channel.OpenTable(tableID,lastVerNo,order);
    }   

    private partial class FDMenuServiceClientChannel
    {
        public  int OpenTable(int tableID, int lastVerNo,DishOrder2 order)
        {
            object[] _args = new object[3];

            _args[0]=tableID;
            _args[1]=lastVerNo;
            _args[2]=order;
            return (int)base.Invoke("OpenTable", _args);
        }
    }
} //end of public partial class FDMenuServiceClient 



   pulic void CallServiceTest()
  {
            using (MyServiceClient.serviceClient = MyServiceClient.CreateServiceClient ()) {
                  DishOrder2 test=new DishOrder2();
            test.BillID=123;
            test.TableID=456;
                serviceClient.OpenTable(111,222,test);
            }
  }

在服务站点(.net 3.5)代码中:

[DataContract]
public class DishOrder2
{
    [DataMember]
    public int BillID { get; set; }
    [DataMember]
    public int TableID { get; set; }
}

[OperationContract]
int OpenTable(int tableID, int lastVerNo,DishOrder2 order);

    public int OpenTable(int tableID, int lastVerNo,DishOrder2 dishOrder)  //service implementation
    {
        try
        {
            //**---why the object:dishorder is null?** throw the nullref exception
            if (dishOrder.BillID>0)  ...                  
            //**tableID & lastVerNo is read OK 111,222**  
                  ...             
        }
        catch (Exception ex)
        {
            throw FaultException...            
         }
    }
4

1 回答 1

0

As I cannot found the reason why cannot serialize the object automatic in monotouch, So I have to do serialization myself. It can work now, thanks all.

    public static string XmlSerialize<T>(T obj)
    {
        using (MemoryStream ms = new MemoryStream())
        {
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            serializer.Serialize(ms, obj);
            ms.Seek(0, SeekOrigin.Begin);
            using (StreamReader reader = new StreamReader(ms, Encoding.UTF8))
            {
                return reader.ReadToEnd();
            }
        }
    }
于 2012-10-09T01:51:50.307 回答