Skip to content

Windows Service #74

@jap64

Description

@jap64

Using the ServiceBrokerListener in a Windows Service, it appears that it only seems to fire one TableChanged event and then doesn't trigger any further updates, is my setup correct?

    private static SqlDependencyEx listenerTable1;
    private static SqlDependencyEx listenerTable2;
    private static SqlDependencyEx listenerTable3;

    protected SqlServiceBroker(EventLog eventLog)
    {
        _eventLog = eventLog;
        _connectionString = ConfigurationManager.ConnectionStrings["xxxxx"].ConnectionString;
        _brokerDatabase = ConfigurationManager.AppSettings["BrokerDatabase"];
        _isDebug = Convert.ToBoolean(ConfigurationManager.AppSettings["IsDebug"]);
    }

    public static SqlServiceBroker Instance(EventLog eventLog)
    {
        // Uses lazy initialization.
        // Note: this is not thread safe.
        if (_instance == null)
            _instance = new SqlServiceBroker(eventLog);

        return _instance;
    }

    public void Start()
    {
        //table 1 listener
        listenerTable1 = new SqlDependencyEx(_connectionString, _brokerDatabase, "Table1", identity: 1);
        listenerTable1.TableChanged += Table1Changed;
        listenerTable1.NotificationProcessStopped += ListenerTable1_NotificationProcessStopped;
        listenerTable1.Start();

        //table 2 listener
        listenerTable2 = new SqlDependencyEx(_connectionString, _brokerDatabase, "Table2", identity: 2);
        listenerTable2.TableChanged += Table2Changed;
        listenerTable2.NotificationProcessStopped += ListenerTable2_NotificationProcessStopped;
        listenerTable2.Start();

        //table 2 listener
        listenerTable3 = new SqlDependencyEx(_connectionString, _brokerDatabase, "Table3", identity: 3);
        listenerTable3.TableChanged += Table3Changed;
        listenerTable3.NotificationProcessStopped += ListenerTable3_NotificationProcessStopped;
        listenerTable3.Start();

        if (_isDebug)
            _eventLog.WriteEntry(string.Format("We have {0} dependency listeners", GetDependenciesCount()));

        Thread.Sleep(1000);
    }

    private void ListenerTable1_NotificationProcessStopped(object sender, EventArgs e)
    {
        listenerTable1.Stop();
        listenerTable1.Start();
    }

    private void ListenerTable2_NotificationProcessStopped(object sender, EventArgs e)
    {
        listenerTable2.Stop();
        listenerTable2.Start();
    }

    private void ListenerTable3_NotificationProcessStopped(object sender, EventArgs e)
    {
        listenerTable3.Stop();
        listenerTable3.Start();
    }

    public void Stop()
    {
        listenerTable1.Stop();
        listenerTable2.Stop();
        listenerTable3.Stop();
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions