If you ever have a situation where you have to mix few of the Web based technologies in single solution, you would start to wonder why these different technologies have their own way of dealing with Dependency Injection and IoC containers.
For example:
MVC 5
Well, ASP.NET MVC5 has its own namespaces and DependencyResolver instances that can be updated. You can find examples here
ASP.NET Core
You need to use a different infrastructure ("Services") to inject and resolve the dependencies.
Web API
As per WebAPI documentation, you need to implement your own resolver by implementing IDependecyResolver interface available under System.Web.Http.Dependencies namespace. You can then replace the default resolver with your resolver by updating the instance of HttpConfiguration used by WebAPI.
SignalR
As per SignalR documentation, it is to be done through a static class called GlobalHost. It exposes the SignalR related DependencyResolver as a property and we can use that to register types so that SignalR related objects can be automagically be created.
public void Configuration(IAppBuilder app)
{
GlobalHost.DependencyResolver.Register(
typeof(ChatHub),
() => new ChatHub(new ChatMessageRepository()));
}
There is a interface IDependencyResolver too under its own dedicated namespace which can be used to write own resolver and assign the same to HubConfiguration class during start up. However, recommended way is to inherit from DefaultDependencyResolver and add custom logic - this is because DefaultDependencyResolver takes care of resolving SignalR related infrastructure.
For example:
MVC 5
Well, ASP.NET MVC5 has its own namespaces and DependencyResolver instances that can be updated. You can find examples here
ASP.NET Core
You need to use a different infrastructure ("Services") to inject and resolve the dependencies.
Web API
As per WebAPI documentation, you need to implement your own resolver by implementing IDependecyResolver interface available under System.Web.Http.Dependencies namespace. You can then replace the default resolver with your resolver by updating the instance of HttpConfiguration used by WebAPI.
SignalR
As per SignalR documentation, it is to be done through a static class called GlobalHost. It exposes the SignalR related DependencyResolver as a property and we can use that to register types so that SignalR related objects can be automagically be created.
public void Configuration(IAppBuilder app)
{
GlobalHost.DependencyResolver.Register(
typeof(ChatHub),
() => new ChatHub(new ChatMessageRepository()));
}
There is a interface IDependencyResolver too under its own dedicated namespace which can be used to write own resolver and assign the same to HubConfiguration class during start up. However, recommended way is to inherit from DefaultDependencyResolver and add custom logic - this is because DefaultDependencyResolver takes care of resolving SignalR related infrastructure.