如何在Azure AppFabric缓存中将类用作列表集合

如何在Azure AppFabric缓存中将类用作列表集合,azure,Azure,H,, 我正在使用Windows Azure AppFabri缓存。 我有两个asp.NET项目。 一个用于将数据放入缓存,另一个用于读取该缓存。这两个项目在两个不同的系统上运行。 我有4个dll包括在这些项目中 Microsoft.WindowsFabric.Common.dll Microsoft.WindowsFabric.Data.Common.dll Microsoft.ApplicationServer.Caching.Client.dll Microsoft.Application

H,, 我正在使用Windows Azure AppFabri缓存。 我有两个asp.NET项目。 一个用于将数据放入缓存,另一个用于读取该缓存。这两个项目在两个不同的系统上运行。 我有4个dll包括在这些项目中

Microsoft.WindowsFabric.Common.dll
Microsoft.WindowsFabric.Data.Common.dll
Microsoft.ApplicationServer.Caching.Client.dll
Microsoft.ApplicationServer.Caching.Core.dll
项目1:要在缓存中插入数据,请使用以下代码:数据已成功保存到缓存

Default.aspx.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.ApplicationServer.Caching;

public partial class Default2 : System.Web.UI.Page
{
    public class Employee
    {
        public string Name { get; set; }
        public decimal Salary { get; set; }

    }
    protected static DataCacheFactory _factory = null;
    protected static DataCache _cache = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        PutDataIntoCache();

    }
    protected void PutDataIntoCache()
    {
        List<Employee> emp = new List<Employee>();
        try
        {

            emp.Add(new Employee { Name = "James", Salary = 20000 });
            PutCacheItem("55", emp);

        }
        catch (Exception ex)
        {

        }
    }
    protected void PutCacheItem(string key, object value)
    {

        try
        {
            _cache = GetDefaultCache();
            _cache.Put(key, value);
        }
        catch (Exception ex)
        {


        }
    }
    protected static DataCache GetDefaultCache()
    {
        _factory = new DataCacheFactory();
        _cache = _factory.GetDefaultCache();
        return _cache;

    }

}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.ApplicationServer.Caching;

public partial class Default2 : System.Web.UI.Page
{
    protected static DataCacheFactory _factory = null;
    protected static DataCache _cache = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        GetDataFromCache();

    }
    protected void GetDataFromCache()
    {
        _cache = GetDefaultCache();
        string key = "55";
        var item = _cache.Get(key);// Error is generation here
    }

    protected static DataCache GetDefaultCache()
    {
        _factory = new DataCacheFactory();
        _cache = _factory.GetDefaultCache();
        return _cache;

    }

}
在行_cache.Get(key)上生成错误

为什么会出现这种错误?
如何使用类作为列表集合在缓存中添加/读取。我认为问题在于您的第二个项目没有定义Employee类。您应该定义一个包含Employee的类库,并引用两个项目中相同的程序集。

按照您设置这些项目的方式,我认为ASP正在将这些代码隐藏页编译成临时程序集-即Employee类被定义为位于程序集
App\u Web\u 4xzswv4j,Version=0.0.0,区域性=中性,PublicKeyToken=空

Microsoft.WindowsFabric.Common.dll
Microsoft.WindowsFabric.Data.Common.dll
Microsoft.ApplicationServer.Caching.Client.dll
Microsoft.ApplicationServer.Caching.Core.dll
相反,您应该将这些类放入一个类库中,该类库可以在项目之间共享——这样每个编译都将有一个固定的名称,而不是唯一的名称


另外,我认为您还应该将您的员工标记为可序列化,以确保它可以序列化和反序列化