Introducing dependency injection (simple approach)
Let's now enter a different territory, where we'll learn how to fully integrate our code with the internal behaviors of SignalR. SignalR offers a powerful way to extend its functionalities through a dependency injection API and strategy. Thanks to that, we can easily hook into the Hub creation process, and consequently we have a way to inject other objects into it and make them available to its methods.
In this recipe, we'll illustrate how we can transparently inject a specific reusable logic inside a Hub when it's created. The Hub will expose a simple method receiving a string and returning a corresponding translation obtained from a translator service injected at its creation.
Getting ready
For this recipe, we'll use a new empty web application, which we'll call Recipe42
.
How to do it…
After creating our empty web application, we will add all the parts we need using the following steps:
We start with a new Hub called
EchoHub
, created by using...