WCF Web服务返回List< Object>的压缩字节数组.具有空值 [英] WCF WebService returns Zipped byte array of List<Object> with null values

查看:54
本文介绍了WCF Web服务返回List< Object>的压缩字节数组.具有空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在测试我编写的wcf WebService,我有一个私有函数,该函数返回一个List< Object> ;,然后将其序列化为MemoryStream,然后将其压缩并返回一个压缩的序列化List< Object>.应用程序,我解压缩字节数组, 解压缩并反序列化,得到List< Object>但是所有对象都具有空值

   

I'm testing a wcf WebService I wrote, I have a private function that returns a List<Object>, then I Serialize it to a MemoryStream then I Zip this and Return a Zipped Serialized List<Object>, on the Client Application, I Unzipp the byte array, unzip it, and Deserialize it, I get a List<Object> but all objects have null values

   

 IService
    
    [ServiceContract]
    public interface IService
    {
        [OperationContract]
        Task<byte[]> ProductosList();
        
    }
    
    Service       
    
        async Task<byte[]> Serializar(dynamic[] _list)
        {
            MemoryStream _sw = new MemoryStream();
    
            XmlSerializer _ser = new XmlSerializer(_list.GetType());
            _ser.Serialize(_sw, _list);
    
            _ser = null;
    
            var _result = await ZipBytes(_sw.ToArray());
    
            _sw.Flush();
            _sw.Close();
            _sw.Dispose();
            
            return _result;
        }
    
        public async Task<byte[]> ProductosList()
        {
            ConcurrentBag<clsProducto> _result = new ConcurrentBag<clsProducto>();
    
            try
            {
                string _empresa = @"E:\Compacw\Empresas\VENECIA3";
                string _connString = string.Format(@"Provider=vfpoledb;Data Source={0};Collating Sequence=general;", _empresa);
    
                string _query = "SELECT ccodigop01, cnombrep01, cprecio1, cprecio2 FROM MGW10005 WHERE ctipopro01 = 1 AND cstatusp01 = 1";
    
                using (OleDbConnection _conn = new OleDbConnection(_connString))
                {
                    using(OleDbCommand _comm = new OleDbCommand(_query, _conn))
                    {
                        if(_conn.State == System.Data.ConnectionState.Closed) await _conn.OpenAsync();
    
                        var _dr = await _comm.ExecuteReaderAsync();
                        await _dr.ReadAsync();
                        do
                        {
    
                            _result.Add(new clsProducto()
                            {
                                Codigo = _dr.GetValue(0).ToString().Trim().ToUpper(),
                                Nombre = _dr.GetValue(1).ToString().Trim().ToUpper(),
                                Precio1 = decimal.Parse(_dr.GetValue(2).ToString()),
                                Precio2 = decimal.Parse(_dr.GetValue(3).ToString())
                            });
    
                        } while (await _dr.ReadAsync());
    
                        _dr.Close();
                        _dr = null;
    
                        _comm.Dispose();
    
                        if (_conn.State == System.Data.ConnectionState.Open) _conn.Close();
                        _conn.Close();
                        _conn.Dispose();
                    }
                }
    
            }
            catch (Exception ex)
            {
                _result.Add(new clsProducto()
                {
                    Codigo = "0",
                    Nombre = ex.Message
                });
            }
    
            return await Serializar(_result.ToArray());
        }
    
        private async Task<byte[]> ZipBytes(byte[] _data)
        {
            byte[] _result;
            using (MemoryStream outStream = new MemoryStream())
            {
                using (GZipStream gzipStream = new GZipStream(outStream, CompressionMode.Compress))
                using (MemoryStream srcStream = new MemoryStream(_data))
                    await srcStream.CopyToAsync(gzipStream);
                _result = outStream.ToArray();
            }
            return _result;
        }




客户

   




Client

   

public async Task<List<wcfService.clsProducto>> DeSerializar(byte[] _data)
        {
            MemoryStream _sr = new MemoryStream(_data);
            
            XmlSerializer _ser = new XmlSerializer(typeof(List<wcfService.clsProducto>));
            var _result2 = _ser.Deserialize(_sr);
    
            _ser = null;
            
            _sr.Close();
            _sr.Dispose();
    
            return await Task.FromResult((List<wcfService.clsProducto>)_result2);
        }
    
        private async Task<byte[]> UnZipBytes(byte[] _compressed)
        {
            byte[] _result;
            using (MemoryStream inStream = new MemoryStream(_compressed))
            using (GZipStream gzipStream = new GZipStream(inStream, CompressionMode.Decompress))
            using (MemoryStream outStream = new MemoryStream())
            {
                await gzipStream.CopyToAsync(outStream);
                _result = outStream.ToArray();
            }
            return _result;
        }
    
        async Task<List<wcfService.clsProducto>> ServicioWcf()
        {
            var _service = new wcfService.ServiceClient();
            var _result = await _service.ProductosListAsync();
    
            var _datos = await UnZipBytes(_result);
    
            return await DeSerializar(_datos);
        }



如果我修改函数以返回通用列表,则结果是正确的,但是我想使数据传输更小,更快,因为有时列表很大.



If I modify the Function to return the Generic List, the result is correct, but I want to make the data transfer smaller and faster because sometimes the list is very big.

推荐答案

对象不是类型.我认为您不能只序列化一个对象.也许,您应该使用Json查看一个WebAPI.
Object is not a type. I don't think you can just serialize an Object. Maybe, you should look at a WebAPI using Json.


这篇关于WCF Web服务返回List&lt; Object&gt;的压缩字节数组.具有空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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