我在monotouch(6.0.2)中使用带有BasicHttpBinding的WCF来使用.net WCF服务, 我传递了3个参数,类型是int,int,DishOrder(用户定义类型),在服务器站点中,它可以正确读取int参数,但是用户定义的对象(dishOrder)总是为空,为什么?
我已经在用户定义的类型类中使用了[DataContract]属性标记。
单声道中的(客户端代码:)
[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...
}
}
答案 0 :(得分:0)
由于我无法找到无法在monotouch中自动序列化对象的原因,所以我必须自己进行序列化。它现在可以工作了,谢谢大家。
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();
}
}
}