【问题标题】:ObjectDisposedException when creating user创建用户时出现 ObjectDisposedException
【发布时间】:2015-09-09 03:25:46
【问题描述】:

我在尝试创建用户时遇到错误。我在下面的代码中通过line 15 得到它。

1  public async void AddOrganisationAdmin()
2    {
3        
4        String OrganisationName = Request["OrganisationName"];
5        Debug.Print(OrganisationName);
6        RegisterViewModel model = new RegisterViewModel();
7        model.Email = "admin@" + OrganisationName;
8        model.Organisation = OrganisationName;
9        model.Password = "adminPassword!1";
10       model.ConfirmPassword = "adminPassword!1";
11       model.Role = "Organisation Administrator";
12
13
14       var user = new ApplicationUser() { UserName = model.Email, Email = model.Email, Organisation = model.Organisation, Role = model.Role };
15       IdentityResult result = await UserManager.CreateAsync(user, model.Password);
16   }

错误是:

An exception of type 'System.ObjectDisposedException' occurred in mscorlib.dll but 
was not handled in user code

Additional information: Cannot access a disposed object.

什么对象被释放了?这些行是asp.net mvc 5项目中默认注册函数的副本,那我做错了什么?

我正在从这样的 ajax 帖子中调用该函数:

$.ajax({
      url: "/Account/AddOrganisationAdmin",
      type: 'POST',
      data: { OrganisationName : "@CompanyName"},
      success: function (data, textStatus, xhr) {
             console.log(data);
      },
      error: function (xhr, textStatus, errorThrown) {
             console.log(xhr);
      }
});

我进入了函数,我的名字是正确的。

【问题讨论】:

  • 您有自己的IDisposable 或finalizier 实现吗?
  • @AmitKumarGhosh 不,我没有自己的实现。
  • 你能展示CreateAsync方法吗?
  • @AmitKumarGhosh 我正在使用 asp.net 实体的东西。这里稍微解释一下createasyncmsdn.microsoft.com/en-us/library/dn497540%28v=vs.108%29.aspx
  • @AmitKumarGhosh 可能是你的DbContext 你为什么不把手表放在上面?

标签: c# asp.net-mvc-5 objectdisposedexception usermanager


【解决方案1】:

tacos_tacos_tacos 的评论让我稍微研究了一下 dbcontext,经过一段时间的跟踪和错误后,我得到了以下信息

1    public void AddOrganisationAdmin()
2    {
3        
4        String OrganisationName = Request["OrganisationName"];
5        Debug.Print(OrganisationName);
6        RegisterViewModel model = new RegisterViewModel();
7        model.Email = "admin@" + OrganisationName + ".com";
8        model.Organisation = OrganisationName;
9        model.Password = "adminPassword!1";
10       model.ConfirmPassword = "adminPassword!1";
11       model.Role = "Organisation Admin";
12       Promato.Models.ApplicationDbContext dbcontext = new Promato.Models.ApplicationDbContext();
13        
14       var user = new ApplicationUser() { UserName = model.Email, Email = model.Email, Organisation = model.Organisation, Role = model.Role };
15       user.PasswordHash = UserManager.PasswordHasher.HashPassword(model.Password);
16       user.SecurityStamp = Guid.NewGuid().ToString();
17       dbcontext.Users.Add(user);
18       dbcontext.SaveChanges();
19   }

此代码对我有用。一个用户被添加到.mdf,我可以登录了:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-14
    相关资源
    最近更新 更多