SignalRWindsorFacility adds ability to call client side services just with injected interface
- It is testable. You are using these interfaces as any normal .NET interface
- It supports intellisense
-
Define contract for client-side service
public interface IChat { void AddMessage(string msg); }
-
Register
IChat
as client-side service associated withChatHub
public class DefaultInstaller : IWindsorInstaller { public void Install(IWindsorContainer container, IConfigurationStore store) { container.AddFacility<ClientServiceFacility>(); container.Register( Component.For<IChat>().AsClientService().WithHub<ChatHub>()); } }
-
Write your client-side logic
(function ($) { $.extend($.connection.chatHub.client, { addMessage: function(msg) { alert(msg); } }); $.connection.hub.start(); })(jQuery);
-
Inject
IChat
anywhere and call its methods as normalpublic class HomeController : Controller { readonly IChat chat; public HomeController(IChat chat) { this.chat = chat; } public ActionResult Index() { chat.AddMessage("Hello"); return View(); } }