ASP.NET核心5,如何在SignalR Hub中注入依赖class?

ASP.NET Core 5, How to inject dependencies in SignalR Hub class?

我正在使用 ABP vNext 开发 IoT 应用程序,方法是使用集线器中的存储库,但连接已被释放。应该如何正确注入依赖?

[Authorize(Roles = RoleResources.Collector)]
public class CollectorHub : AbpHub
{
    private readonly CollectorManager _manager;
    private readonly IRepository<Collector, Guid> _repository;
    private readonly ICacheManager<string, CollectorSession> _sessionCache;

    public CollectorHub(
        CollectorManager manager,
        IRepository<Collector, Guid> repository,
        ICacheManager<string, CollectorSession> sessionCache)
    {
        _manager = manager;
        _repository = repository;
        _sessionCache = sessionCache;
    }

    public override async Task OnDisconnectedAsync(Exception e)
    {
        var collectorId = GetCollectorId();
        var connectionId = Context.ConnectionId;
        _sessionCache.Remove(connectionId);

        ## ?? both repository and hub are transient, but the following line of code throws an error that the connection has been disposed!
        var collector = await _repository.FindAsync(collectorId);
        if (collector != null)
        {
            collector.CommunicationState = CommunicationState.Offline;
            await _repository.UpdateAsync(collector, true);
        }

        await base.OnDisconnectedAsync(e);
    }
}

System.OperationCanceledException: The operation was canceled. at System.Threading.CancellationToken.ThrowOperationCanceledException() at MySqlConnector.Core.ConnectionPool.GetSessionAsync(MySqlConnection connection, Int32 startTickCount, IOBehavior ioBehavior, CancellationToken cancellationToken) in //src/MySqlConnector/Core/ConnectionPool.cs:line 139 at MySqlConnector.MySqlConnection.CreateSessionAsync(ConnectionPool pool, Int32 startTickCount, Nullable1 ioBehavior, CancellationToken cancellationToken) in /_/src/MySqlConnector/MySqlConnection.cs:line 892 at MySqlConnector.MySqlConnection.OpenAsync(Nullable1 ioBehavior, CancellationToken cancellationToken) in //src/MySqlConnector/MySqlConnection.cs:line 435 at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenInternalAsync(Boolean errorsExpected, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenInternalAsync(Boolean errorsExpected, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenAsync(CancellationToken cancellationToken, Boolean errorsExpected) at Pomelo.EntityFrameworkCore.MySql.Storage.Internal.MySqlRelationalConnection.OpenAsync(CancellationToken cancellationToken, Boolean errorsExpected) at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.Query.Internal.SplitQueryingEnumerable1.AsyncEnumerator.InitializeReaderAsync(DbContext _, Boolean result, CancellationToken cancellationToken) at Pomelo.EntityFrameworkCore.MySql.Storage.Internal.MySqlExecutionStrategy.ExecuteAsync[TState,TResult](TState state, Func4 operation, Func4 verifySucceeded, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.Query.Internal.SplitQueryingEnumerable1.AsyncEnumerator.MoveNextAsync()

我找到了正确的解决方案,使用 IUnitOfWork 手动启动新的工作单元。

using (var uow = _unitOfWorkManager.Begin())
{
    await uow.CompleteAsync();
}