【问题标题】:unable to read azure cached data from a different application无法从其他应用程序读取 azure 缓存数据
【发布时间】:2012-05-24 14:42:28
【问题描述】:

我正在使用以下代码进行 azure-caching,它工作正常...

[Serializable]
public class Person
{
    public string First { set; get; }
    public string Last { set; get; }

}

[TestClass]
public class AzureCachingTests1
{
    private static DataCacheFactory _factory;

    [TestInitialize]
    public void Setup()
    {

        _factory = new DataCacheFactory(new DataCacheFactoryConfiguration("mycache"));
    }


    [TestMethod]
    public void TestMethod1()
    {

        DataCache cache = _factory.GetDefaultCache();

        var person = new Person { First = "Jane", Last = "doe" };
        const string key = "mykey";

        cache.Put(key, person, TimeSpan.FromMinutes(10));
        var data = (Person)cache.Get(key);
        Assert.AreEqual("Jane", data.First);

    }
}

现在在 Visual Studio 的另一个实例中,我运行以下代码...

[TestClass]
public class AzureCachingTests
{
    private static DataCacheFactory _factory;
    [TestInitialize]
    public void Setup()
    {

        _factory = new DataCacheFactory(new DataCacheFactoryConfiguration("mycache"));
    }


    [TestMethod]
    public void TestMethod1()
    {

        DataCache cache = _factory.GetDefaultCache();
        const string key = "mykey";
        var data = (Person) cache.Get(key);  <----- Error here... <--------
        Assert.AreEqual("Jane", data.First);

    }
}

[Serializable]
public class Person
{
    public string First { set; get; }
    public string Last { set; get; }

}

这一次,我收到以下错误...

测试方法 AzureCaching.AzureCachingTests.TestMethod1 抛出异常: System.Runtime.Serialization.SerializationException:找不到程序集“AzureCaching1,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null”。

我的班级 Person 是可序列化的。为什么我在 AzureCaching1 中的缓存无法查询到 AzureCaching 中的缓存??

请帮忙。谢谢

【问题讨论】:

    标签: c# caching azure


    【解决方案1】:

    除非您引用了 AzureCachingTests1 所在的项目,否则您的测试不知道如何反序列化它从缓存中检索的项目。您有两个具有相同名称和相同属性的类,但它们不是同一个类。

    因为您正在编写测试,所以您需要从 AzureCachingTests 所在的项目中引用项目 AzureCachingTests1 并删除 AzureCachingTests 项目中 Person 的类定义(并确保您正在转换为正确的类也是如此)。

    如果这不是一个测试,而您只是想在两个或多个项目之间共享一个类,最好的办法是拥有第三个项目,其中包含两个项目共有的所有类,在这种情况下,它只是人员类。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-03
      • 2021-12-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多