Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/SignalR/server/Core/src/Internal/DefaultHubDispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,11 @@ private void ReplaceArguments(HubMethodDescriptor descriptor, HubMethodInvocatio
var hubInvocationArgumentPointer = 0;
for (var parameterPointer = 0; parameterPointer < arguments.Length; parameterPointer++)
{
if (descriptor.IsServiceArgument(parameterPointer))
{
arguments[parameterPointer] = descriptor.GetService(scope.ServiceProvider, parameterPointer, descriptor.OriginalParameterTypes[parameterPointer]);
continue;
}
if (hubMethodInvocationMessage.Arguments?.Length > hubInvocationArgumentPointer &&
(hubMethodInvocationMessage.Arguments[hubInvocationArgumentPointer] == null ||
descriptor.OriginalParameterTypes[parameterPointer].IsAssignableFrom(hubMethodInvocationMessage.Arguments[hubInvocationArgumentPointer]?.GetType())))
Expand All @@ -741,10 +746,6 @@ private void ReplaceArguments(HubMethodDescriptor descriptor, HubMethodInvocatio
cts = CancellationTokenSource.CreateLinkedTokenSource(connection.ConnectionAborted);
arguments[parameterPointer] = cts.Token;
}
else if (descriptor.IsServiceArgument(parameterPointer))
{
arguments[parameterPointer] = descriptor.GetService(scope.ServiceProvider, parameterPointer, descriptor.OriginalParameterTypes[parameterPointer]);
}
else if (isStreamCall && ReflectionHelper.IsStreamingType(descriptor.OriginalParameterTypes[parameterPointer], mustBeDirectType: true))
{
Log.StartingParameterStream(_logger, hubMethodInvocationMessage.StreamIds![streamPointer]);
Expand Down Expand Up @@ -896,4 +897,4 @@ private static void SetActivityError(Activity? activity, Exception ex)
activity?.SetTag("error.type", ex.GetType().FullName);
activity?.SetStatus(ActivityStatusCode.Error);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1381,6 +1381,11 @@ public async Task<int> ServicesAndParams(int value, [FromService] Service1 servi
}
return total + value;
}

public int ServiceWithStringAttribute([FromService] Service1 service, string value)
{
return 115;
}

public int ServiceWithoutAttribute(Service1 service)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4679,6 +4679,23 @@ public async Task HubMethodCanInjectService()
Assert.True(Assert.IsType<bool>(res.Result));
}
}

[Fact]
public async Task HubMethodCanInjectServiceWithNullParameter()
{
var serviceProvider = HubConnectionHandlerTestUtils.CreateServiceProvider(provider =>
{
provider.AddSingleton<Service1>();
});
var connectionHandler = serviceProvider.GetService<HubConnectionHandler<ServicesHub>>();

using (var client = new TestClient())
{
var connectionHandlerTask = await client.ConnectAsync(connectionHandler).DefaultTimeout();
var res = await client.InvokeAsync(nameof(ServicesHub.ServiceWithStringAttribute),(string)null ).DefaultTimeout();
Assert.Equal(115L, res.Result);
}
}

[Fact]
public async Task HubMethodCanInjectMultipleServices()
Expand Down Expand Up @@ -4764,7 +4781,6 @@ public async Task ServiceNotResolvedWithoutAttribute_WithSettingDisabledGlobally
Assert.Equal("Failed to invoke 'ServiceWithoutAttribute' due to an error on the server. InvalidDataException: Invocation provides 0 argument(s) but target expects 1.", res.Error);
}
}

[Fact]
public async Task ServiceResolvedWithoutAttribute()
{
Expand All @@ -4785,7 +4801,6 @@ public async Task ServiceResolvedWithoutAttribute()
Assert.Equal(1L, res.Result);
}
}

[Fact]
public async Task ServiceResolvedForIEnumerableParameter()
{
Expand Down
Loading