Exemple #1
0
 private void RemoveNameOwnerWatcher(string key, OwnerChangedMatchRule rule, Action <ServiceOwnerChangedEventArgs, Exception> dlg)
 {
     lock (_gate)
     {
         if (_nameOwnerWatchers?.ContainsKey(key) == true)
         {
             _nameOwnerWatchers[key] = (Action <ServiceOwnerChangedEventArgs, Exception>)Delegate.Remove(_nameOwnerWatchers[key], dlg);
             if (_nameOwnerWatchers[key] == null)
             {
                 _nameOwnerWatchers.Remove(key);
                 CallRemoveMatchRule(rule.ToString());
             }
         }
     }
 }
Exemple #2
0
        public async Task <IDisposable> WatchNameOwnerChangedAsync(string serviceName, Action <ServiceOwnerChangedEventArgs, Exception> handler)
        {
            var    rule = new OwnerChangedMatchRule(serviceName);
            string key  = serviceName;

            Task task = null;

            lock (_gate)
            {
                ThrowIfNotConnected();
                ThrowIfRemoteIsNotBus();

                if (_nameOwnerWatchers.ContainsKey(key))
                {
                    _nameOwnerWatchers[key] = (Action <ServiceOwnerChangedEventArgs, Exception>)Delegate.Combine(_nameOwnerWatchers[key], handler);
                    task = Task.CompletedTask;
                }
                else
                {
                    _nameOwnerWatchers[key] = handler;
                    task = CallAddMatchRuleAsync(rule.ToString());
                }
            }
            NameOwnerWatcherRegistration registration = new NameOwnerWatcherRegistration(this, key, rule, handler);

            try
            {
                await task;
            }
            catch
            {
                registration.Dispose();
                throw;
            }
            return(registration);
        }