0

私は AppFabric キャッシュを試していますが、取得したデータをキャッシュに保存する際に問題が発生しました。問題の根本は、AppFabric キャッシュでは、データに DataContract 属性と Datamember 属性を保存するクラスに適用する必要があるように思われることです。

その場合、これを保存するにはどうすればよいですか (簡略化されたコード):

var q = (from u in dbContext.Users
                         select new
                         {
                             Name = u.Name,
                             Id = u.RowId
                         });

cache.Put(“test”, q.ToList());

Put を呼び出すと、次の例外が発生します。

System.Runtime.Serialization.InvalidDataContractException was caught
 Message=Type '<>f__AnonymousTypec`6[System.Int32,System.String,System.Nullable`1[System.Int32],System.Boolean,System.Nullable`1[System.Int32],System.Int32]' cannot 
be serialized. Consider marking it with the DataContractAttribute attribute, and 
marking all of its members you want serialized with the DataMemberAttribute 
attribute.  If the type is a collection, consider marking it with the CollectionDataContractAttribute.  See the Microsoft .NET Framework 
documentation for other supported types.

AppFabric がキャッシュできるように IQueryable の結果をシリアル化するにはどうすればよいですか?

ありがとうございました、

リック

4

1 に答える 1

1

IQueryable を実行しようとしているのではなく、型が Anonymous であるということです。結果のクラスを作成してから、保存するクラスの 1 つを作成してみてください。

[Serializable]
public class UserIDPair
{
    public string Name {get;set;}
    public int ID {get;set;}
}

var q = (from u in dbContext.Users
    select new UserIDPair
    {
        Name = u.Name,
        Id = u.RowId
    });

cache.Put(“test”, q.ToList());

クラスがシリアライズ可能であることを確認してください

于 2010-10-07T23:11:42.293 に答える