protobuf-net v2 和 Monotouch:它是如何混合的? [英] protobuf-net v2 and Monotouch : How does it mix?

查看:43
本文介绍了protobuf-net v2 和 Monotouch:它是如何混合的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试将 protobuf-net 与 MonoTouch 一起使用,但我不知道如何使用,尽管听说这是可能的,但我一直无法找到任何实际工作的教程或任何示例.

I have been trying to use protobuf-net with MonoTouch but I have no idea how, and despite having heard that it is possible, I haven't been able to find any tutorial or any example that actually work.

Marc Gravell 在他的博客上证实了这一点 它确实适用于 MonoTouch.我也浏览了他在这篇文章中提到的两个人的博客,但我没有找到任何与 protobuf 相关的内容.

It was confirmed by Marc Gravell on his blog that it does work on MonoTouch. I have also looked through the blogs of the two people he states in this article, but I haven't found anything related to protobuf.

在这个问题上没有任何线索,我决定下载 protobuf-net 并尝试一下.所以我创建了以下对象用于测试目的:

Having no lead on the subject, i decided to download protobuf-net and try it out anyway. So I created the following object for testing purposes :

[ProtoContract]
public class ProtoObject
{
    public ProtoObject()
    {

    }

    [ProtoMember(1)]
    public byte[] Bytes { get; set; }
}

并且我尝试使用带有

[OperationContract]
ProtoObject GetObject();

但设备上接收到的 ProtoObject 实例始终为空.这并不出人意料,因为我已经读过要使 protobuf-net 与 WCF 一起工作,您需要 修改 app.config/web.config.

but the instance of ProtoObject recieved on the device is always null. This is not really unexpected since i have read that to make protobuf-net work with WCF you need to modify the app.config/web.config.

因为一个 MonoTouch 项目没有 app.config,所以实现起来有点困难,但我还没有放弃.为了替换 app.config,我尝试以编程方式将 ProtoEndpointBehavior 添加到客户端端点的行为中,但我遇到了障碍.ProtoBuf.ServiceModel.ProtoEndpointBehavior,在 protobuf-net 的 .NET 3.0 实现上可用,在 iOS 版本上不可用.

It's a little hard to accomplish since a MonoTouch project has no app.config, but I did not yet give up. To replace the app.config, I tried to add the ProtoEndpointBehavior to the client's endpoint's behaviors programmatically, and there I hit a wall. ProtoBuf.ServiceModel.ProtoEndpointBehavior, available on .NET 3.0 implementation of protobuf-net is not available on the iOS release.

我将如何使用 protobuf-net 反序列化从使用 protobuf-net 序列化的基于 Windows 的 WCF 端点接收的对象.

How would I go about using protobuf-net to deserialize objects received from a windows-based WCF endpoint using protobuf-net serialization.

推荐答案

它实际上与本Friction Point Studios 的博客条目.由于在设备上进行元编程并不是真正的选择,所以诀窍是预先生成一个序列化 dll.这可以通过创建一个配置 RuntimeTypeModel(通过添加您感兴趣的类型)的小型控制台 exe(这只是一个工具 - 它的设计并不漂亮)来完成,然后调用 .Compile(...):

It is actually pretty much the same as described in this blog entry by Friction Point Studios. Since meta-programming on the device is not really an option, the trick is to pre-generate a serialization dll. This can be done by creating a small console exe (this is just a tool - it isn't designed to be pretty) that configures a RuntimeTypeModel (by adding the types you are interested in), and then call .Compile(...):

var model = TypeModel.Create();
model.Add(typeof (ProtoObject), true);
model.Compile("MySerializer", "MySerializer.dll");

这会生成一个序列化程序 dll;只需引用此 dll(以及 iOS 版本的 protobuf-net),并使用 dll 中的序列化程序类型与您的模型进行交互:

This generates a serializer dll; simply reference this dll (along with the iOS version protobuf-net), and use the serializer type in the dll to interact with your model:

var ser = new MySerializer();
ser.Serialize(dest, obj); // etc

这篇关于protobuf-net v2 和 Monotouch:它是如何混合的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆