【问题标题】:"Inspecting the state of an object in the debuggee of type System.Reflection.MethodBase is not supported in this context"“在此上下文中不支持在 System.Reflection.MethodBase 类型的调试对象中检查对象的状态”
【发布时间】:2018-08-23 13:44:47
【问题描述】:

我不知道这个错误是什么意思。我正在使用 Visual Studio for Mac 7.5.0 社区版。我在带有 ASP.NET Core 的实体框架中使用延迟加载。

public partial class AdminUser
{
    public AdminUser()
    {
        RoleAssign = new HashSet<RoleAssign>();
    }

    public Guid UserId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
    public string UserName { get; set; }
    public byte[] Password { get; set; }
    public DateTime CreatedTimeStamp { get; set; }
    public DateTime? ModifiedTimeStamp { get; set; }
    public DateTime? LogDate { get; set; }
    public short? LogNumber { get; set; }
    public bool ReloadActiveFlag { get; set; }
    public bool IsActive { get; set; }
    public string ExtraText { get; set; }
    public string ResetPasswordToken { get; set; }
    public DateTime? ResetPasswordTokenCreatedTimeStamp { get; set; }

    public virtual ICollection<RoleAssign> RoleAssign { get; set; }
}

RoleAssign实体模型:

public partial class RoleAssign
{
    public Guid RoleAssignId { get; set; }
    public Guid RoleId { get; set; }
    public Guid UserId { get; set; }

    public virtual AdminRole Role { get; set; }
    public virtual AdminUser User { get; set; }
}

这里是实体构建器:

modelBuilder.Entity<RoleAssign>(entity =>
{
    entity.Property(e => e.RoleAssignId).ValueGeneratedNever();

    entity.HasOne(d => d.Role)
        .WithMany(p => p.RoleAssign)
        .HasForeignKey(d => d.RoleId)
        .OnDelete(DeleteBehavior.ClientSetNull)
        .HasConstraintName("FK__RoleAssig__RoleI__160F4887");

    entity.HasOne(d => d.User)
        .WithMany(p => p.RoleAssign)
        .HasForeignKey(d => d.UserId)
        .OnDelete(DeleteBehavior.ClientSetNull)
        .HasConstraintName("FK__RoleAssig__UserI__17036CC0");
});

这是用户表的实体构建器:

modelBuilder.Entity<AdminUser>(entity =>
{
    entity.HasKey(e => e.UserId);

    entity.Property(e => e.UserId).ValueGeneratedNever();

    entity.Property(e => e.CreatedTimeStamp)
        .HasColumnType("datetime")
        .HasDefaultValueSql("(getdate())");

    entity.Property(e => e.Email)
        .IsRequired()
        .IsUnicode(false);

    entity.Property(e => e.ExtraText).IsUnicode(false);

    entity.Property(e => e.FirstName)
        .IsRequired()
        .IsUnicode(false);

    entity.Property(e => e.IsActive)
        .IsRequired()
        .HasColumnName("isActive")
        .HasDefaultValueSql("((1))");

    entity.Property(e => e.LastName)
        .IsRequired()
        .IsUnicode(false);

    entity.Property(e => e.LogDate).HasColumnType("datetime");

    entity.Property(e => e.ModifiedTimeStamp).HasColumnType("datetime");

    entity.Property(e => e.Password).IsRequired();

    entity.Property(e => e.ResetPasswordToken).IsUnicode(false);

    entity.Property(e => e.ResetPasswordTokenCreatedTimeStamp).HasColumnType("datetime");

    entity.Property(e => e.UserName)
        .IsRequired()
        .IsUnicode(false);
});

UOW 代码:

public async Task<UserViewModel> AdminAuthentication(UserViewModel userView)
{
    var user = await _adminGenericRepository.FindAsync(x => x.IsActive && x.UserName.Equals(userView.UserName) && (AesEncryptAndDecrypt.DecryptStringFromBytes(x.Password, crytograpyKey, crytograpyIV).Equals(userView.Password)));

    if (user != null)
    {
        return new UserViewModel
        {
            UserId = user.UserId,
            isActive = user.IsActive,
            UserName = user.UserName,
            LastName = user.LastName,
            FirstName = user.FirstName,
            SelectedRole = mapRoleDbDataToViewModel(user.RoleAssign != null ? user.RoleAssign.FirstOrDefault().Role : null)
        };
    }
    return null;
}

映射器类:

private RoleViewModel mapRoleDbDataToViewModel(AdminRole dbRole)
{
    if (dbRole != null)
    {
        return new RoleViewModel
        {
            RoleId = dbRole.RoleId,
            RoleName = dbRole.RoleName,
            RoleType = dbRole.RoleType,
            SortOrder = dbRole.SortOrder,
            TreeLevel = dbRole.TreeLevel,
            Permissions = GetRuleByRoleId(dbRole.RoleId)
        };
    }
    return null;
}

存储库文件:

public virtual async Task<T> FindAsync(Expression<Func<T, bool>> predicate)
{
    return await _entities.Set<T>().SingleOrDefaultAsync(predicate);
}

public IQueryable<T> FindBy(System.Linq.Expressions.Expression<Func<T, bool>> predicate)
{
    IQueryable<T> query = _entities.Set<T>().Where(predicate);
    return query;
}

错误信息截图:

成绩单:

在此上下文中不支持在 System.Reflection.MethodBase 类型的调试对象中检查对象的状态。

【问题讨论】:

  • 这个错误是否会阻止代码执行?
  • 是的。之后代码不会执行..
  • 请添加_adminGenericRepository.FindAsync()的代码,以及图像中断点处的整行代码:var user = await .... 没有断点继续运行时,会引发什么异常?
  • 添加完整代码。在 SelectedRole = mapRoleDbDataToViewModel(user.RoleAssign != null ? user.RoleAssign.FirstOrDefault().Role : null) 上出现错误。由于那个错误
  • @StevePy - 我发现 --> 延迟加载不适合异步模式。你对这个问题有什么想法吗

标签: c# asp.net entity-framework asp.net-core visual-studio-mac


【解决方案1】:

据我了解,调试发生在哪里,这是由 Visual Studio 调试器的表达式电梯产生的,因此这可能意味着调试器试图从 System.Reflection.MethodBase 类型的实例中获取数据,但是这样的对象不可用,因此产生了该错误,

您可以尝试使用旧版调试引擎,可能会修复它 (工具 -> 选项 -> 调试 -> 常规 -> “使用托管兼容模式”)

【讨论】:

  • 我正在使用 Visual Studio for Mac。我没有找到你说的那个选项。我试图找到该选项,但没有找到。
  • 这个 msdn 博客是我的参考 blogs.msdn.microsoft.com/devops/2013/10/16/… ,它适用于 vs2013 并且也适用于 VS2015 ,也许你错过了一步
  • 实际上还有另一种方法,右键单击并选择项目的属性,转到“调试”然后选中复选框“启用本机代码调试”我认为这在 vs for mac 中可用跨度>
  • 使用管理器兼容模式为我在 VS2019/Win 10 上解决了这个问题。
  • @Kolichikov 我一开始没有得到这个,但现在我意识到微软在 VS2022 中删除了这个!!!。那个sux。
【解决方案2】:

不要依赖 Find,因为它会在 DB 之前进入缓存,尤其是在您要检索相关实体的地方。另外,我相信您对密码的处理是倒退的,并且由于解密功能而无法通过 EF/SQL。因此,对用户提供的密码进行加密/散列,并将其与数据库中已经加密/散列的数据进行比较。

string encryptedPassword = AesEncryptAndDecrypt.EncryptString(userView.Password, crytograpyKey, crytograpyIV);
var userData = await _adminGenericRepository.FindBy(x => x.IsActive && x.UserName == userView.UserName && x.Password == encryptedPassword)
    .Select( new     
    {
        UserId = user.UserId,
        IsActive = user.IsActive,
        UserName = user.UserName,
        LastName = user.LastName,
        FirstName = user.FirstName,
        // When using FirstOrDefault, you should have an OrderBy to ensure the selection is predictable.
        SelectedRole = user.RoleAssign.OrderByDescending(x => x.Date).FirstOrDefault()?.Role 
        // Cannot call C# methods here since this will go to SQL.. 
        // If you can populate a UserRoleViewModel in-line, then that can be put here to skip the extra mapping below.
    }).SingleOrDefaultAsync();

// At this point we will have the user details and it's selected Role ready for mapping. 
//This assumes that the mapping of the Role does not rely on any child relationships under the Role.
if (userData != null)
    return new UserViewModel
    {
        UserId = userData.UserId,
        IsActive = userData.IsActive,
        UserName = userData.UserName,
        LastName = userData.LastName,
        FirstName = userData.FirstName,
        SelectedRole = mapRoleDbDataToViewModel(userData.SelectedRole)
    };
else
    return null;

【讨论】:

  • “正确的方式”是什么意思?关于密码的处理或填充视图模型?不行吗?
  • 填充视图模型。我没有尝试过......因为这种方法有很多代码
  • 填充视图模型的额外步骤(与在 EF .Select() 表达式中执行此操作相比,仅当您需要调用 C# 函数 (mapRoleDbDataToViewModel) 时。EF 会尝试传递它函数到不知道该函数的 SQL Server。相反,如果您可以从相关数据 (user.RoleAssign.Role) 创建视图模型,那么您可以在一个表达式中填充完整的视图模型结构。先尝试一下,弄清楚如何让它工作,然后从那里优化它。
猜你喜欢
  • 2021-03-17
  • 2014-12-18
  • 2017-04-22
  • 1970-01-01
  • 2015-05-17
  • 1970-01-01
  • 1970-01-01
  • 2020-07-07
  • 2018-02-10
相关资源
最近更新 更多