2012-04-16 50 views
1

我有一個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呼叫並不按預期行事。據我所知,我遵循我在網上找到的代碼示例,甚至還有其他的測試項目。我假設我正在做一些愚蠢的事情,但只是不能瞄準它。

回答

2

在您的處理程序中,您需要爲集線器創建代理並調用代理上的方法。那麼,注入代理,因爲每次創建它都會很昂貴!

http://www.asp.net/signalr/overview/signalr-20/hubs-api/hubs-api-guide-net-client

+0

我給一個嘗試。 – Etch 2012-04-16 21:09:05

+0

我使用類似的模式,但從rabbitmq處理程序 – redsquare 2012-04-16 21:17:50

+0

我實際上發現我的NServicebus配置問題,解決了一些問題。現在奇怪的是,我的上面的代碼第一次工作,但沒有工作。我準備好深入並嘗試使用.CreateProxy方法。 – Etch 2012-04-17 16:05:56

0

嘗試

myHub.client.testMsg = function(){....} 

myHub.testMsg = function(){....}