我有SqlTableDependency的问题。当我对期望表进行插入/更新/删除时,不会调用我的Changed方法。 OnStatusChanged事件工作正常。

 string conn = @"data source=secret server; integrated security=True; initial catalog=secret db;User id=secret user";

    var mapper = new ModelToTableMapper<SqlDataModel>();

    mapper.AddMapping(c => c.datavalue, "datavalue");

    using (var dep = new SqlTableDependency<SqlDataModel>(conn, "data", mapper))
    {
        dep.OnChanged +=  Changed;
        dep.OnStatusChanged += OnStatusChanged;
        dep.OnError += OnError;
        dep.TraceLevel = TraceLevel.Verbose;
        dep.TraceListener = new TextWriterTraceListener(Console.Out);
        dep.Start();

        Console.WriteLine("Press a key to exit");
        Console.ReadKey();
        dep.Stop();
    }
}
static void OnStatusChanged(object sender, StatusChangedEventArgs e)
{
    Console.WriteLine(e.ToString());
}
static void OnError(object sender, ErrorEventArgs e)
{
    Console.WriteLine(e.ToString());
}
static void Changed(object sender, RecordChangedEventArgs<SqlDataModel> e)
{
    if (e.ChangeType != ChangeType.None)
    {
        var changedEntity = e.Entity;
        Console.WriteLine("DML operation: " + e.ChangeType);
        Console.WriteLine("value: " + changedEntity.datavalue);
    }
}

以上代码我基于https://tabledependency.codeplex.com/wikipage?title=SqlTableDependency
我确定我已经使用db_owner角色。我启用了代理,我看到在mssql db中创建了trigers,services等。

c# - SqlTableDependency onchange事件未触发-LMLPHP

最佳答案

我最终在sys.transmission_queue表中发现错误:
将消息放入目标队列时发生异常。错误:



即使数据库已经我完全授予自己的权限,我仍然没有做

ALTER AUTHORIZATION ON DATABASE::secret db TO sa

10-04 11:33