English 中文(简体)
从iPhone MonoTouch应用程序调用WCF服务的序列化问题
原标题:Serialization issues calling WCF service from iPhone MonoTouch app
  • 时间:2011-02-14 00:04:12
  •  标签:
  • iphone
  • wcf

我在Windows服务器上有一个WCF服务,我正试图从MonoDevelopment中的iPhone应用程序调用该服务。web服务客户端是使用Silverlight服务生成实用程序生成的,并复制到MonoTouch环境中的iPhone开发项目中。

所以服务很简单。我有一个方法,它调用GetCustomers()方法并返回List<;客户>。我可以在MonoTouch iPhone应用程序代码中成功创建客户端。该方法确实会执行,如果我尝试对结果进行计数,它会告诉有91条记录(Northwind数据库/Customers表)。所以我确信这项服务运行得很好。

问题是,当我尝试访问Customer对象的任何属性时,我会收到以下错误:

CustomerName属性没有公共getter和setter

但下面的例外情况表明它不是。如果有人能告诉我我做错了什么,我需要一些帮助!

Exception in async operation: System.Runtime.Serialization.InvalidDataContractException: DataMember property  System.String CustomerName  on type  NorthwindService.Customer  must have both getter and setter.
  at System.Runtime.Serialization.SharedContractMap.GetMembers (System.Type type, System.Xml.XmlQualifiedName qname, Boolean declared_only) [0x00116] in /Developer/MonoTouch/Source/mono/mcs/class/System.Runtime.Serialization/System.Runtime.Serialization/SerializationMap.cs:553 
  at System.Runtime.Serialization.SharedContractMap.Initialize () [0x00053] in /Developer/MonoTouch/Source/mono/mcs/class/System.Runtime.Serialization/System.Runtime.Serialization/SerializationMap.cs:516 
  at System.Runtime.Serialization.KnownTypeCollection.RegisterContract (System.Type type) [0x0004f] in /Developer/MonoTouch/Source/mono/mcs/class/System.Runtime.Serialization/System.Runtime.Serialization/KnownTypeCollection.cs:766 
  at System.Runtime.Serialization.KnownTypeCollection.TryRegister (System.Type type) [0x0002a] in /Developer/MonoTouch/Source/mono/mcs/class/System.Runtime.Serialization/System.Runtime.Serialization/KnownTypeCollection.cs:593 
  at System.Runtime.Serialization.KnownTypeCollection.RegisterCollection (System.Type type) [0x0000f] in /Developer/MonoTouch/Source/mono/mcs/class/System.Runtime.Serialization/System.Runtime.Serialization/KnownTypeCollection.cs:666 
  at System.Runtime.Serialization.KnownTypeCollection.TryRegister (System.Type type) [0x00062] in /Developer/MonoTouch/Source/mono/mcs/class/System.Runtime.Serialization/System.Runtime.Serialization/KnownTypeCollection.cs:605 
  at System.Runtime.Serialization.KnownTypeCollection.InsertItem (Int32 index, System.Type type) [0x00000] in /Developer/MonoTouch/Source/mono/mcs/class/System.Runtime.Serialization/System.Runtime.Serialization/KnownTypeCollection.cs:389 
  at System.Collections.ObjectModel.Collection`1[T].Add (.T item) [0x0000c] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Collections.ObjectModel/Collection.cs:72 
  at System.Runtime.Serialization.DataContractSerializer.ReadObject (System.Xml.XmlDictionaryReader reader, Boolean verifyObjectName) [0x0000c] in /Developer/MonoTouch/Source/mono/mcs/class/System.Runtime.Serialization/System.Runtime.Serialization/DataContractSerializer.cs:267 
  at System.Runtime.Serialization.XmlObjectSerializer.ReadObject (System.Xml.XmlDictionaryReader reader) [0x00000] in /Developer/MonoTouch/Source/mono/mcs/class/System.Runtime.Serialization/System.Runtime.Serialization/XmlObjectSerializer.cs:74 
  at System.ServiceModel.Dispatcher.DataContractMessagesFormatter.MessageToParts (System.ServiceModel.Description.MessageDescription md, System.ServiceModel.Channels.Message message) [0x000b8] in /Developer/MonoTouch/Source/mono/mcs/class/System.ServiceModel/System.ServiceModel.Dispatcher/BaseMessagesFormatter.cs:364 
  at System.ServiceModel.Dispatcher.BaseMessagesFormatter.DeserializeReply (System.ServiceModel.Channels.Message message, System.Object[] parameters) [0x00043] in /Developer/MonoTouch/Source/mono/mcs/class/System.ServiceModel/System.ServiceModel.Dispatcher/BaseMessagesFormatter.cs:175 
  at System.ServiceModel.ClientRuntimeChannel.Request (System.ServiceModel.Description.OperationDescription od, System.Object[] parameters) [0x0016a] in /Developer/MonoTouch/Source/mono/mcs/class/System.ServiceModel/System.ServiceModel/ClientRuntimeChannel.cs:500 
  at System.ServiceModel.ClientRuntimeChannel.DoProcess (System.Reflection.MethodBase method, System.String operationName, System.Object[] parameters) [0x00038] in /Developer/MonoTouch/Source/mono/mcs/class/System.ServiceModel/System.ServiceModel/ClientRuntimeChannel.cs:443 
  at System.ServiceModel.ClientRuntimeChannel.Process (System.Reflection.MethodBase method, System.String operationName, System.Object[] parameters) [0x00000] in /Developer/MonoTouch/Source/mono/mcs/class/System.ServiceModel/System.ServiceModel/ClientRuntimeChannel.cs:425 
问题回答

不,服务不太好。它看起来是一个异步方法,所以当您的第一个调用看起来成功时,除非您关闭异步调用并获得返回值,否则可能不会真正成功。结果的处理和计数可能非常成功,但结果的实际序列化(要求Customer类型同时具有setter和getter)却没有成功。

这是WCF中为数不多的几乎总是正确且非常具体的例外之一。您确定Customer类型同时具有getter和setter吗?他们的访问级别是否一致?

MonoTouch链接器将排除最终原生iOS二进制文件中未使用的任何代码。在您的情况下,getter/setter可能没有在您的项目中使用,因此也没有进入本机版本。

若要解决此问题,可以使用MonoTouch.Foundation.Preserve属性装饰DataMember属性。或者,将Preserve属性粘贴在作为DataContract的类上,并传递AllMembers=true,如下面的示例所示。

[DataContract,
MonoTouch.Foundation.Preserve(AllMembers=true)]
public class EntityClass
{
    [DataMember]
    public string Field {get;set;}
}




相关问题
Code sign Error

I have created a new iPhone application.I have two mach machines. I have created the certificate for running application in iPhone in one mac. Can I use the other mac for running the application in ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

将音频Clips从Peter改为服务器

我不禁要问,那里是否有任何实例表明从Peit向服务器发送音响。 I m不关心电话或SIP风格的解决办法,只是一个简单的袖珍流程......

• 如何将搜查线重新定位?

我正试图把图像放在搜索条左边。 但是,问题始于这里,搜索条线不能重新布署。