I'm trying to access my DbContext inside my SignalR Hub, but it keeps throwing this exception:
{"Value cannot be null. (Parameter 'unitOfWork')"}
I don't know what is the problem. Here is my code:
public class MyHub : Hub
{
private readonly IRepository<MyTable, long> _repository;
public MyHub (IRepository<MyTable, long> repository)
{
_repository = repository;
}
public async Task Get(TestDto testDto)
{
try
{
var myData = await _repository.GetDbContext().Set<MyTable>()
.Include(x => x.obj1)
.ThenInclude(x => x.list1)
.ThenInclude(x => x.obj2)
.ToListAsync();
await Clients.All.SendAsync("Get", myData);
}
catch (Exception ex)
{
throw new UserFriendlyException(ex.InnerException.Message.ToString());
}
}
}
Note: I'm using ASP.NET Boilerplate framework (.NET core 3.1 and Angular)
Add [UnitOfWork]
attribute and make it a virtual
method:
[UnitOfWork]
public virtual async Task Get(TestDto testDto)
{
// ...
}
Or you can inject IUnitOfWorkManager
to begin a UnitOfWork
explicitly:
public async Task Get(TestDto testDto)
{
using (var uow = UnitOfWorkManager.Begin())
{
try
{
var myData = await _repository.GetDbContext().Set<MyTable>()
.Include(x => x.obj1)
.ThenInclude(x => x.list1)
.ThenInclude(x => x.obj2)
.ToListAsync();
await Clients.All.SendAsync("Get", myData);
}
catch (Exception ex)
{
throw new UserFriendlyException(ex.InnerException.Message.ToString());
}
}
}