Domanda

Voglio verificare se Service Broker è in esecuzione utilizzando il codice e in base allo stato, avviare SqlDependency o no. Come posso fare?

È stato utile?

Soluzione

Si può fare una semplice query:

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

In alternativa si può semplicemente avviare lo SqlDependency e intercettare l'errore che si ottiene se non è stata attivata, ma il primo metodo è più semplice e meglio:

  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.
  }
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top