/// <summary> /// Main loop for listener task. /// </summary> private void NotificationLoop() { while (IsListening()) { List <NotificationMessage> messages = new List <NotificationMessage>(); try { messages = SqlProcedures.ReceiveSubscription(AppName); } catch (Exception ex) { try { ErrorMessageHandler.Invoke(new NotificationMessage(ex.Message)); } catch (Exception) { } } foreach (NotificationMessage message in messages) { try { switch (message.MessageType) { case NotificationMessageType.Error: ErrorMessageHandler.Invoke(message); break; case NotificationMessageType.NotImplementedType: ErrorMessageHandler.Invoke(message); break; case NotificationMessageType.Unsubscribed: UnsubscribedMessageHandler.Invoke(message); break; case NotificationMessageType.Empty: break; default: MessageHandler.Invoke(message); break; } } catch (Exception ex) { try { message.AddError(ex); ErrorMessageHandler.Invoke(message); } catch (Exception) { } } } } }
/// <summary> /// Creates new receiver witch specified properies. /// </summary> /// <param name="appName"> Application name for which messages listener will be listening. </param> /// <param name="connectionString"> Connection string used to connect to DB. </param> /// <param name="messageHandler"> Handler used to handle typical NotificationMessage. Default: no handler is specified. </param> /// <param name="unsubscribedMessageHandler"> Handler used to handle unsubscribe NotificationMessage. Default: messageHandler handler is used. </param> /// <param name="errorMessageHandler"> Handler used to handle unsubscribe NotificationMessage. Default: unsubscribedMessageHandler handler is used. </param> /// <param name="sqlTimeout"> Timeout in seconds used for query. Default value uses value set in constructor. 0 indicates infinite. Default: 30. </param> public Receiver( string appName, string connectionString, HandleMessage messageHandler = null, HandleMessage unsubscribedMessageHandler = null, HandleMessage errorMessageHandler = null, int sqlTimeout = 30) { AppName = appName; SqlProcedures = new SqlProcedures(connectionString, sqlTimeout); MessageHandler += messageHandler; UnsubscribedMessageHandler += unsubscribedMessageHandler ?? messageHandler; ErrorMessageHandler += errorMessageHandler ?? unsubscribedMessageHandler ?? messageHandler; }
/// <summary> /// UnSubscribe from notification. /// </summary> /// <param name="subscription"> Subscription used to unsubscribe. </param> public void UnSubscribe(Subscription subscription) { SqlProcedures.UninstallSubscription(subscription); }
/// <summary> /// Subscribes for notification. /// </summary> /// <param name="subscription"> Subscription used to subscribe. </param> public void Subscribe(Subscription subscription) { SqlProcedures.InstallSubscription(subscription); }