Skip to content

Dependency Injection Conflict with Null Parameter in SignalR Hub Methods #61491

@pranjalg-dew

Description

@pranjalg-dew

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

I am trying to inject a service into a hub method using Dependency Injection (DI). I have explicitly specified the [FromServices] attribute before the parameter that should be resolved from DI in the hub method.

Currently, I have multiple hub methods that start with a service parameter. Recently, I encountered an issue with such an arrangement. When I pass a null value for the parameter following the service parameter, the service parameter fails to resolve its dependency.

It seems that the hub method is trying to resolve dependencies from both the client message and the DI container. Consequently, placing the injecting parameter first may lead to improper binding of the null value.

I also tried using the DisableImplicitFromServicesParameters option, but the same issue persists.

Then, I tried placing the service parameter last, and it worked fine.

Expected Behavior

When explicitly marked with the [FromServices] attribute, the service parameter should reliably resolve its dependency from the Dependency Injection (DI) container, regardless of the order of parameters in the hub method.

Steps To Reproduce

  1. Clone the sample project repository from https://github.com/pranjalg-dew/AspNetCore-SignalR-BugRepro.
  2. Build and run the project locally.
  3. Open the web page hosted in the repository which can send values to the SignalR endpoint.
  4. Keep textbox empty and click on send message button.
  5. Observe the resolution of the [FromServices] IMyService service parameter. Note that the service parameter is null.

Exceptions (if any)

No response

.NET Version

No response

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-signalrIncludes: SignalR clients and serversbugThis issue describes a behavior which is not expected - a bug.good first issueGood for newcomers.help wantedUp for grabs. We would accept a PR to help resolve this issue

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions