public ContactsSubscription(SubscriptionClient subscriptionClient, string dbConnectionString) { this.subscriptionClient = subscriptionClient; this.options = OptionsFactory.NewDbOptions <CommunicationsContext>(dbConnectionString); this.handlers = new Dictionary <Type, Action <string, CommunicationsContext> >() { { typeof(ContactCreatedEventNotification), (notificationJson, context) => { var notificationEvent = JsonConvert.DeserializeObject <ContactCreatedEventNotification>(notificationJson); var handler = new ContactCreatedHandler(context); handler.Handle(notificationEvent).Wait(); } }, { typeof(ContactRemovedEventNotification), (notificationJson, context) => { var notificationEvent = JsonConvert.DeserializeObject <ContactRemovedEventNotification>(notificationJson); var handler = new ContactRemovedHandler(context); handler.Handle(notificationEvent).Wait(); } } }; }
/// <summary> /// Safely handles all ContactCreatedHandler event invocation /// </summary> /// <param name="contact">Contact event argument</param> private void SafeInvokeContactCreated(Contact contact) { if (ContactCreated == null) { return; } ContactCreatedHandler listener = null; Delegate[] dels = ContactCreated.GetInvocationList(); foreach (Delegate del in dels) { try { listener = (ContactCreatedHandler)del; listener.Invoke(contact); } catch (Exception e) { ContactCreated -= listener; } } }