Asp.net mvc 在NServiceBus中处理事件时不调用客户端函数的信号器
我有一个MVC应用程序,我正在显示数据库中的记录,我可以创建一个新记录。当Nservicebus的IEvent处理程序完成时,我使用SignalR通知客户端 Index.cshtmlAsp.net mvc 在NServiceBus中处理事件时不调用客户端函数的信号器,asp.net-mvc,nservicebus,signalr,Asp.net Mvc,Nservicebus,Signalr,我有一个MVC应用程序,我正在显示数据库中的记录,我可以创建一个新记录。当Nservicebus的IEvent处理程序完成时,我使用SignalR通知客户端 Index.cshtml <script src="signalr/hubs" type="text/javascript"></script> <script> var myHub; $(function () { myHub = $.connection.userAc
<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>
UserAccountHub
public class UserAccountHub : Hub
{
}
UserAccountCreatedNotifyEventHandler.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 UserAccountCreatedNotifyEventHandler : IHandleMessages<UserAccountCreatedNotifyEvent>
{
public void Handle(UserAccountCreatedNotifyEvent message)
{
IConnectionManager connectionManager = AspNetHost.DependencyResolver.Resolve<IConnectionManager>();
dynamic clients = connectionManager.GetClients<UserAccountHub>();
clients.testMsg();
}
}
公共类UserAccountCreatedNotifyEventHandler:IHandleMessages
{
公共无效句柄(UserAccountCreatedNotifyEvent消息)
{
IConnectionManager connectionManager=AspNetHost.dependencysolver.Resolve();
动态客户端=connectionManager.GetClients();
clients.testMsg();
}
}
基本上,我转到索引操作,它只显示我的所有记录,并有一个创建按钮。我点击create按钮@Html.ActionLink(“create”,“create”,null,null)
,它调用了公共ActionResult创建(CreateUserAccountModel user)
方法。启动总线,然后重定向到索引操作。服务总线完成了它的工作,并且相应地触发了UserAccountCreatedNotifyEventHandler方法
这就是我开始看到一些问题的地方。我调用适当的信号器方法来获取客户机,这样我就可以广播一条消息.testMsg()
,但是客户机没有收到消息
因此,简而言之,我的signerclients.testMsg
调用并没有按预期进行。据我所知,我正在遵循我在web上找到的代码示例,甚至是我的其他测试项目。我假设我在做一些愚蠢的事情,但就是不能瞄准它。在处理程序中,您需要为中心创建一个代理,并调用代理上的方法。好吧,注入代理,因为每次创建代理都会很昂贵
请参见试一试
myHub.client.testMsg = function(){....}
不是
我使用了类似的模式,但rabbitmq handlersI实际上发现了我的NServicebus配置的一个问题,修复了一点问题。现在奇怪的是,我上面的代码第一次就可以工作,但之后就不工作了。我准备深入研究并尝试.CreateProxy方法。好的,我有更多信息。由于尝试启动连接时出现401个未经授权的错误(通过fiddler查看),我无法使CreateProxy方法工作。我还从上面进一步测试了我的代码,并为我添加了更多的谜团。如果我打开两个浏览器,只打开第二个浏览器,它就会一直正常工作。Fiddler确实显示了……的授权错误。。。。。。。有什么见解吗?我认为这与NServicebus是一个独立的服务这一事实有关。Windows身份验证。我尝试将windows凭据添加到连接。凭据无效。我一直在看这条路线:
myHub.testMsg = function(){....}