IOS序列化IEnumerable槽WCF

IOS序列化IEnumerable槽WCF

本文介绍了使用Protobuf-net和Monotouch for IOS序列化IEnumerable槽WCF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在IOS的Monotouch/Monodevelop上编写WCF服务的代码.我对我的可序列化对象使用了[DataMember]/[DataContract]等标准属性,对我的接口使用了[ServiceContract]/[OperationContract]等标准属性.一切工作正常,但是当我尝试实现在接口实现(服务器端)上返回IEnumerable的方法时,它没有效果.

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.

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

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

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

My interface (Server side on Windows):

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

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

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

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

我的界面实现:

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 suspect it should be possible to tweak the data-contract serializer code for that. I'll take a look.

现在:我的建议是返回一个明显的具体类型,该类型具有IEnumerable<T>作为属性.或者,也许返回一个List<T>,它可能会起作用.

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.

这篇关于使用Protobuf-net和Monotouch for IOS序列化IEnumerable槽WCF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 11:09