question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Call UserRegistrationManager.RegisterAsync() throw exception in a domain event

See original GitHub issue

Call UserRegistrationManager.RegisterAsync() throw exception in a domain event

public class EventCreateUser : MainxxDomainServiceBase, IEventHandler<Records.Events.AddUserRecordEventData>, ITransientDependency
{
    private readonly UserRegistrationManager _userRegistrationManager;
    private readonly IUnitOfWorkManager _unitOfWorkManager;

    public EventCreateUser(
        IUnitOfWorkManager unitOfWorkManager,
    UserRegistrationManager userRegistrationManager)
    {
        _userRegistrationManager = userRegistrationManager;
        _unitOfWorkManager = unitOfWorkManager;
    }
    public async void HandleEvent(AddUserRecordEventData eventData)
    {
        var user = await _userRegistrationManager.RegisterAsync(
                    eventData.AddUserRecordData.UserName,
                    eventData.AddUserRecordData.UserName,
                    eventData.AddUserRecordData.UserName + "@test.core",
                    eventData.AddUserRecordData.UserName,
                    eventData.AddUserRecordData.Password,
                    true, null);
    }
}

error

Error Massage

System.ObjectDisposedException
  HResult=0x80131622
  Message=Cannot access a disposed object. A common cause of this error is disposing a context that was resolved from dependency injection and then later trying to use the same context instance elsewhere in your application. This may occur if you are calling Dispose() on the context, or wrapping the context in a using statement. If you are using dependency injection, you should let the dependency injection container take care of disposing context instances.Cannot access a disposed object. A common cause of this error is disposing a context that was resolved from dependency injection and then later trying to use the same context instance elsewhere in your application. This may occur if you are calling Dispose() on the context, or wrapping the context in a using statement. If you are using dependency injection, you should let the dependency injection container take care of disposing context instances.
  Source=Microsoft.EntityFrameworkCore
  StackTrace:
   at Microsoft.EntityFrameworkCore.DbContext.CheckDisposed()
   at Microsoft.EntityFrameworkCore.DbContext.get_DbContextDependencies()
   at Microsoft.EntityFrameworkCore.DbContext.get_Model()
   at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.get_EntityType()
   at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.get_EntityQueryable()
   at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.System.Linq.IQueryable.get_Provider()
   at System.Linq.Queryable.Where[TSource](IQueryable`1 source, Expression`1 predicate)
   at Mainxx.Authorization.Users.UserRegistrationManager.<RegisterAsync>d__13.MoveNext() in S:\Gitee\Mainxx\aspnet-core\src\Mainxx.Core\Authorization\Users\UserRegistrationManager.cs:line 82
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
  • Abp 3.7.2
  • .Net Core2.1 .

Your answers are very important to me, thank you

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
acjhcommented, Jun 24, 2018
  • Add [UnitOfWork] attribute.
  • Make method virtual.
  • Implement IAsyncEventHandler if async.
[UnitOfWork]
public virtual async Task HandleEventAsync(AddUserRecordEventData eventData)
0reactions
kuyz123commented, Jun 20, 2023

Because I need to write a callback interface and the other party requires a result to be returned within 2 seconds, I didn’t wait on my end and calling the asynchronous method of efcore would result in an error. The error is as follows: Cannot access a disposed context instance. A common cause of this error is disposing a context instance that was resolved from dependency injection and then later trying to use the same context instance elsewhere in your application. This may occur if you are calling 'Dispose' on the context instance, or wrapping it in a using statement. If you are using dependency injection, you should let the dependency injection container take care of disposing context instances. Object name: 'SqlServerDbContext'. Cannot access a disposed context instance. A common cause of this error is disposing a context instance that was resolved from dependency injection and then later trying to use the same context instance elsewhere in your application. This may occur if you are calling 'Dispose' on the context instance, or wrapping it in a using statement. If you are using dependency injection, you should let the dependency injection container take care of disposing context instances. ObjectDisposed_ObjectName_Name controller public async Task<IActionResult> ReceiveCard() { _jdyCardService.ReceiveData(_Body); return Ok("success"); }

Read more comments on GitHub >

github_iconTop Results From Across the Web

CQRS - Domain Exceptions Vs Events for ...
I'm wondering if it's preferable to publish events rather than throw exceptions from aggregates. Say, I have a domain where there is a ......
Read more >
Exception throwed when calling RegisterAsync of ...
I have a below method , written using `C#` public async Task<DeviceRegistrationResult> RegisterDevice(ProvisioningDeviceClient provClient) ...
Read more >
ABP在领域事件中异步调用方法抛异常转载
RegisterAsync抛异常Call UserRegistrationManager.RegisterAsync() throw exception in a ... RegisterAsync() throw exception in a domain event.
Read more >
Validation Error as an event returned by the Domain Layer
I assume the validation error indicates the domain service is prevented from completing the requested task. In that case an event is not ......
Read more >
Domain events: simple and reliable solution
The idea is simple: if you want to indicate an event that is significant to your domain, raise this event explicitly and let...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found