Controller内部的SqlDependencyEx事件未被触发

时间:2017-07-25 20:03:31

标签: c# asp.net-mvc-4 sqldependency sqldependencyex

我正在尝试使用SqlDependencyEx,问题是OnDataChange事件没有在控制器内被触发,我已经启用了Service Broker。这是代码:

    private const string CONNECTION_STRING = "Server=LFTCMCPTP83;Database=Database;Trusted_Connection=True;MultipleActiveResultSets=true; Integrated Security=false;User ID=used_id;Password=password";
    private const string DATABASE_NAME = "db_name";
    private const string TABLE_NAME = "table_name";
    private const string SCHEMA_NAME = "dbo";
    private SqlDependencyEx sqlDependency = new SqlDependencyEx(CONNECTION_STRING, DATABASE_NAME,
    TABLE_NAME, SCHEMA_NAME);
    private void RegisterNotification()
    {
        sqlDependency.TableChanged += OnDataChange;
        sqlDependency.Start();
    }
    private void OnDataChange(object sender, SqlDependencyEx.TableChangedEventArgs e)
    {
        //Code
    }
    public IActionResult Create(){
         RegisterNotification();
    }

最好的问候

1 个答案:

答案 0 :(得分:1)

问题解决了,这是代码:

private const string CONNECTION_STRING = "Server=LFTCMCPTP83;Database=Database;Trusted_Connection=True;MultipleActiveResultSets=true; Integrated Security=false;User ID=used_id;Password=password";
private const string DATABASE_NAME = "db_name";
private const string TABLE_NAME = "table_name";
private const string SCHEMA_NAME = "dbo";
private SqlDependencyEx sqlDependency = new SqlDependencyEx(CONNECTION_STRING, DATABASE_NAME,
TABLE_NAME, SCHEMA_NAME);
listener.TableChanged += (o, args) =>
{
   //Code...
};
listener.Start();
listener.Stop();

最好的问候