@@ -186,7 +186,11 @@ static bool IsUsePort(IPAddress address, int port)
186186 builder . Services . AddRoutingCore ( ) ;
187187 builder . Services . AddLogging ( ConfigureLogging ) ;
188188 builder . Services . ConfigureHttpJsonOptions ( ConfigureHttpJsonOptions ) ;
189- ConfigureSignalRProtocol ( builder . Services . AddSignalR ( ConfigureSignalR ) ) ;
189+ builder . Services . AddSingleton (
190+ typeof ( HubConnectionHandler < > ) ,
191+ typeof ( FixHubConnectionHandler < > ) ) ;
192+ var signalRServerBuilder = builder . Services . AddSignalR ( ConfigureSignalR ) ;
193+ ConfigureSignalRProtocol ( signalRServerBuilder ) ;
190194 builder . Services . AddHttpContextAccessor ( ) ;
191195 ConfigureAuthentication ( builder . Services . AddAuthentication ( DefaultAuthenticationScheme ) ) ;
192196
@@ -421,6 +425,9 @@ protected virtual void ConfigureHub(HttpConnectionDispatcherOptions options)
421425
422426 public IServiceProvider Services => app . ThrowIsNull ( ) . Services ;
423427
428+ public abstract bool TryGetHubContext ( [ NotNullWhen ( true ) ] out IHubContext ? hubContext ) ;
429+
430+ [ Obsolete ( "use TryGetHubContext" , true ) ]
424431 public abstract IHubContext HubContext { get ; }
425432
426433 readonly Dictionary < string , Type > hubTypes = [ ] ;
@@ -636,4 +643,32 @@ protected override Task<AuthenticateResult> HandleAuthenticateAsync()
636643 var result = HandleAuthenticate ( ) ;
637644 return Task . FromResult ( result ) ;
638645 }
646+ }
647+
648+ file sealed class FixHubConnectionHandler <
649+ [ DynamicallyAccessedMembers ( DynamicallyAccessedMemberTypes . All ) ] THub > (
650+ HubLifetimeManager < THub > lifetimeManager ,
651+ IHubProtocolResolver protocolResolver ,
652+ IOptions < HubOptions > globalHubOptions ,
653+ IOptions < HubOptions < THub > > hubOptions ,
654+ ILoggerFactory loggerFactory ,
655+ IUserIdProvider userIdProvider ,
656+ IServiceScopeFactory serviceScopeFactory ) :
657+ HubConnectionHandler < THub > ( lifetimeManager , protocolResolver , globalHubOptions ,
658+ hubOptions , loggerFactory , userIdProvider ,
659+ serviceScopeFactory )
660+ where THub : Hub
661+ {
662+ /// <inheritdoc/>
663+ public sealed override async Task OnConnectedAsync ( ConnectionContext connection )
664+ {
665+ try
666+ {
667+ await base . OnConnectedAsync ( connection ) ;
668+ }
669+ catch ( ObjectDisposedException )
670+ {
671+ // 应用程序退出时引发 Ioc 被释放异常忽略
672+ }
673+ }
639674}
0 commit comments