【问题标题】:Implement Protobuf net v2 with entity framework return data使用实体框架返回数据实现 Protobuf net v2
【发布时间】:2013-05-30 04:02:32
【问题描述】:

谁能指出我下面的源代码有什么问题:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Net;
using System.IO.Compression;
using System.Web.Services.Protocols;
using CCBProductionEntityModel;
using ConsoleDebug.NestleWebReference;
using ProtoBuf;

namespace ConsoleDebug
{
    class Program
    {

        static void Main(string[] args)
        {
            //This is where I get data from and store into a list
            CCBProductionEntities et = new CCBProductionEntities();
            List<GetNestleData_Result> results = et.GetNestleData(5).ToList();

            List<GetNestleData_Result> responseResults;

            using (var stream = new MemoryStream())
            {
                Serializer.Serialize(stream, results);

                using (var responseStream = new MemoryStream())
                {
                    responseResults = Serializer.Deserialize<List<GetNestleData_Result>>(responseStream);
                }
            }

            results = responseResults;

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < results.Count(); i++)
            {
                var result = results.ElementAt(i);
                sb.Append(result.AssetId + "\t" + result.DeviceId + "\t" + result.InstanceNumber + "\t" + result.Speed + "\t" + result.Heading + "\t" + result.DriverId + "\t" + result.PositionAge + "\t" + result.VehicleRegistrationNum + "\t" + result.GSMAddress + "\t" + result.Odometer + "\t" + result.Latitude + "\t" + result.Longitude + "\t" + result.Altitude + "\t" + result.IgnitionState);
            }
            Console.WriteLine(sb.ToString());
            Console.ReadLine();
        }
    }
}

尝试运行时,我收到错误消息: 类型不是预期的,也无法推断出合约:CCBProductionEntityModel.GetNestleData_Result

我尝试到处搜索例如源代码,但很难找到一个好的和干净的,在使用序列化程序之前我必须定义一些东西吗?如何?谢谢!

【问题讨论】:

    标签: .net linq entity-framework protocol-buffers protobuf-net


    【解决方案1】:

    GetNestleData_Result 在哪里定义?这是你自己的代码吗?还是来自 EF 代码生成器?还是……?

    基本上,protobuf-net 希望使用声明可用于序列化的数据协定的类型。特别是由于protobuf的wife格式不包含成员名(而只是字段号),所以需要一种知道1===Name等的方式。这样做的方法。最简单的,如果类型是你自己的代码,就是添加属性——它会识别一些模式,例如:

    [ProtoContract]
    public class GetNestleData_Result {
        [ProtoMember(1)]
        public int SomeValue {get;set;}
    
        [ProtoMember(2)]
        public List<Foo> SomeMoreValues {get;set;}
    
        // etc
    }
    

    [DataContract]
    public class GetNestleData_Result {
        [DataMember(Order=1)]
        public int SomeValue {get;set;}
    
        [DataMember(Order=2)]
        public List<Foo> SomeMoreValues {get;set;}
    
        // etc
    }
    

    如果您正在使用代码生成,有时代码生成工具会很乐意添加DataContract/DataMember(用于 WCF 目的);但如果不是,则不希望编辑生成的文件。在这种情况下,还支持在单独的代码文件中的 partial 类中执行此操作 - 然后编译器会为您拼接在一起:

    [ProtoContract]
    [ProtoPartialMember(1, "SomeValue")]
    [ProtoPartialMember(2, "SomeMoreValues")]
    partial class GetNestleData_Result {}
    

    但是,如果该类型完全超出您的控制范围,并且不包含任何可用于确定顺序的属性,您还可以在代码中配置所有内容:

    RuntimeTypeMode.Default.Add(typeof(GetNestleData_Result), false)
                           .Add("SomeValue", "SomeMoreValues");
    

    它会做同样的事情。

    一旦序列化器知道你的类型应该如何配置,它就应该工作了。

    【讨论】:

    • 我还有 3 个与 protobuf 相关的问题:i。在我获得对象的流形式后,如果我想消费到 Web 服务,我如何对其进行编码以使流是可消费的? XML 编码? ii.我试图通过额外的压缩再次将 protobuf 格式的数据从控制台发送到 Web 服务,这有必要吗? iii.所以我每次都必须对“类”文件进行这种装饰,以使 protobuf 序列化工作?没有快捷方式或更简洁的代码方式?
    【解决方案2】:

    谢谢你,马克,你的反应非常快:) 我通过装饰从实体框架模型生成的类来实现它:

    public partial class GetNestleData_Result
    {
        [ProtoMember(1)]
        public Nullable<int> AssetId { get; set; }
        [ProtoMember(2)]
        public long DeviceId { get; set; }
        [ProtoMember(3)]
        public Nullable<int> EventTime { get; set; }
        [ProtoMember(4)]
        public Nullable<short> InstanceNumber { get; set; }
        [ProtoMember(5)]
        public Nullable<short> Speed { get; set; }
        [ProtoMember(6)]
        public Nullable<short> Heading { get; set; }
        [ProtoMember(7)]
        public Nullable<int> DriverId { get; set; }
        [ProtoMember(8)]
        public Nullable<short> PositionAge { get; set; }
        [ProtoMember(9)]
        public string VehicleRegistrationNum { get; set; }
        [ProtoMember(10)]
        public string GSMAddress { get; set; }
        [ProtoMember(11)]
        public Nullable<int> Odometer { get; set; }
        [ProtoMember(12)]
        public Nullable<int> Latitude { get; set; }
        [ProtoMember(13)]
        public Nullable<short> Altitude { get; set; }
        [ProtoMember(14)]
        public Nullable<int> Longitude { get; set; }
        [ProtoMember(15)]
        public string IgnitionState { get; set; }
    }
    

    【讨论】:

    • omg,反序列化不再起作用,我遵循相同的步骤,返回具有正确长度但所有零/空值的某种类型的列表,这有什么可能的原因吗?请帮忙!
    猜你喜欢
    • 2014-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-02
    • 1970-01-01
    • 2019-05-09
    • 1970-01-01
    相关资源
    最近更新 更多