Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Issue with UserManager in the domain service. #4503


User avatar
0
sparkyjr created

I have a DomainService and I want to fetch all the users for a particular tenant. I try to do that using UserManager.So inject UserManager as a dependency in the constructor and try to use _userManger.Users.ToList() . But I get an exception as "DbContext has been disposed."


6 Answer(s)
  • User Avatar
    0
    aaron created
    Support Team

    Begin a UnitOfWork:

    using (var uow = UnitOfWorkManager.Begin())
    {
        var users = _userManager.Users.ToList();
        uow.Complete();
    }
    
  • User Avatar
    0
    sparkyjr created

    Thanks @aaron..

  • User Avatar
    0
    tteoh created

    <cite>aaron: </cite> Begin a UnitOfWork:

    using (var uow = UnitOfWorkManager.Begin())
    {
       var users = _userManager.Users.ToList();
       uow.Complete();
    }
    

    Hi Aaron,

    Could you explain why an explicit UoW is required?

    Thanks. /Tommy

  • User Avatar
    0
    aaron created
    Support Team

    Domain service methods are not conventional unit of work methods.

    Alternatively, you can use [UnitOfWork] attribute for:

    • All public or public virtual methods for classes those are used over interface (Like an application service used over service interface).
    • All public virtual methods for self injected classes (Like MVC Controllers and Web API Controllers).
    • All protected virtual methods.
  • User Avatar
    0
    tteoh created

    <cite>aaron: </cite> Domain service methods are not conventional unit of work methods.

    Alternatively, you can use [UnitOfWork] attribute for:

    • All public or public virtual methods for classes those are used over interface (Like an application service used over service interface).
    • All public virtual methods for self injected classes (Like MVC Controllers and Web API Controllers).
    • All protected virtual methods.

    Hi Aaron,

    Based on the link you provided.... it states the below:

    "You can inject and use IUnitOfWorkManager as shown here (Some base classes have already UnitOfWorkManager injected by default: MVC Controllers, application services, <span style="color:#FF0000">domain services</span>...). Thus, you can create more limited scope unit of works. In this approach, you should call Complete method manually. If you don't call, transaction is rolled back and changes are not saved."

    I interpret it as Domain Services is injected with UoW by default. Thank you for further clarification.

    /Tommy

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @tteoh,

    As @aaron wrote, "Domain service methods are not conventional unit of work methods." but domain services have a property named "UnitOfWorkManager", see <a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate/blob/dev/src/Abp/AbpServiceBase.cs#L26">https://github.com/aspnetboilerplate/as ... ase.cs#L26</a>.