C# 类型为';System.Runtime.Serialization.SerializationException';在mscorlib.dll中发生,但未在用户代码中处理

C# 类型为';System.Runtime.Serialization.SerializationException';在mscorlib.dll中发生,但未在用户代码中处理,c#,asp.net-mvc,serialization,stackexchange.redis,azure-redis-cache,C#,Asp.net Mvc,Serialization,Stackexchange.redis,Azure Redis Cache,我有以下目标: [Serializable] public class Module { [Key] public int Id { get; set; } public string ModuleName { get; set; } public string FontAwesomeClass { get; set; } } [Serializable] public class Modu

我有以下目标:

 [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; }
    }
堆栈跟踪

   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)

我相信您需要为这种类型创建一个空构造函数(请参阅下面的代码片段)并实例化您的集合。序列化引擎不会为您执行此操作,它希望该集合被实例化

此外,由于集合属性是接口而不是类,因此序列化程序在尝试初始化此属性和添加项时可能会感到困惑

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; }
}
公共类模块
{
//添加了这个构造函数
公共模块orusuario()
{
this.Modules=newlist();
}        
[关键]
公共int Id{get;set;}
公共字符串电子邮件{get;set;}
公共虚拟ICollection模块{get;set;}
}
虽然您在序列化之前对其进行初始化,但在反序列化时,序列化程序会代表您实例化对象


愉快的编码。

我认为可能ICollection不可序列化,所以我改为List,但这并没有解决问题,现在测试构造函数@Glenn Ferreiit根据您提供的屏幕广播,任何其他想法看起来都缺少程序集。那个DLL在哪里?找到了:但所有的程序集都在那里。它是自动生成的,显然你走对了路。除非在生成时生成序列化程序集,否则它们总是在运行时生成,这(a)会导致性能降级,并且(b)如果运行应用程序的用户上下文没有对磁盘的写访问权限,则可能会导致问题。我的建议是在生成时生成序列化程序集。您应该看到csproj文件中的设置(项目设置)
 #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
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)
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; }
}