SignalR 在处理 NServiceBus 中的事件时不调用客户端函数



>我有一个MVC应用程序,我正在显示来自数据库的记录,并且我提供了创建新记录的功能。 我正在使用 SignalR 在来自 Nservicebus 的 IEvent 处理程序完成时通知客户端。

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>

用户控制器.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);
        }

用户账户中心

public class UserAccountHub : Hub
    {
    }

用户帐户已创建通知事件处理程序.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并相应地触发方法。

这就是我开始看到一些问题的地方。我调用适当的信号器方法来获取客户端,以便我可以广播消息.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(){....}

最新更新