Telerik RadPersistenceManager 无法读取存储内容

Telerik RadPersistenceManager Unable to read storage content

我已经将 RadPersistenceManager 与 asp.net 一起使用,并遵循以下指南:

http://demos.telerik.com/aspnet-ajax/persistence-framework/examples/custom-storage-provider/defaultcs.aspx.

但是当我在我的项目中实现这个时,出现了以下异常:

Title: xxxx.aspx, xxxxx Method Name: Unable to read storage content. Could not find file 'C:\inetpub\wwwroot\XXXX\App_Data\TelerikAspNetRadControlsPersistedState'.

Exception: at Telerik.Web.UI.PersistenceFramework.AppDataStorageProvider.LoadStateFromStorage(String key) at Telerik.Web.UI.RadPersistenceManager.LoadState() at GraphicalUserInterface.JobBasket.LoadGridJobBasket()

这是默认的存储提供商密钥。如果控件正在寻找它,那么您的自定义提供程序根本没有生效。

确保您拥有演示中的所有内容,主要是:

设置自定义存储提供程序的行:

protected void Page_Init(object sender, EventArgs e)
{
    RadPersistenceManager1.StorageProviderKey = CookieName;
    RadPersistenceManager1.StorageProvider = new CookieStorageProvider(CookieName);
}

和自定义存储提供程序本身:

using System;
using System.Linq;
using System.Web;
using Telerik.Web.UI.PersistenceFramework;
using System.IO;
using System.IO.Compression;
using System.Text;

public class CookieStorageProvider : IStateStorageProvider
{
    private static readonly Encoding AsciiEncoding = System.Text.Encoding.ASCII;
    private static readonly int MaxCookieSize = 4000;
    private static readonly int LengthDataByteCount = sizeof(Int32);
    private string StorageKey { get; set; }

    #region IStateStorageProvider

    public CookieStorageProvider(string key)
    {
        StorageKey = key;
    }

    public void SaveStateToStorage(string key, string serializedState)
    {
        HttpCookie cookie = new HttpCookie(StorageKey);
        string settingsData = CompressString(serializedState);

        if (settingsData.Length > MaxCookieSize)
        {
            throw new ArgumentOutOfRangeException("Current settings exceed 4k in compressed form! Operation canceled!");
        }

        cookie.Value = settingsData;

        HttpContext.Current.Response.Cookies.Add(cookie);
    }

    public string LoadStateFromStorage(string key)
    {
        return DecompressString(HttpContext.Current.Request.Cookies[StorageKey].Value.ToString());
    }

    #endregion

    private string CompressString(string inputString)
    {
        byte[] outputBytes = null;
        byte[] inputBytes = AsciiEncoding.GetBytes(inputString);

        using (MemoryStream ms = new MemoryStream())
        {
            using (GZipStream zipStream = new GZipStream(ms, CompressionMode.Compress))
            {
                zipStream.Write(inputBytes, 0, inputBytes.Length);
            }
            outputBytes = ms.ToArray();
        }

        return Convert.ToBase64String(AddDataCount(outputBytes, inputBytes.Length));
    }

    private string DecompressString(string inputString)
    {
        string outputString = String.Empty;
        byte[] inputBytes = Convert.FromBase64String(inputString);
        Int32 lengthDataArray = BitConverter.ToInt32(inputBytes, inputBytes.Length - LengthDataByteCount);
        byte[] outputBytes = new byte[lengthDataArray];

        using (MemoryStream ms = new MemoryStream(RemoveDataCount(inputBytes)))
        {
            using (GZipStream zipStream = new GZipStream(ms, CompressionMode.Decompress))
            {
                zipStream.Read(outputBytes, 0, outputBytes.Length);
            }
            outputString = AsciiEncoding.GetString(outputBytes);
        }

        return outputString;
    }

    private byte[] AddDataCount(byte[] inputArray, Int32 length)
    {
        byte[] lengthDataArray = BitConverter.GetBytes(length);
        Array.Resize<byte>(ref inputArray, inputArray.Length + LengthDataByteCount);
        Array.Copy(lengthDataArray, 0, inputArray, inputArray.Length - LengthDataByteCount, LengthDataByteCount);
        return inputArray;
    }

    private byte[] RemoveDataCount(byte[] inputArray)
    {
        Array.Resize<byte>(ref inputArray, inputArray.Length - LengthDataByteCount);
        return inputArray;
    }
}

演示和我的示例之间的不同之处在于我需要在页面加载而不是加载按钮上加载持久性配置。所以有时 CookieStorageProvider 似乎没有在 Page_Init 中启动,所以我在 page_load 中添加了它。