Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 使用实体框架返回数据实现Protobuf net v2_.net_Linq_Entity Framework_Protocol Buffers_Protobuf Net - Fatal编程技术网

.net 使用实体框架返回数据实现Protobuf net v2

.net 使用实体框架返回数据实现Protobuf net v2,.net,linq,entity-framework,protocol-buffers,protobuf-net,.net,Linq,Entity Framework,Protocol Buffers,Protobuf Net,有人能指出我下面的源代码有什么问题吗 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 CCBProdu

有人能指出我下面的源代码有什么问题吗

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

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

在哪里定义GetNestleData_结果?这是你自己的代码吗?或者这是来自EF代码生成器?还是

基本上,protobuf net希望使用声明数据协定的类型,以便用于序列化。特别是,因为protobuf妻子格式不包括成员名,而只包括字段号,所以它需要一种知道1===Name等的方法。有多种方法可以做到这一点。如果类型是您自己的代码,最简单的方法是添加属性-它将识别一些模式,例如:

[ProtoContract]
public class GetNestleData_Result {
    [ProtoMember(1)]
    public int SomeValue {get;set;}

    [ProtoMember(2)]
    public List<Foo> SomeMoreValues {get;set;}

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

RuntimeTypeMode.Default.Add(typeof(GetNestleData_Result), false)
                       .Add("SomeValue", "SomeMoreValues");
这将做同样的事情


一旦序列化程序知道您的类型应该如何配置,它就会工作。

谢谢您,Marc,您的响应非常快: 我通过修饰实体框架模型生成的类来实现:

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

不过,我还有3个与protobuf相关的问题:I。在获得对象的流形式之后,如果我想使用Web服务,我如何对其进行编码以使流可以使用?XML编码?二,。我试图再次通过额外的压缩将protobuf格式的数据从控制台发送到Web服务,这是必要的吗?所以我每次都要对类文件做这个修饰,以便protobuf序列化工作?没有快捷方式或更干净的代码方式?omg,反序列化不再工作,我按照相同的步骤返回,返回的特定类型的列表具有正确的长度,但所有的零/空值,有任何可能的原因吗?请帮忙!
RuntimeTypeMode.Default.Add(typeof(GetNestleData_Result), false)
                       .Add("SomeValue", "SomeMoreValues");
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; }
}