私は
クエリの下に実行されない場合は、(データベース名を変更)、is_broker_enabled列が1に設定された出力チェックでは、サービスブローカー
SELECT [name], [service_broker_guid], [is_broker_enabled] FROM [master].[sys].[databases]
を有効にする必要が二query.Firstと答えを開始します
ALTER DATABASE sampleNotifications SET ENABLE_BROKER WITH ROLLBACK IMMEDIATE ;
このデータベースに監視する必要なテーブルを追加します。あなたのWEBAPIでは、あなたのWEBAPIのweb.config
でこのデータベースの接続文字列を追加しますsignalRが インストール・パッケージMicrosoft.AspNet.SignalR
をハブを作成nuget、インストールフォルダとsignalRハブクラスを追加して持つことができますglobal.asax.csまたはstartup.cs
RouteTable.Routes.MapHubs(); //for global.asax.cs and will work only for signalR 1.1. From 2.0 onwards you need to have startup class
における基本的なonConnected方法
public override Task OnConnected()
{
//you can log the connection ID.
return base.OnConnected();
}
登録signalRか
app.MapSignalR(); //startup.cs
は今、最も重要な部分はSQLDependencyクラスを開始し、変更event.The重要な部分で処理することですあなたは私がのglobal.asaxに追加した簡単な方法下記のように、監視している正確に定義する必要がありますされます。sampleNotifications DB
private void RegisterSQLNotifications()
{
string connectionString = ConfigurationManager.ConnectionStrings["sampleNotifications"].ConnectionString;
SqlDependency.Start(connectionString);
string commandText = @"Select * from dbo.TestNotifications";
using (SqlConnection connection = new SqlConnection(connectionString))
{
using (SqlCommand command = new SqlCommand(commandText, connection))
{
connection.Open();
var sqlDependency = new SqlDependency(command);
sqlDependency.OnChange += sqlDependency_OnChange;
// NOTE: You have to execute the command, or the notification will never fire.
using (SqlDataReader reader = command.ExecuteReader())
{
}
}
}
}
void sqlDependency_OnChange(object sender, SqlNotificationEventArgs e)
{
if (e.Info == SqlNotificationInfo.Insert)
{
//This is how signalrHub can be accessed outside the SignalR Hub MyHub.cs file
// you can add your business logic here, like what exactly needs to be broadcasted
var context = GlobalHost.ConnectionManager.GetHubContext<MyHub>();
context.Clients.All.sendNotifications();
}
//Call the RegisterSQLNotifications method again
RegisterSQLNotifications();
}
コールglobal.asax.csの中のApplication_Start RegisterSQLNotificationsのTestNotificationsテーブルを監視CS。テーブルに挿入があるたびに、sqlDependency_OnChangeイベントがトリガーされ、それぞれのクライアントにブロードキャストできます。
クライアント側では、HTMLページで以下を追加します。 WPFプロジェクトに合わせてこれを変更することができます。
var connection = $ .hubConnection(); connection.url = "http://localhost:40471/signalr"; // APIのURL VAR alertsHubProxy = connection.createHubProxy( 'MyHub')あなたの最初のクエリについて
//broadcast alert
alertsHubProxy.on('sendNotifications', function (item) {
//do something here
});
- 2台の異なるマシンにWEBAPIとデータベースを実行しても大丈夫でなければなりません。複数のIISインスタンスは、あなたが私の答えを確認することができます最も近いリンク http://techbrij.com/database-change-notifications-asp-net-signalr-sqldependency
http://www.codeproject.com/Articles/883702/Real-Time-Notifications-using-SignalR-and-SQL-Depe
http://venkatbaggu.com/signalr-database-update-notifications-asp-net-mvc-usiing-sql-dependency/
の
http://www.asp.net/signalr/overview/performance/scaleout-with-sql-server
カップルを使用しているこのスケールアウトの例を参照してください?私は何か見落としてますか ? –