1

次のオブジェクトがあります。

 [Serializable]
    public class Module
    {
        [Key]
        public int Id { get; set; }
        public string ModuleName { get; set; }
        public string FontAwesomeClass { get; set; }
    }

   [Serializable]
    public class ModulosPorUsuario
    {
        [Key]
        public int Id { get; set; }
        public string Email { get; set; }
        public virtual ICollection<Module> Modules{ get; set; }
    }

そして、このメソッドを使用して、割り当てられたモジュールごとに特定のキーをキャッシュしています。

 /// <summary>
        /// Gets the modules activated for a user
        /// </summary>
        /// <param name="email">Email address of the user</param>
        /// <returns>List of modules for the selected user</returns>
        public static List<Models.ModulosPorUsuario> GetModulesForUser(string identityname)
        {
            /// It needs to be cached for every user because every user can have different modules enabled.
            var cachekeyname = "ApplicationModulesPerUser|" + identityname;

            IDatabase cache = CacheConnectionHelper.Connection.GetDatabase();
            List<Models.ModulosPorUsuario> listOfModulesPerUser = new List<Models.ModulosPorUsuario>();
            listOfModulesPerUser = (List<Models.ModulosPorUsuario>)cache.Get(cachekeyname);
            if (listOfModulesPerUser == null)
            {
                listOfModulesPerUser = dbApp.ModulosPorUsuario.Where(p => p.Email == identityname).ToList();
                cache.Set(cachekeyname, listOfModulesPerUser, TimeSpan.FromMinutes(SettingsHelper.CacheModuleNames));
                return listOfModulesPerUser;
            }
            else
            {
                return listOfModulesPerUser;
            }
        }

初めて機能するときはもちろん、DBからデータを返します。しかし、2 回目 (キャッシュにオブジェクトがあります) ですが、モジュールのリストは null です。

http://screencast.com/t/OFB1vkvno

また、stackexchange redis キャッシュからのこのヘルパー クラスもあります。

public static class SampleStackExchangeRedisExtensions
    {
        public static T Get<T>(this IDatabase cache, string key)
        {
            return Deserialize<T>(cache.StringGet(key));
        }

        public static object Get(this IDatabase cache, string key)
        {
            return Deserialize<object>(cache.StringGet(key));
        }

        public static void Set(this IDatabase cache, string key, object value, TimeSpan expiration)
        {
            cache.StringSet(key, Serialize(value), expiration);
        }

        static byte[] Serialize(object o)
        {
            if (o == null)
            {
                return null;
            }
            BinaryFormatter binaryFormatter = new BinaryFormatter();
            using (MemoryStream memoryStream = new MemoryStream())
            {
                binaryFormatter.Serialize(memoryStream, o);
                byte[] objectDataAsStream = memoryStream.ToArray();
                return objectDataAsStream;
            }
        }

        static T Deserialize<T>(byte[] stream)
        {
            BinaryFormatter binaryFormatter = new BinaryFormatter();
            if (stream == null)
                return default(T);

            using (MemoryStream memoryStream = new MemoryStream(stream))
            {
                T result = (T)binaryFormatter.Deserialize(memoryStream);
                return result;
            }
        }
    }

SEEDのために関連するデータがあることは知っています:

 #region Seed Modules
            var module1 = new Module() { Id = 1, ModuleName = "Contabilidad", FontAwesomeClass = "fa-ambulance" };
            var module2 = new Module() { Id = 2, ModuleName = "Recursos Humanos", FontAwesomeClass = "fa-heartbeat" };
            var module3 = new Module() { Id = 3, ModuleName = "Inventario", FontAwesomeClass = "fa-anchor" };
            var module4 = new Module() { Id = 4, ModuleName = "Produccion", FontAwesomeClass = "fa-binoculars" };
            var module5 = new Module() { Id = 5, ModuleName = "Produccion", FontAwesomeClass = "fa-binoculars" };
            var module6 = new Module() { Id = 6, ModuleName = "Ventas", FontAwesomeClass = "fa-coffee" };
            var module7 = new Module() { Id = 7, ModuleName = "Compras", FontAwesomeClass = "fa-calendar-o" };
            var module8 = new Module() { Id = 8, ModuleName = "Cotizaciones", FontAwesomeClass = "fa-building" };
            context.Modulos.Add(module1);
            context.Modulos.Add(module2);
            context.Modulos.Add(module3);
            context.Modulos.Add(module4);
            context.Modulos.Add(module5);
            context.Modulos.Add(module6);
            context.Modulos.Add(module7);
            context.Modulos.Add(module8);

            context.SaveChanges();
            #endregion

  #region Seed ModulosPor Usuario
            context.ModulosPorUsuario.Add(new ModulosPorUsuario()
            {
                Id=1,
                Email = "companyadmin@mysaasapp.onmicrosoft.com",
                Modules = new List<Module>() { module1, module2 }
            });

            context.ModulosPorUsuario.Add(new ModulosPorUsuario()
            {
                Id=2,
                Email = "accountingadmin@mysaasapp.onmicrosoft.com",
                Modules = new List<Module>() { module3, module5 }
            });

            context.ModulosPorUsuario.Add(new ModulosPorUsuario()
            {
                Id=3,
                Email = "jayhamlin@mysaasapp.onmicrosoft.com",
                Modules = new List<Module>() { module4, module6 }
            });

            context.ModulosPorUsuario.Add(new ModulosPorUsuario()
            {
                Id=4,
                Email = "usuario1@mysaasapp.onmicrosoft.com",
                Modules = new List<Module>() { module7, module7 }
            });


            context.SaveChanges();
            #endregion

アップデート1:

これは、キャッシュされた値を 2 回目に取得しようとしたときに発生するエラーです。

タイプ 'System.Runtime.Serialization.SerializationException' の例外が mscorlib.dll で発生しましたが、ユーザー コードで処理されませんでした

Additional information: Unable to find assembly 'EntityFrameworkDynamicProxies-Inspinia_MVC5, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.

スタックトレース

   at System.Runtime.Serialization.Formatters.Binary.BinaryAssemblyInfo.GetAssembly()
   at System.Runtime.Serialization.Formatters.Binary.ObjectReader.GetType(BinaryAssemblyInfo assemblyInfo, String name)
   at System.Runtime.Serialization.Formatters.Binary.ObjectMap..ctor(String objectName, String[] memberNames, BinaryTypeEnum[] binaryTypeEnumA, Object[] typeInformationA, Int32[] memberAssemIds, ObjectReader objectReader, Int32 objectId, BinaryAssemblyInfo assemblyInfo, SizedArray assemIdToAssemblyTable)
   at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.ReadObjectWithMapTyped(BinaryObjectWithMapTyped record)
   at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.ReadObjectWithMapTyped(BinaryHeaderEnum binaryHeaderEnum)
4

1 に答える 1

3

この型の空のコンストラクターを作成し (以下のコード スニペットを参照)、コレクションをインスタンス化する必要があると思います。シリアライゼーション エンジンはそれを行いません。そのコレクションがインスタンス化されることを期待します。

さらに、コレクション プロパティはクラスではなくインターフェイスであるため、このプロパティを初期化して項目を追加しようとすると、シリアライザーが混乱する可能性があります。

public class ModulosPorUsuario
{
    // added this constructor
    public ModulosPorUsuario()
    {
        this.Modules = new List<Module>();
    }        

    [Key]
    public int Id { get; set; }
    public string Email { get; set; }
    public virtual ICollection<Module> Modules { get; set; }
}

シリアル化する前に初期化していますが、シリアライザーは、逆シリアル化するときにユーザーに代わってオブジェクトをインスタンス化しています。

ハッピーコーディング。

于 2015-07-03T16:18:24.750 に答える