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

发布于 2024-11-14 11:18:40 字数 1927 浏览 0 评论 0原文

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

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

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

MyObject:

[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 ();
}

我的接口实现:

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

I'm trying to code a WCF service on Monotouch/Monodevelop for IOS. I was using standard attributes like [DataMember]/[DataContract] for my serializable object and [ServiceContract]/[OperationContract] for my interface. Everything worked fine, but when I tried to implement a method returning an IEnumerable on the interface implementation (server side), it didn't worked.

So to solve my problem I tried to use the latest version of protobuf-net being protobuf-net v2 beta r404. But I'm still getting a serialization error from Protobuf-net. Note that the IEnumerable in "MyObject" serialize without problem.

Here's how my code looks right now:

MyObject:

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

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

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

My interface (Server side on Windows):

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

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

My interface (client side on IOS, I can't add ProtoBehavior as attribute becuase it is not in the ISO dll of protobuf):

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

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

My interface implementation:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

将军与妓 2024-11-21 11:18:40

我怀疑应该可以为此调整数据契约序列化器代码。我去看看。

目前:我的建议是返回一个明显的具体类型,该类型具有 IEnumerable作为属性。或者返回一个 List ,这可能会起作用。

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

I suspect it should be possible to tweak the data-contract serializer code for that. I'll take a look.

For now: my advice would be to return an obvious concrete type that has the IEnumerable<T> as a property. Or maybe return a List<T> instead, which might work.

It sounds reasonable to support this scenario, though. I'll see what I can do.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文