Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inject dependencies into SignalR hub method #16686

Open
Daniel15 opened this issue Oct 31, 2019 · 4 comments
Labels
Milestone

Comments

@Daniel15
Copy link

@Daniel15 Daniel15 commented Oct 31, 2019

Is there a way to inject dependencies into a SignalR hub method, similar to how we can use FromServicesAttribute with ASP.NET MVC to inject a service into an action method? I tried just listing the dependency in the method signature but got an error "Failed to invoke foo due to an error on the server"

@davidfowl

This comment has been minimized.

Copy link
Member

@davidfowl davidfowl commented Oct 31, 2019

No there isn't. It's something we could look at in the future though.

@jkotalik jkotalik added this to the Backlog milestone Oct 31, 2019
@halter73

This comment has been minimized.

Copy link
Member

@halter73 halter73 commented Oct 31, 2019

FWIW, the lifetime of the hub is typically only the duration of a Hub method invocation. I think the same is true for Controllers and Actions for that matter. So ctor injection might not look as nice, it should work more or less the same.

@Daniel15

This comment has been minimized.

Copy link
Author

@Daniel15 Daniel15 commented Oct 31, 2019

The issue is if each hub method requires different dependencies, you end up with a giant constructor, and may construct dependencies that aren't actually needed by the method that's being called. :)

I haven't tested but I guess injecting the IServiceProvider into the constructor would work, then I could use that or ActivatorUtilities in the hub method.

@halter73

This comment has been minimized.

Copy link
Member

@halter73 halter73 commented Oct 31, 2019

I haven't tested but I guess injecting the IServiceProvider into the constructor would work, then I could use that or ActivatorUtilities in the hub method.

That should definitely work, but I understand that's far from ideal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.