SignalR在处理NServiceBus中的事件时没有调用客户端函数
我有一个MVC应用程序,我正在显示来自数据库的记录,并且能够创建新记录。当Nservicebus的IEvent处理程序完成时,我使用SignalR通知客户端。SignalR在处理NServiceBus中的事件时没有调用客户端函数
Index.cshtml
<script src="signalr/hubs" type="text/javascript"></script>
<script>
var myHub;
$(function() {
myHub = $.connection.userAccountHub;
//add handler to handle the nofication
myHub.testMsg = function() {
alert("I would really like for this to work");
};
$.connection.hub.start();
});
</script>
UserController.cs
public class UserController : Controller
{
private readonly IBus _bus;
public ActionResult Index()
{
return View(getalldata());
}
[HttpPost]
public ActionResult Create(CreateUserAccountModel user)
{
if (ModelState.IsValid)
{
_bus.Send(new CreateUserAccountCommand
{
FirstName = user.FirstName,
LastName = user.LastName,
NetworkLogin = user.NetworkLogin
});
return RedirectToAction("Index");
}
return View(user);
}
UserAccountHub
public class UserAccountHub : Hub
{
}
个UserAccountCreatedNotifyEventHandler.cs
public class UserAccountCreatedNotifyEventHandler : IHandleMessages<UserAccountCreatedNotifyEvent>
{
public void Handle(UserAccountCreatedNotifyEvent message)
{
IConnectionManager connectionManager = AspNetHost.DependencyResolver.Resolve<IConnectionManager>();
dynamic clients = connectionManager.GetClients<UserAccountHub>();
clients.testMsg();
}
}
基本上我去索引作用,这只是显示我的所有记录,并有一个创建按钮。我点击创建按钮@Html.ActionLink("Create", "Create", null, null)
,它叫做public ActionResult Create(CreateUserAccountModel user)
方法。启动总线,然后重定向到索引操作。服务总线完成它的事情并且UserAccountCreatedNotifyEventHandler Handle
方法被适当地触发。
这是我开始看到一些问题的地方。我打电话给适当的signalr方法来获取客户端,以便我可以广播消息.testMsg()
但是客户端没有收到消息。
因此总之,我的信号器clients.testMsg
呼叫并不按预期行事。据我所知,我遵循我在网上找到的代码示例,甚至还有其他的测试项目。我假设我正在做一些愚蠢的事情,但只是不能瞄准它。
在您的处理程序中,您需要为集线器创建代理并调用代理上的方法。那么,注入代理,因为每次创建它都会很昂贵!
见http://www.asp.net/signalr/overview/signalr-20/hubs-api/hubs-api-guide-net-client
尝试
myHub.client.testMsg = function(){....}
不
myHub.testMsg = function(){....}
我给一个尝试。 – Etch 2012-04-16 21:09:05
我使用类似的模式,但从rabbitmq处理程序 – redsquare 2012-04-16 21:17:50
我实际上发现我的NServicebus配置问题,解决了一些问题。现在奇怪的是,我的上面的代码第一次工作,但没有工作。我准备好深入并尝试使用.CreateProxy方法。 – Etch 2012-04-17 16:05:56