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)
-
0
Begin a UnitOfWork:
using (var uow = UnitOfWorkManager.Begin()) { var users = _userManager.Users.ToList(); uow.Complete(); }Markdown is supportedCopy & paste or drag & drop images (max 30 MB per image) -
0
Thanks @aaron..
Markdown is supportedCopy & paste or drag & drop images (max 30 MB per image) -
0
<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
Markdown is supportedCopy & paste or drag & drop images (max 30 MB per image) -
0
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.
Markdown is supportedCopy & paste or drag & drop images (max 30 MB per image) -
0
<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
Markdown is supportedCopy & paste or drag & drop images (max 30 MB per image) -
0
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>.
Markdown is supportedCopy & paste or drag & drop images (max 30 MB per image)