Using SignalR with SqlDependency to Update the Database

Can SignalR be used in conjunction with SqlCacheDependency (or SqlDependency) to directly update databases in the browser? Perhaps there is another way to achieve this functionality?

The only thing I can get right now is to include a call to addMessage from an asynchronous call that updates the data, but that doesn't really apply to updates from different sources (like a background service updating a table).


source to share

2 answers

Ok I figured it out, or at least one way to do it.

I didn't realize at first that you need to use this code from an mvc controller, once you do that, you can also call that controller from any other location or application using <href = "class en-us / library / debx8sh9.aspx "rel =" nofollow "> WebRequest.

@Hightechrider In order to complete the completeness, you need to include 2 more links to make this part of the code. This demo code is executed using a PersistentConnection, but the principle for the hub remains the same.

EDIT: I am now using a stream inside my mvc to manage sqld dependency, it seems like a more integrated solution. Check out this post on how to implement background processing in "correctly", 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>();





You can use the OnChange event in SQLDependency . In your event handler, you can send a message via SignalR. Since you will be calling your hub from outside, you will 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>(); 




All Articles