4

Is is possible to use SignalR in combination 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 ).

2 Answers 2

5

You 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>(); 
...
Sign up to request clarification or add additional context in comments.

1 Comment

Just FYI, we plan to implement a scaleout solution based on SQL using this mechanism.
1

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 completeness, 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.net mvc to manage the sqldependency, this feels like a more integrated solution. Check this post on how to implement background processing in asp.net "the right way" http://haacked.com/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);
        }
    }
}

4 Comments

Why do you need to monitor the SQL Dependency in a separate process? You could just monitor it in a thread running under ASP.NET unless there's some infrastructure reason why your web site can't monitor your database directly?? In any case, if you do want to have an API like this you'd be better off using the new ASP.NET Web API, and you should probably make it an HttpPost only API to prevent accidental (or malicious) calls simply by visiting a web page.
@Hightechrider thx for your response, i was planning on implementing authentication to prevent malicious use, but a post would probably still be better to use to. I am not sure how to start a separate thread from inside my asp.net mvc application, at what point should i start the thread ( i'm thinking in Application_start from global asax, but i'm not sure )
Yes, starting it in Global.asax is typical. Of course the AppPool could shut down when idle (which is why you normally need a service) but in your case, if nobody is using the site (through SignalR or otherwise) you don't care if the database is being updated.
Thx for the info Hightechrider ! I'm currently basing my implantation on this post haacked.com/archive/2011/10/16/… , i feel like using IRegisterOject gives me much more control, i always want to know where my threads are at, if you catch my drift.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.