@@ -4927,13 +4927,14 @@ public async Task KeyedServiceResolvedIfInDI()
49274927 } ) ;
49284928
49294929 provider . AddKeyedScoped < Service1 > ( "service1" ) ;
4930+ provider . AddKeyedScoped < Service1 > ( "service2" ) ;
49304931 } ) ;
4931- var connectionHandler = serviceProvider . GetService < HubConnectionHandler < ServicesHub > > ( ) ;
4932+ var connectionHandler = serviceProvider . GetService < HubConnectionHandler < KeyedServicesHub > > ( ) ;
49324933
49334934 using ( var client = new TestClient ( ) )
49344935 {
49354936 var connectionHandlerTask = await client . ConnectAsync ( connectionHandler ) . DefaultTimeout ( ) ;
4936- var res = await client . InvokeAsync ( nameof ( ServicesHub . KeyedService ) ) . DefaultTimeout ( ) ;
4937+ var res = await client . InvokeAsync ( nameof ( KeyedServicesHub . KeyedService ) ) . DefaultTimeout ( ) ;
49374938 Assert . Equal ( 43L , res . Result ) ;
49384939 }
49394940 }
@@ -4949,13 +4950,14 @@ public async Task HubMethodCanInjectKeyedServiceWithOtherParameters()
49494950 } ) ;
49504951
49514952 provider . AddKeyedScoped < Service1 > ( "service1" ) ;
4953+ provider . AddKeyedScoped < Service1 > ( "service2" ) ;
49524954 } ) ;
4953- var connectionHandler = serviceProvider . GetService < HubConnectionHandler < ServicesHub > > ( ) ;
4955+ var connectionHandler = serviceProvider . GetService < HubConnectionHandler < KeyedServicesHub > > ( ) ;
49544956
49554957 using ( var client = new TestClient ( ) )
49564958 {
49574959 var connectionHandlerTask = await client . ConnectAsync ( connectionHandler ) . DefaultTimeout ( ) ;
4958- var res = await client . InvokeAsync ( nameof ( ServicesHub . KeyedServiceWithParam ) , 91 ) . DefaultTimeout ( ) ;
4960+ var res = await client . InvokeAsync ( nameof ( KeyedServicesHub . KeyedServiceWithParam ) , 91 ) . DefaultTimeout ( ) ;
49594961 Assert . Equal ( 1183L , res . Result ) ;
49604962 }
49614963 }
@@ -4971,14 +4973,15 @@ public async Task HubMethodCanInjectKeyedServiceWithNonKeyedService()
49714973 } ) ;
49724974
49734975 provider . AddKeyedScoped < Service1 > ( "service1" ) ;
4976+ provider . AddKeyedScoped < Service1 > ( "service2" ) ;
49744977 provider . AddScoped < Service2 > ( ) ;
49754978 } ) ;
4976- var connectionHandler = serviceProvider . GetService < HubConnectionHandler < ServicesHub > > ( ) ;
4979+ var connectionHandler = serviceProvider . GetService < HubConnectionHandler < KeyedServicesHub > > ( ) ;
49774980
49784981 using ( var client = new TestClient ( ) )
49794982 {
49804983 var connectionHandlerTask = await client . ConnectAsync ( connectionHandler ) . DefaultTimeout ( ) ;
4981- var res = await client . InvokeAsync ( nameof ( ServicesHub . KeyedServiceNonKeyedService ) ) . DefaultTimeout ( ) ;
4984+ var res = await client . InvokeAsync ( nameof ( KeyedServicesHub . KeyedServiceNonKeyedService ) ) . DefaultTimeout ( ) ;
49824985 Assert . Equal ( 11L , res . Result ) ;
49834986 }
49844987 }
@@ -4996,12 +4999,12 @@ public async Task MultipleKeyedServicesResolved()
49964999 provider . AddKeyedScoped < Service1 > ( "service1" ) ;
49975000 provider . AddKeyedScoped < Service1 > ( "service2" ) ;
49985001 } ) ;
4999- var connectionHandler = serviceProvider . GetService < HubConnectionHandler < ServicesHub > > ( ) ;
5002+ var connectionHandler = serviceProvider . GetService < HubConnectionHandler < KeyedServicesHub > > ( ) ;
50005003
50015004 using ( var client = new TestClient ( ) )
50025005 {
50035006 var connectionHandlerTask = await client . ConnectAsync ( connectionHandler ) . DefaultTimeout ( ) ;
5004- var res = await client . InvokeAsync ( nameof ( ServicesHub . MultipleKeyedServices ) ) . DefaultTimeout ( ) ;
5007+ var res = await client . InvokeAsync ( nameof ( KeyedServicesHub . MultipleKeyedServices ) ) . DefaultTimeout ( ) ;
50055008 Assert . Equal ( 45L , res . Result ) ;
50065009 }
50075010 }
@@ -5017,19 +5020,20 @@ public async Task MultipleKeyedServicesWithSameNameResolved()
50175020 } ) ;
50185021
50195022 provider . AddKeyedScoped < Service1 > ( "service1" ) ;
5023+ provider . AddKeyedScoped < Service1 > ( "service2" ) ;
50205024 } ) ;
5021- var connectionHandler = serviceProvider . GetService < HubConnectionHandler < ServicesHub > > ( ) ;
5025+ var connectionHandler = serviceProvider . GetService < HubConnectionHandler < KeyedServicesHub > > ( ) ;
50225026
50235027 using ( var client = new TestClient ( ) )
50245028 {
50255029 var connectionHandlerTask = await client . ConnectAsync ( connectionHandler ) . DefaultTimeout ( ) ;
5026- var res = await client . InvokeAsync ( nameof ( ServicesHub . MultipleSameKeyedServices ) ) . DefaultTimeout ( ) ;
5030+ var res = await client . InvokeAsync ( nameof ( KeyedServicesHub . MultipleSameKeyedServices ) ) . DefaultTimeout ( ) ;
50275031 Assert . Equal ( 445L , res . Result ) ;
50285032 }
50295033 }
50305034
50315035 [ Fact ]
5032- public async Task KeyedServiceNotResolvedIfNotInDI ( )
5036+ public void KeyedServiceNotResolvedIfNotInDI ( )
50335037 {
50345038 var serviceProvider = HubConnectionHandlerTestUtils . CreateServiceProvider ( provider =>
50355039 {
@@ -5038,14 +5042,24 @@ public async Task KeyedServiceNotResolvedIfNotInDI()
50385042 options . EnableDetailedErrors = true ;
50395043 } ) ;
50405044 } ) ;
5041- var connectionHandler = serviceProvider . GetService < HubConnectionHandler < ServicesHub > > ( ) ;
5045+ var ex = Assert . Throws < InvalidOperationException > ( ( ) => serviceProvider . GetService < HubConnectionHandler < KeyedServicesHub > > ( ) ) ;
5046+ Assert . Equal ( "'Microsoft.AspNetCore.SignalR.Tests.Service1' is not in DI as a keyed service." , ex . Message ) ;
5047+ }
50425048
5043- using ( var client = new TestClient ( ) )
5049+ [ Fact ]
5050+ public void KeyedServiceAndFromServiceOnSameParameterInvalidWithKeyedServiceInDI ( )
5051+ {
5052+ var serviceProvider = HubConnectionHandlerTestUtils . CreateServiceProvider ( provider =>
50445053 {
5045- var connectionHandlerTask = await client . ConnectAsync ( connectionHandler ) . DefaultTimeout ( ) ;
5046- var res = await client . InvokeAsync ( nameof ( ServicesHub . KeyedService ) ) . DefaultTimeout ( ) ;
5047- Assert . Equal ( "Failed to invoke 'KeyedService' due to an error on the server. InvalidDataException: Invocation provides 0 argument(s) but target expects 1." , res . Error ) ;
5048- }
5054+ provider . AddSignalR ( options =>
5055+ {
5056+ options . EnableDetailedErrors = true ;
5057+ } ) ;
5058+
5059+ provider . AddKeyedScoped < Service1 > ( "service1" ) ;
5060+ } ) ;
5061+ var ex = Assert . Throws < InvalidOperationException > ( ( ) => serviceProvider . GetService < HubConnectionHandler < BadServicesHub > > ( ) ) ;
5062+ Assert . Equal ( "BadServicesHub.BadMethod: The FromKeyedServicesAttribute is not supported on parameters that are also annotated with IFromServiceMetadata." , ex . Message ) ;
50495063 }
50505064
50515065 [ Fact ]
0 commit comments