2017-04-16 124 views
0

,我的工作得到通知,使用SignalR我的MVC應用程序的特性「notificationHub」,但我得到以下錯誤無法讀取屬性「notificationHub」的未定義而在代碼無法讀取未定義

最後一行瀏覽器的調試
public class NotificationHub : Hub 
    { 
     //public void Hello() 
     //{ 
     // Clients.All.hello(); 
     //} 
    } 



private void SqlDep_OnChange(object sender, SqlNotificationEventArgs e) 
     { 
      if (e.Type == SqlNotificationType.Change) 
      { 
       SqlDependency sqlDep = sender as SqlDependency; 
       var notificationHub = GlobalHost.ConnectionManager.GetHubContext<NotificationHub>(); 
       notificationHub.Clients.All.notify("added"); 
       RegisterNotification(DateTime.Now); 
      } 
     } 

<script src="~/Scripts/jquery-3.1.1.min.js"></script> 
    <script src="~/Scripts/jquery.signalR-2.2.1.min.js"></script> 
    <script src="/signalr/hubs" ></script> 
    <script src="~/Scripts/bootstrap.min.js"></script> 

var notificationHub = $.connection.notificationHub; 
+0

請顯示你的Hub類。 – 2017-04-16 19:30:15

+0

編輯後添加Hub類 –

回答

0

測繪中心以這種方式SignalR管道:

using Microsoft.Owin; 
using Owin; 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
[assembly: OwinStartup(typeof(WeatherAppDemo.SignalR.StartUp))] 
namespace WeatherAppDemo.SignalR { 
    public class StartUp { 
     public void Configuration(IAppBuilder app) { 
      app.MapSignalR(); 
     } 
    } 
} 

,我們必須做兩東西 - 1.我們第一次開始啓動類 2.廣告丁集線器到SignalR管道

現在你要檢查這個網址:

http://localhost:58416/signalr/hubs 

// http://localhost:58416這將是你的本地主機

如果你能看到的JavaScript代碼,然後您已經成功配置了SignalR Server。 然後我希望你沒有發現任何錯誤