How can I check in C # if Broker Service is included in SQL?

I want to check if Broker Service is working using code and depending on the state, run sqld dependency or not. How can i do this?

+3
source share
1 answer

You can make a simple request:

SELECT is_broker_enabled FROM sys.databases WHERE Name = 'mydatabasename'

Alternatively, you can simply run SqlDependency and block the error you received if it was not enabled, but the first method is simpler and better:

  try {
      SqlDependency.Start();
  } catch (InvalidOperationException ex) {
      // If broker hasn't been enabled, you'll get the following exception:
      //
      // The SQL Server Service Broker for the current database is not enabled, and
      // as a result query notifications are not supported.  Please enable the Service
      // Broker for this database if you wish to use notifications.
  }
+11
source

Source: https://habr.com/ru/post/1758864/


All Articles