2

I am using SqlDependecy with signalR to push notifications to client browser when there is some database changes, I followed this and this post and everything works fine in local SqlExpress version 11.0 with Local Connection String , but i have some kind of permissions problem when i connect to remote database hosted in GoDaddy with Production Connection String

Working Local ConnectionString

<!--<add name="NotifyConnection" providerName="System.Data.SqlClient" connectionString=
"Data Source=.\SQLExpress;Initial Catalog=TestDB;Integrated Security=SSPI;" />-->

Production ConnectionString

<add name="NotifyConnection" connectionString="Server=000.00.00.000;Database=TestDB;
User Id=UserName;Password=YourPassword;" providerName="System.Data.SqlClient" />

Get Data Method

public IEnumerable<Order> GetData()
{

using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings
["NotifyConnection"].ConnectionString))
{

using (SqlCommand command = connection.CreateCommand())
{

command.CommandType = CommandType.Text;

command.CommandText = "SELECT OrderID,CustomerID FROM dbo.[RestUser]";

command.Notification = null;

SqlDependency dependency = new SqlDependency(command);
dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);

if (connection.State == ConnectionState.Closed)
connection.Open();

using (var reader = command.ExecuteReader())
return reader.Cast<IDataRecord>()   // Here the Error throws
       .Select(x => new Order()
       {
       OrderID = x.GetInt32(0),
       CustomerID = x.GetInt32(1)                   
       }).ToList();


 }
 }
 }

What i have tried ?

I followed this post to Grant permissions in sql server , but not sure is this correct method to follow.

USE YourDatabaseName;

CREATE QUEUE NameChangeQueue;

CREATE SERVICE NameChangeService ON QUEUE NameChangeQueue ([http://schemas.microsoft.com/
SQL/Notifications/PostQueryNotification]);

GRANT SUBSCRIBE QUERY NOTIFICATIONS TO YourUserName; // Here i get this error:

//Cannot grant, deny, or revoke permissions to sa, dbo, entity owner, information_schema, 
  sys, or yourself.

ALTER DATABASE YourDatabaseName SET ENABLE_BROKER;  // Broker is already enabled

Screen Shot:

enter image description here

I am new to SqlDependency, how to fix this issue ?

Any help would be great.

Shaiju T
  • 6,201
  • 20
  • 104
  • 196
  • Check out the networking and authentication considerations in [this answer](http://stackoverflow.com/a/8316200/707618). Also, where in your code are you calling [`SqlDependency.Start()`](https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldependency.start(v=vs.110).aspx)? – Smudge202 Jul 20 '16 at 14:10
  • @Smudge202 Thanks for the comment, I am calling it in `Appliaction_Start` , and we are planning to move to azure, but in azure `SqlDependency` is not supported so we get [this](http://stackoverflow.com/questions/33693020/azure-sql-server-error-statement-receive-msg-is-not-supported-in-this-version) error, planned to use to [triggers](http://stackoverflow.com/questions/9880091/monitor-data-changes-in-sql-azure) to track database changes with signaR, any better suggestions ? – Shaiju T Jul 20 '16 at 15:48
  • 1
    Personally, I'm not a big fan of either SqlDependency nor Trigger approaches. Instead, I would consider publishing messages to a Service Bus (or queue of some form). One listener can persist the data to the database (as I'm sure you're doing now), whilst another listener can handle your SignalR push notifications. It not only solves this issue but gives you a layer of resilience against service outage. That's how I'd approach it, but I understand that doesn't resolve your SqlDependency problems directly, sorry. – Smudge202 Jul 20 '16 at 16:25
  • @stom Hi i am facing the same issue. same requirement same problem. did you fixed this bug ? – Narasappa Jul 25 '16 at 13:40
  • @Narasappa, I had issue with Go daddy database server, in shared hosting they restrict some features, are you using shared hosting ? and trying to show notification with signalR ? – Shaiju T Jul 25 '16 at 13:56
  • @stom thanks. we too using GoDaddy shared server.. Any solution for this problem ? – Narasappa Jul 26 '16 at 04:19
  • @Narasappa , one solution for using `SqlDependency` is getting dedicated server in Godaddy which will have all permissions or I have posted my solution check [this](http://stackoverflow.com/a/38587725/2218697) to send notification without `SqlDependency`, i have used asp.net mvc, hope you get some idea, Let me know your solution. – Shaiju T Jul 27 '16 at 07:17

1 Answers1

0

In shared hosting because they restrict some features, so i was unable to use SqlDependency, but here is my solution to Play notifications without SqlDependency in asp mvc

If you are new signalR, then first try this post to create simple chat web application.

My requirement was to play notifications when new sales happens in shops

1. Create SignalR Server Hub

SignalR server hub class that sends messages to all clients browser.

[HubName("PascalCaseNewSalesHub")]
public class NewSalesHub : Hub
{

    public void Send(string shopid)
    {
        // Call the alertNewSalesToPage method to update clients.

        Clients.All.alertNewSalesToPage(shopid);
    }

}

2. Javascript Send Method in PlaceOrder View

When a customer places new order for this shop then the following javascript code Calls the Send method on the server hub to update clients.

<script>

$(function () 
{
// Reference the auto-generated proxy for the hub.
var chat = $.connection.PascalCaseNewSalesHub;

var thisShopID = @(ViewBag.ShopID);


// Start the connection.
$.connection.hub.start().done(function () {

// Call the Send method on the hub to send this shops ID

chat.server.send(thisShopID);


});
});

3. Javascript Client Call Back Method in ShopSales View

The hub class on the server calls this javascript function to push content updates to each client.

<script type="text/javascript">
$(function () 
{

console.log('Page loaded');

// Declare a proxy to reference the hub.

var notifications = $.connection.PascalCaseNewSalesHub;

if (notifications != null)
{
 console.log('connected to SalesHUB proxy');
}

var thisShopID = @(ViewBag.ShopID);

// Create a function that the hub can call back to alert new sales.

notifications.client.alertNewSalesToPage = function (shopid)
{
// check if sales happened for this shop then play notification

if (shopid == thisShopID) 
{

 var sound =new Howl({
          src: ['../sounds/rings.mp3','../sounds/rings.wav','../sounds/rings.ogg',
          '../sounds/rings.aiff'],
          autoplay: true,
          loop: true
          });

          sound.play();

          $('#loading').show();

          // Partial View to Update LatestSales for this Shop

          $("#new-Sales").load('@Url.Action("GetLatestSales", "Shop")')

          $('#loading').hide();

          console.log('New Sale happened, Notification Played');
        }

    };


    // Start the connection.
    $.connection.hub.start().done(function () {


        console.log('signalR connection started');


    }).fail(function (e) {
        alert(e);
    });
});



</script>

Used Howler.js Plugin to play notification , check this post.

Hope helps someone.

Community
  • 1
  • 1
Shaiju T
  • 6,201
  • 20
  • 104
  • 196