Esempio n. 1
0
 /// <summary>
 /// Called when a listener is removed from the service bus
 /// </summary>
 /// <param name="endpoint"></param>
 protected internal virtual void OnListenerRemoved(ListenerEndpoint endpoint)
 {
 }
 /// <summary>
 /// Called when a listener is removed from the service bus
 /// </summary>
 /// <param name="endpoint"></param>
 protected virtual internal void OnListenerRemoved(ListenerEndpoint endpoint)
 {
 }
        public void AddListener(ListenerEndpoint endpoint)
        {
            if (_disposed) throw new ObjectDisposedException("ServiceBusRuntime");

            if (endpoint == null) throw new ArgumentNullException("endpoint");

            if (endpoint.Listener.Runtime != null)
            {
                throw new InvalidOperationException("Listener is attached to a bus already");
            }

            bool added = false;
            try
            {
                using (TransactionScope ts = new TransactionScope())
                {
                    lock (_listenerEndpointsLock)
                    {
                        endpoint.Listener.Runtime = this;
                        _listenerEndpoints.Add(endpoint);
                        added = true;
                    }

                    foreach (RuntimeService service in ServiceLocator.GetAllInstances<RuntimeService>())
                    {
                        service.OnListenerAdded(endpoint);
                    }

                    EventHandler<EndpointEventArgs> listenEvent = ListenerAdded;
                    if (listenEvent != null) listenEvent(this, new EndpointEventArgs(endpoint));

                    if (_started)
                    {
                        endpoint.Listener.StartInternal();
                    }

                    ts.Complete();
                }
            }
            catch
            {
                if (added)
                {
                    // remove on failure
                    lock (_listenerEndpointsLock)
                    {
                        _listenerEndpoints.Remove(endpoint);
                    }
                }
                throw;
            }
        }
        public void RemoveListener(ListenerEndpoint endpoint)
        {
            if (_disposed) throw new ObjectDisposedException("ServiceBusRuntime");

            if (endpoint == null) throw new ArgumentNullException("endpoint");

            bool removed = false;
            try
            {
                using(TransactionScope ts = new TransactionScope())
                {
                    lock(_listenerEndpointsLock)
                    {
                        _listenerEndpoints.Remove(endpoint);
                        endpoint.Listener.Runtime = null;
                    }

                    foreach (RuntimeService service in ServiceLocator.GetAllInstances<RuntimeService>())
                    {
                        service.OnListenerRemoved(endpoint);
                    }

                    EventHandler<EndpointEventArgs> unlistenEvent = ListenerRemoved;
                    if (unlistenEvent != null) unlistenEvent(this, new EndpointEventArgs(endpoint));

                    ts.Complete();
                }
            }
            catch
            {
                if (removed)
                {
                    // re-add on failure
                    lock (_listenerEndpointsLock)
                    {
                        _listenerEndpoints.Add(endpoint);
                    }
                }
                throw;
            }
        }
        public void AddListener(ListenerEndpoint endpoint)
        {
            if (_disposed)
            {
                throw new ObjectDisposedException("ServiceBusRuntime");
            }

            if (endpoint == null)
            {
                throw new ArgumentNullException("endpoint");
            }

            if (endpoint.Listener.Runtime != null)
            {
                throw new InvalidOperationException("Listener is attached to a bus already");
            }

            bool added = false;

            try
            {
                using (TransactionScope ts = new TransactionScope())
                {
                    lock (_listenerEndpointsLock)
                    {
                        endpoint.Listener.Runtime = this;
                        _listenerEndpoints.Add(endpoint);
                        added = true;
                    }

                    foreach (RuntimeService service in ServiceLocator.GetAllInstances <RuntimeService>())
                    {
                        service.OnListenerAdded(endpoint);
                    }

                    EventHandler <EndpointEventArgs> listenEvent = ListenerAdded;
                    if (listenEvent != null)
                    {
                        listenEvent(this, new EndpointEventArgs(endpoint));
                    }

                    if (_started)
                    {
                        endpoint.Listener.StartInternal();
                    }

                    ts.Complete();
                }
            }
            catch
            {
                if (added)
                {
                    // remove on failure
                    lock (_listenerEndpointsLock)
                    {
                        _listenerEndpoints.Remove(endpoint);
                    }
                }
                throw;
            }
        }