首页 新闻 会员 周边 捐助

wcf服务库和服务应用程序问题~

0
悬赏园豆:20 [待解决问题]

我用wcf服务库 从网上摘了一个 压缩数据流的方法,运行起来都没问题。

  后来我把这个方法 搬到 服务应用程序中,死活都获取不到当前的数据流,压缩的时候数据流都有,在客户端 获取当前数据流的时候就成null了。 是不是和托管有关?

服务端代码:

 

    /// <summary>
        /// 无参数构造函数
        /// </summary>
        ///
        public Service1()
        {
            InitBuffers(InitTestDataSet());
        }
        private byte[] buffer_all = null;
        private byte[] buffer_currect = null;
        private int get_buffer_length = 1000;
        private long remain_length;
        private MemoryStream stream;

        public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }


        private DataSet InitTestDataSet()
        {
            DataSet ds = new DataSet("test");
            DataTable table = new DataTable("test");
            DataColumn column = new DataColumn("test");
            column.DataType = Type.GetType("System.String");
            table.Columns.Add(column);
            DataRow row;
            for (int i = 0; i < 100000; i++)
            {
                row = table.NewRow();
                row["test"] = "测试数据 !";
                table.Rows.Add(row);
            }

            ds.Tables.Add(table);

            return ds;

        }

        private void InitBuffers(DataSet ds)
        {

            IFormatter formatter = new BinaryFormatter();
            MemoryStream stream_ = new MemoryStream();
            formatter.Serialize(stream_, ds);
            buffer_all = stream_.ToArray();
            stream_.Close();
            byte[] bytes_c = Compression(buffer_all, CompressionMode.Compress);
            stream = new MemoryStream(bytes_c);
            stream.Position = 0;
            remain_length = stream.Length;


        }

        private byte[] Compression(byte[] data, CompressionMode mode)
        {
            DeflateStream zip = null;
            try
            {
                if (mode == CompressionMode.Compress)
                {
                    MemoryStream ms = new MemoryStream();
                    zip = new DeflateStream(ms, mode, true);
                    zip.Write(data, 0, data.Length);
                    zip.Close();
                    return ms.ToArray();
                }
                else
                {
                    MemoryStream ms = new MemoryStream();
                    ms.Write(data, 0, data.Length);
                    ms.Flush();
                    ms.Position = 0;
                    zip = new DeflateStream(ms, mode, true);
                    MemoryStream os = new MemoryStream();
                    int SIZE = 1024;
                    byte[] buf = new byte[SIZE];
                    int l = 0;
                    do
                    {
                        l = zip.Read(buf, 0, SIZE);
                        if (l == 0) l = zip.Read(buf, 0, SIZE);
                        os.Write(buf, 0, l);
                    } while (l != 0);
                    zip.Close();
                    return os.ToArray();
                }
            }
            catch
            {
                if (zip != null) zip.Close();
                return null;
            }
            finally
            {
                if (zip != null) zip.Close();
            }
        }


        #region IDataTransfers 成员
        /// <summary>
        /// 获取所有字节流
        /// </summary>
        /// <returns></returns>
        public byte[] GetAllBuffer()
        {
            if (buffer_all != null)
                return buffer_all;
            else return null;
        }
        /// <summary>
        /// 设置压缩后字节流分块,每一块的大小
        /// </summary>
        /// <param name="length"></param>
        public void SetBufferLength(int length)
        {
            this.get_buffer_length = length;
        }
        /// <summary>
        /// 读取压缩后字节流一块,并提升字节流的位置
        /// </summary>
        /// <returns></returns>
        public bool ReadNextBuffer()
        {
            bool bo;
            if (remain_length > 0)
            {
                if (remain_length > get_buffer_length)
                {
                    buffer_currect = new byte[get_buffer_length];

                    stream.Read(buffer_currect, 0, get_buffer_length);
                    remain_length -= get_buffer_length;
                }
                else
                {
                    buffer_currect = new byte[remain_length];
                    stream.Read(buffer_currect, 0, (int)remain_length);
                    remain_length = 0;
                }

                bo = true;
            }
            else
                bo = false;
            return bo;

        }
        /// <summary>
        /// 获取当前块的字节流
        /// </summary>
        /// <returns></returns>
        public byte[] GetCurrentBuffer()
        {
            if (buffer_currect != null)            //问题就在这个地方,此处如果在服务库中式可以的 我移到 服务应用程序里 在下面客户端调用就成null 了

                return buffer_currect;
            else
                return null;

        }

        #endregion

 

 

客户端

 

    WcfTest.Service1Client client = new WcfTest.Service1Client();
            MemoryStream stream = new MemoryStream();
            byte[] buffer;
            //获取所用块压缩流,并组装
            while (client.ReadNextBuffer())
            {
                buffer = client.GetCurrentBuffer();   //此处就为null了
                stream.Write(buffer, 0, buffer.Length);
            }
            stream.Position = 0;
            buffer = new byte[stream.Length];
            stream.Read(buffer, 0, buffer.Length);
            stream.Close();
            //解压压缩流
            byte[] bytes = Compression(buffer, CompressionMode.Decompress);
            stream = new MemoryStream(bytes);
            IFormatter formatter = new BinaryFormatter();
            //反序列化
            DataSet ds = (DataSet)formatter.Deserialize(stream);
            stream.Close();
            this.dataGridView1.DataSource = ds;
            this.dataGridView1.DataMember = "test";
            this.label1.Text = ds.Tables[0].Rows.Count.ToString();
            client.Close();

 

        }
        private byte[] Compression(byte[] data, CompressionMode mode)
        {
            DeflateStream zip = null;
            try
            {
                if (mode == CompressionMode.Compress)
                {
                    MemoryStream ms = new MemoryStream();
                    zip = new DeflateStream(ms, mode, true);
                    zip.Write(data, 0, data.Length);
                    zip.Close();
                    return ms.ToArray();
                }
                else
                {
                    MemoryStream ms = new MemoryStream();
                    ms.Write(data, 0, data.Length);
                    ms.Flush();
                    ms.Position = 0;
                    zip = new DeflateStream(ms, mode, true);
                    MemoryStream os = new MemoryStream();
                    int SIZE = 1024;
                    byte[] buf = new byte[SIZE];
                    int l = 0;
                    do
                    {
                        l = zip.Read(buf, 0, SIZE);
                        if (l == 0) l = zip.Read(buf, 0, SIZE);
                        os.Write(buf, 0, l);
                    } while (l != 0);
                    zip.Close();
                    return os.ToArray();
                }
            }
            catch
            {
                if (zip != null) zip.Close();
                return null;
            }
            finally
            {
                if (zip != null) zip.Close();
            }
        }

 

挺郁闷的, 实在找不到哪里出了问题,因为在调试的时候 读取压缩的时候都是有数据的,在获取当前数据流的时候就成空了! 求大侠们指点

伊啊宋的主页 伊啊宋 | 初学一级 | 园豆:122
提问于:2014-04-03 16:08
< >
分享
所有回答(1)
0

代码太多了,而且没啥备注,实在看不下去了...

读取压缩的时候都是有数据的,在获取当前数据流的时候就成空了!

是不是因为你读取压缩时候的流跟你当前的流不是同一个流?

seaconch | 园豆:4823 (老鸟四级) | 2014-04-03 18:13
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册