Base solution for your next web application
Open Closed

Create User DisposedError in .Application #374


User avatar
0
spencerdoe created

Hi,

I am a newbie here and are still struggling about the comprehensive concepts about ABP. I tried to add a register page, writing some code about the registration service like this in .Application layer:

async void IUserService.CreateUserWithoutAuthorization(UserInput input)
        {
            var testUser = new User
            {
           
                CellPhone = input.CellPhone,
                EmailAddress = input.EmailAddress,
                UserName = input.UserName        
              
            };
     
            (await _userManager.CreateAsync(testUser)).CheckErrors();       
        }

Something happens when calling CreateAsync function. IDE tells me : Cannot access disposed object: UserManagerProxy. I have no idea why this object is disposed at the moment.

StackTrace are as follows:

at Microsoft.AspNet.Identity.UserManager`2.ThrowIfDisposed()
   at Microsoft.AspNet.Identity.UserManager`2.<CreateAsync>d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Abp.Authorization.Users.AbpUserManager`3.&lt;CreateAsync&gt;d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)

Then I tried to remove await, but I found the test user is not saved in AppUsers Table. But it successfully returns an Id. I am wondering where the testUser is stored? Please find the code and the connectionString down below:

_userManager.CreateAsync(preInsert); 
            var user = _userManager.FindByNameAsync(testUser.UserName);
&lt;add name=&quot;Default&quot; connectionString=&quot;Server=localhost; Database=AbpTestDb; Trusted_Connection=True;&quot; providerName=&quot;System.Data.SqlClient&quot; /&gt;

Can any one help me with this :D ? Or tell me where I should dig in? Or give me some good articles about how userManager really works in Asp.NET identity? I am totally lost...

Thanks very much!


3 Answer(s)
  • User Avatar
    0
    hikalkan created
    Support Team

    Can you try this one:

    public async Task CreateUserWithoutAuthorization(UserInput input)
    {
        var testUser = new User
        {
            CellPhone = input.CellPhone,
            EmailAddress = input.EmailAddress,
            UserName = input.UserName        
        };
    
        (await _userManager.CreateAsync(testUser)).CheckErrors();       
    }
    

    I changed void to Task. The only thing I can see is that for now.

  • User Avatar
    0
    spencerdoe created

    <cite>hikalkan: </cite> Can you try this one:

    public async Task CreateUserWithoutAuthorization(UserInput input)
    {
       var testUser = new User
       {
           CellPhone = input.CellPhone,
           EmailAddress = input.EmailAddress,
           UserName = input.UserName        
       };
    
       (await _userManager.CreateAsync(testUser)).CheckErrors();       
    }
    

    I changed void to Task. The only thing I can see is that for now.

    Thanks! But can you please brief me in about why the return type matters? Your help is really appreciated!! :D

  • User Avatar
    0
    hikalkan created
    Support Team

    I'm not expert in async. But as I know, you should not use "async void" except UI event handlers, always use "async Task". You can search it on the web.