【问题标题】:Serialization issues calling WCF service from iPhone MonoTouch app从 iPhone MonoTouch 应用程序调用 WCF 服务的序列化问题
【发布时间】:2011-02-14 00:04:12
【问题描述】:

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

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

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

CustomerName 属性确实有公共 getter 和 setter

但以下异常表明它没有。如果有人能告诉我我做错了什么,我需要一些帮助!

异步操作中的异常:System.Runtime.Serialization.InvalidDataContractException:类型“NorthwindService.Customer”上的 DataMember 属性“System.String CustomerName”必须同时具有 getter 和 setter。 在 /Developer/MonoTouch/Source/mono/mcs/class/System.Runtime.Serialization/System 中的 System.Runtime.Serialization.SharedContractMap.GetMembers(System.Type 类型,System.Xml.XmlQualifiedName qname,Boolean declared_only)[0x00116] .Runtime.Serialization/SerializationMap.cs:553 在 System.Runtime.Serialization.SharedContractMap.Initialize () [0x00053] 在 /Developer/MonoTouch/Source/mono/mcs/class/System.Runtime.Serialization/System.Runtime.Serialization/SerializationMap.cs:516 在 System.Runtime.Serialization.KnownTypeCollection.RegisterContract(System.Type 类型)[0x0004f] 在 /Developer/MonoTouch/Source/mono/mcs/class/System.Runtime.Serialization/System.Runtime.Serialization/KnownTypeCollection.cs:766 在 System.Runtime.Serialization.KnownTypeCollection.TryRegister (System.Type 类型) [0x0002a] 在 /Developer/MonoTouch/Source/mono/mcs/class/System.Runtime.Serialization/System.Runtime.Serialization/KnownTypeCollection.cs:593 在 System.Runtime.Serialization.KnownTypeCollection.RegisterCollection(System.Type 类型)[0x0000f] 在 /Developer/MonoTouch/Source/mono/mcs/class/System.Runtime.Serialization/System.Runtime.Serialization/KnownTypeCollection.cs:666 在 System.Runtime.Serialization.KnownTypeCollection.TryRegister (System.Type 类型) [0x00062] 在 /Developer/MonoTouch/Source/mono/mcs/class/System.Runtime.Serialization/System.Runtime.Serialization/KnownTypeCollection.cs:605 在 System.Runtime.Serialization.KnownTypeCollection.InsertItem(Int32 索引,System.Type 类型)[0x00000] 在 /Developer/MonoTouch/Source/mono/mcs/class/System.Runtime.Serialization/System.Runtime.Serialization/KnownTypeCollection。 CS:389 在 System.Collections.ObjectModel.Collection`1[T].Add (.T item) [0x0000c] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Collections.ObjectModel/Collection.cs:72 在 /Developer/MonoTouch/Source/mono/mcs/class/System.Runtime.Serialization/System.Runtime.Serialization/ 中的 System.Runtime.Serialization.DataContractSerializer.ReadObject(System.Xml.XmlDictionaryReader 阅读器,布尔验证对象名称)[0x0000c] DataContractSerializer.cs:267 在 System.Runtime.Serialization.XmlObjectSerializer.ReadObject(System.Xml.XmlDictionaryReader 阅读器)[0x00000] 在 /Developer/MonoTouch/Source/mono/mcs/class/System.Runtime.Serialization/System.Runtime.Serialization/XmlObjectSerializer.cs :74 在 System.ServiceModel.Dispatcher.DataContractMessagesFormatter.MessageToParts (System.ServiceModel.Description.MessageDescription md, System.ServiceModel.Channels.Message 消息) [0x000b8] 在 /Developer/MonoTouch/Source/mono/mcs/class/System.ServiceModel/ System.ServiceModel.Dispatcher/BaseMessagesFormatter.cs:364 在 /Developer/MonoTouch/Source/mono/mcs/class/System.ServiceModel/System 中的 System.ServiceModel.Dispatcher.BaseMessagesFormatter.DeserializeReply(System.ServiceModel.Channels.Message 消息,System.Object[] 参数)[0x00043]。 ServiceModel.Dispatcher/BaseMessagesFormatter.cs:175 在 System.ServiceModel.ClientRuntimeChannel.Request (System.ServiceModel.Description.OperationDescription od, System.Object[] 参数) [0x0016a] 在 /Developer/MonoTouch/Source/mono/mcs/class/System.ServiceModel/System.ServiceModel/ ClientRuntimeChannel.cs:500 在 System.ServiceModel.ClientRuntimeChannel.DoProcess(System.Reflection.MethodBase 方法,System.String operationName,System.Object[] 参数)[0x00038] 在 /Developer/MonoTouch/Source/mono/mcs/class/System.ServiceModel/System .ServiceModel/ClientRuntimeChannel.cs:443 在 System.ServiceModel.ClientRuntimeChannel.Process(System.Reflection.MethodBase 方法,System.String operationName,System.Object[] 参数)[0x00000] 在 /Developer/MonoTouch/Source/mono/mcs/class/System.ServiceModel/System .ServiceModel/ClientRuntimeChannel.cs:425

【问题讨论】:

    标签: iphone wcf


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多