c# - 使用 Protobuf-net 和 Monotouch for IOS 通过 WCF 序列化 IEnumerable

标签 c# wcf ienumerable protobuf-net serialization

我正尝试在 Monotouch/Monodelop for IOS 上编写 WCF 服务。我为我的可序列化对象使用标准属性,例如 [DataMember]/[DataContract],为我的接口(interface)使用 [ServiceContract]/[OperationContract]。一切正常,但是当我尝试在接口(interface)实现(服务器端)上实现返回 IEnumerable 的方法时,它没有工作。

因此,为了解决我的问题,我尝试使用最新版本的 protobuf-net protobuf-net v2 beta r404。但是我仍然收到来自 Protobuf-net 的序列化错误。请注意,“MyObject”中的 IEnumerable 序列化没有问题。

这是我的代码现在的样子:

我的对象:

[ProtoContract]
public class MyObject
{
    public MyObject ()
    {
    }

    [ProtoMember(1)]
    public int Id {get;set;}

    [ProtoMember(2)]
    public IEnumerable<MyObject> myObjects {get;set;}
}

我的界面(Windows 上的服务器端):

[ServiceContract]
public interface ITouchService
{
        [OperationContract, ProtoBehavior]
        MyObject Execute();

    [OperationContract, ProtoBehavior]
    IEnumerable<MyObject> ExecuteENUM ();
}

我的界面(IOS 的客户端,我无法将 ProtoBehavior 添加为属性,因为它不在 protobuf 的 ISO dll 中):

[ServiceContract]
public interface ITouchService
{
    [OperationContract]
    MyObject Execute();

    [OperationContract]
    IEnumerable<MyObject> ExecuteENUM ();
}

我的接口(interface)实现:

public class TouchService : ITouchService
    {
        public MyObject Execute()
        {
            var myObject = new MyObject() { Id = 9001 };

            var myList = new List<MyObject>();

            for (int i = 0; i < 10; i++)
            {
                myList.Add(new MyObject() { Id = i });
            }

// Will serialize
            myObject.myObjects = myList;

            return myObject;

        }

        public IEnumerable<MyObject> ExecuteENUM()
        {
            var myEnum = new List<MyObject>();

            for (int i = 0; i < 10; i++)
            {
                myEnum.Add(new MyObject() { Id = i });
            }

            return myEnum;
        }
    }

最佳答案

我怀疑应该可以为此调整数据协定序列化程序代码。我去看看。

现在:我的建议是返回一个明显的具体类型,它具有 IEnumerable<T>作为属性(property)。或者返回 List<T>相反,这可能会起作用。

不过,支持这种情况听起来很合理。我会看看我能做些什么。

关于c# - 使用 Protobuf-net 和 Monotouch for IOS 通过 WCF 序列化 IEnumerable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6269783/

相关文章:

c# - 如何在 C# 中查找具有角色格式的字符串?

c# - 为什么 Enumerable.Range 比直接 yield 循环更快?

c# - ASP.NET MVC - 当模型添加附加字段时更新强类型 View 的简单方法?

c# - 自动增加文件名

c# - 如何在 C# 中发现 onvif 设备

c# - 在 WCF 服务上启用 CORS。获取 HTTP 405 : Method Not Allowed

c# - Linq To Sql 从函数返回为 IQueryable<T>

c# - IEnumerable IEnumerator 带或不带 Current moveNext 重置

c# - 在 c# rabbitmq 客户端中得到 "Pipelining of requests forbidden"

WCF、GET 和 HTTPS