Is is possible to use SignalR in bination with SqlCacheDependency (or SqlDependency) to push database updates directly to the browser ? Maybe there is an other way to achieve this functionality ?
The only thing i can get working now includes having to call addMessage from the async call that does an update to the datase, but that doesn't really cover updates from different sources ( for example a background service that updates the table ).
Is is possible to use SignalR in bination with SqlCacheDependency (or SqlDependency) to push database updates directly to the browser ? Maybe there is an other way to achieve this functionality ?
The only thing i can get working now includes having to call addMessage from the async call that does an update to the datase, but that doesn't really cover updates from different sources ( for example a background service that updates the table ).
Share Improve this question asked Mar 26, 2012 at 21:29 Willem D'HaeseleerWillem D'Haeseleer 20.2k10 gold badges68 silver badges103 bronze badges2 Answers
Reset to default 5You should be able to use the OnChange event on a SQLDependency. In your event handler you can send a message over SignalR. Since you will be calling into your Hub from outside you'll need to use the technique shown at the bottom of the documentation here :
using SignalR.Infrastructure;
IConnectionManager connectionManager = AspNetHost.DependencyResolver.Resolve<IConnectionManager>();
dynamic clients = connectionManager.GetClients<MyHub>();
...
Ok, i figured it out, or at least, one way to do it.
What i failed to understand initially is that you need to use that code from within an mvc controller, once you've done that, you can, obviously, call that controller from any other location or application as well, using the WebRequest class.
@Hightechrider For the sake off pleteness, you need to include 2 more references to make that piece of code work. This demo code is done with a PersistentConnection, but the principle for the hub is the same off course.
EDIT: I'm now using a thread inside my asp mvc to manage the sqldependency, this feels like a more integrated solution. Check this post on how to implement background processing in asp "the right way" http://haacked./archive/2011/10/16/the-dangers-of-implementing-recurring-background-tasks-in-asp-net.aspx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SignalR.Infrastructure;
using SignalR.Hosting.AspNet;
using SignalR;
namespace SignalRDemo.Controllers
{
public class DemoController : Controller
{
public void sendMessage( string message)
{
IConnectionManager connectionManager = AspNetHost.DependencyResolver.Resolve<IConnectionManager>();
IConnection connection = connectionManager.GetConnection<MyConnection>();
connection.Broadcast(message);
}
}
}