【发布时间】:2017-09-26 19:49:15
【问题描述】:
我是第一次尝试 SqlDepenedency。我没有收到有关数据库更新的任何通知。
我在里面放置断点:
OnChange(object sender, SqlNotificationEventArgs e),
但它永远不会被击中。
这是我的代码:
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = "Cache Refresh: " + DateTime.Now.ToLongTimeString();
DateTime.Now.ToLongTimeString();
// Create a dependency connection to the database.
SqlDependency.Start(GetConnectionString());
using (SqlConnection connection = new SqlConnection(GetConnectionString()))
{
using (SqlCommand command = new SqlCommand(GetSQL(), connection))
{
SqlDependency dependency =
new SqlDependency(command);
// Refresh the cache after the number of minutes
// listed below if a change does not occur.
// This value could be stored in a configuration file.
connection.Open();
dgHomeRequests.DataSource = command.ExecuteReader();
dgHomeRequests.DataBind();
}
}
}
private string GetConnectionString()
{
// To avoid storing the connection string in your code,
// you can retrieve it from a configuration file.
//return "Data Source=(local);Integrated Security=true;" +"Initial Catalog=AdventureWorks;";
return ConfigurationManager.ConnectionStrings["TestData"].ConnectionString;
}
private string GetSQL()
{
return "Select [Address] From [UserAccount1]";
}
void OnChange(object sender, SqlNotificationEventArgs e)
{
// have breakpoint here:
SqlDependency dependency = sender as SqlDependency;
// Notices are only a one shot deal
// so remove the existing one so a new
// one can be added
dependency.OnChange -= OnChange;
// Fire the event
/* if (OnNewMessage != null)
{
OnNewMessage();
}*/
}
我还在 Global.asax 文件中放置了一些代码:
public class Global : HttpApplication
{
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
SqlDependency.Start(ConfigurationManager.ConnectionStrings["TestData"].ConnectionString);
}
protected void Application_End()
{
// Shut down SignalR Dependencies
SqlDependency.Stop(ConfigurationManager.ConnectionStrings["TestData"].ConnectionString);
}
}
SQL 服务器在本地机器上。我正在通过 Visual Studio(IIS Express) 运行代码。
-
在数据库上启用服务代理:
ALTER DATABASE SET ENABLE_BROKER 去吧
-
要订阅查询通知,我们需要授予 IIS 服务帐户权限
GRANT SUBSCRIBE QUERY NOTIFICATIONS TO “<serviceAccount>”
我猜第二点不需要,因为它是本地的。但我试着给它一些权限。不知道他们是否正确,因为我认为它没有使用应用程序池。并且不需要本地环境的许可。如果我自己是用户并自己创建了架构。
我看到的一个问题是:
alter authorization on database::<dbName> to [sa];
我也给予了许可。
【问题讨论】:
-
您能否检查两件事:1) 通知已“设置”,请参阅
sys.dm_qn_subscriptions和 2) 通知消息未保留在sys.transmission_queue中 -
@RemusRusanu 我查看了视图,为
sys.dm_qn_subscriptions和sys.transmission_queue选择了前 1000 行。他们都在那里,他们都是空的。有什么异常吗? -
sys.qn_subscriptions中应该有一行。我怀疑您的查询通知会立即失效,因为它违反了here 列出的限制之一。您可以使用基本的SqlDependency而不是SqlCacheDependency并检查您在SqlNotificationEventArgs中获得的Info、Source和Type值 -
我在
sys.dm_qn_subscriptions中有一行,只要我在我的表中插入一些东西,sys.dm_qn_subscriptions中的行就消失了!所以我希望查询通知不会失效..我现在使用您所说的链接中提到的两部分 dbName。 -
除了我上面的评论,因为我是第一次这样做,你能检查我的 OnChange 函数是否正确,如果需要任何额外的步骤,是否应该启动数据库更改?因为我确实看到在我的应用程序洞察中创建了一个依赖项。
标签: sql asp.net webforms sqldependency