【问题标题】:'Microsoft.AspNet.Identity.EntityFramework.IdentityUser' is not marked as serializable'Microsoft.AspNet.Identity.EntityFramework.IdentityUser' 未标记为可序列化
【发布时间】:2015-09-17 21:49:41
【问题描述】:

我正在一个系统中实现 ASP.NET 身份,该系统使用带有实体框架的用户模型进行身份验证,并在 WebApp 中由 Session 控制(是的,它是 Web 表单的遗留系统),我们实现了,替换了 User由身份的ApplicationUser 建模,一切正常。

问题是,有一个通知系统,基本上是NotificationUser模型之间的多对多关系, 这些通知与系统的其余部分一起保存在 SQL Server 中,但也位于缓存 Redis 中以加快读取速度,并且需要对其进行序列化以进行写入。我们删除了User 模型,然后在ApplicationUser 中添加了ICollectionNotification 模型,反之亦然 - 使两者之间产生关联。

但是ApplicationUser继承自IdentityUser,甚至添加注解[Serializable]我得到了异常:

键入“Microsoft.AspNet.Identity.EntityFramework.IdentityUser” 程序集 'Microsoft.AspNet.Identity.EntityFramework,版本 = 2.0.0.0,Culture = 中性,PublicKeyToken = 31bf3856ad364e35' 未标记为可序列化

我的问题是,有没有办法序列化这个?或者我将不得不创建另一个用户模型仅与通知模型相关?

ApplicationUser模特

[Serializable]
public class ApplicationUser : IdentityUser
{
    public ApplicationUser()
    {
        this.Notificacoes = new List<Notificacao>();
    }

    public ClaimsIdentity GenerateUserIdentity(IdentityConfig.ApplicationUserManager manager)
    {
        var userIdentity = manager.CreateIdentity(this, DefaultAuthenticationTypes.ApplicationCookie);
        return userIdentity;
    }

    public Task<ClaimsIdentity> GenerateUserIdentityAsync(IdentityConfig.ApplicationUserManager manager)
    {
        return Task.FromResult(GenerateUserIdentity(manager));
    }
    public bool ReceiveNotifications { get; set; }
    public int Permission { get; set; }
    public virtual ICollection<Notificacao> Notificacoes { get; set; }
}

Notification模特

[Serializable]
public partial class Notificacao
{
    public Notificacao()
    {
        this.Usuarios = new List<ApplicationUser>();
    }

    public int Codigo { get; set; }
    public string Mensagem { get; set; }
    public DateTime DataHoraNotificacao { get; set; }
    public int Tipo { get; set; }
    public virtual ICollection<ApplicationUser> Usuarios { get; set; }

}

Serialize 方法用于将对象序列化到 Redis(抛出异常的地方)

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;
    }
}

【问题讨论】:

标签: c# asp.net entity-framework serialization asp.net-identity


【解决方案1】:

您不想将 IdentityUser 或其任何子类序列化到数据库中。期间。

如果你需要维护数据库中的关系,创建一个POCO(Plain Old CLR Object - 简单来说,一个自定义类)。

另外,如果您需要在用户类中携带 IdentityUser,请使用 composition 而不是 inheritance。这意味着,不要从 IdentityUser 继承,而是在您的类中创建它的属性。并将该属性标记为 NonSerializable

【讨论】:

    【解决方案2】:

    由于您无法控制包含 IdentityUser 类的程序集,因此无法做到这一点。另一种解决方案可能是在缓存之前将您的对象映射到 Dto(数据传输对象),反之则在获取时映射。

    关于该主题的其他主题(与 WCF 相关但仍涉及序列化 IdentityUser):

    asp-net-mvc-5-identityuser-in-wcf

    how-do-i-serialize-an-identityuser-reference-in-web-api-2-2

    【讨论】:

      猜你喜欢
      • 2017-02-21
      • 2015-11-10
      • 2016-01-18
      • 2011-01-17
      • 1970-01-01
      • 1970-01-01
      • 2013-03-20
      • 1970-01-01
      • 2015-02-15
      相关资源
      最近更新 更多