问题
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:
I am new to SqlDependency
, how to fix this issue ?
Any help would be great.
回答1:
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.
来源:https://stackoverflow.com/questions/38456433/cannot-find-the-object-querynotificationerrorsqueue-because-it-does-not-exist