日期:2014-05-18  浏览次数:20492 次

压缩ViewState后,在Convert.FromBase64String时偶尔会发生FormatException
压缩ViewState后,在Convert.FromBase64String时偶尔会发生FormatException,请教是什么原因?以下是BasePage中的相关代码
C# code

        private bool viewStateCompress = false;

        protected bool ViewStateCompress
        {
            set { viewStateCompress = value; }
            get { return viewStateCompress;}
        }

        protected override void SavePageStateToPersistenceMedium(object state)
        {
            if (!viewStateCompress)
            { 
                base.SavePageStateToPersistenceMedium(state);
            }
            else
            {
                Pair pair;
                byte[] data;
                byte[] compressedData;
                string compressedStr;
                object viewState = null, controlState = null;

                LosFormatter formatter = new LosFormatter();
                commonUtil.CompressHelper CH = commonUtil.CompressHelper.getInstance();
                PageStatePersister persister = this.PageStatePersister;

                if (state is Pair)
                {
                    pair = (Pair)state;
                    viewState = pair.First;
                    controlState = pair.Second;
                }
                else
                {
                    base.SavePageStateToPersistenceMedium(state);
                    return;
                }

                StringWriter wViewState = new StringWriter();
                StringWriter wControlState = new StringWriter();

                formatter.Serialize(wViewState, viewState);
                string viewStateStr = wViewState.ToString();
                data = Convert.FromBase64String(viewStateStr);
                compressedData = CH.Compress(data);
                compressedStr = Convert.ToBase64String(compressedData);
                persister.ViewState = compressedStr;

                formatter.Serialize(wControlState, controlState);
                string controlStateStr = wControlState.ToString();
                data = Convert.FromBase64String(controlStateStr);
                compressedData = CH.Compress(data);
                compressedStr = Convert.ToBase64String(compressedData);
                persister.ControlState = compressedStr;

                persister.Save();
            }
        }

        protected override object LoadPageStateFromPersistenceMedium()
        {
            if (!viewStateCompress)
            { 
                return base.LoadPageStateFromPersistenceMedium();
            }
            else
            {
                byte[] data;
                byte[] uncompressedData;
                string decompressedStr;
                object viewState, controlState;

                LosFormatter formatter = new LosFormatter();
                commonUtil.CompressHelper CH = commonUtil.CompressHelper.getInstance();
                PageStatePersister persister = this.PageStatePersister;
                persister.Load();

                string viewStateStr = persister.ViewState.ToString();
                string controlStateStr = persister.ControlState.ToString();
                try
                {
                    data = Convert.FromBase64String(viewStateStr);
                    uncompressedData = CH.Decompress(data);
                    decompressedStr = Convert.ToBase64String(uncompressedData);
                    viewState = formatter.Deserialize(decompressedStr);

                    data = Convert.FromBase64String(controlStateStr);
                    uncompressedData = CH.Decompress(data);
                    decompressedStr = Convert.ToBase64String(uncompressedData);
                    controlState = formatter.Deserialize(decompressedStr);
                }
                catch(Exception exp)
                { 
                    viewState = null;
                    controlState = n