2012-05-17 28 views
0

我使用下面的代码做Azure的缓存读取蔚蓝缓存数据,它工作正常...无法从不同的应用程序

[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.Seriali zation.SerializationException:未找到Assembly'AzureCaching1,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null'。

我的课人是可序列化的。为什么我无法查询缓存在AzureCaching1中的缓存?

请帮忙。谢谢

回答

2

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

由于您正在编写测试,因此您需要从AzureCachingTests所在的项目中引用AzureCachingTests1所在的项目,并删除AzureCachingTests项目中Person的类定义(并确保您正在投射正确的课程也是如此)。

如果这不是一个测试,你只是想在两个或多个项目之间共享一个类,最好的办法是让第三个项目包含所有这两个类都通用的类,在这种情况下它只是Person类。