Пример #1
0
        void SendEnvironmentEvent(ServiceEnvironmentEventType eventType, Action <IServiceEnvironmentEventListener> listenerAction, bool throwExIfNobodyListen = true)
        {
            // TODO: check process/appdomain permissions

            RefreshEventListenersList();
            List <ServiceEnvironmentEventListenerInfo> listeners;

            if (!_environmentListeners.TryGetValue(eventType, out listeners) || listeners.Count < 1)
            {
                if (throwExIfNobodyListen)
                {
                    throw new ServiceEnvironmentException(String.Format("Could not find any registered listeners for {0} event.", eventType));
                }
                Log.Write(this.ToString(), String.Format("There are no listeners to event {0}", eventType.ToString()), LogMessageType.Warning);
                return;
            }

            foreach (var listenerInfo in listeners)
            {
                try
                {
                    using (var client = new WcfClient <IServiceEnvironmentEventListener>(this, listenerInfo.EndpointName, listenerInfo.EndpointAddress))
                    {
                        listenerAction(client.Channel);
                    }
                }
                catch (Exception ex)
                {
                    string message = String.Format("Failed to send environment event {0} to listener at {1}, it might be dead.", eventType, listenerInfo.EndpointAddress);
                    if (Service.Current != null)
                    {
                        Service.Current.Log(message, ex, LogMessageType.Information);
                    }
                    else
                    {
                        throw new ServiceEnvironmentException(message, ex);
                    }
                }
            }
        }
        private void NotifyScheduleServiceRequest(ServiceScheduleRequestedEventArgs args)
        {
            RefreshEventListenersList();
            ServiceEnvironmentEventListenerInfo listenerInfo;

            if (!_environmentListeners.TryGetValue(ServiceEnvironmentEventType.ServiceScheduleRequested, out listenerInfo))
            {
                throw new ServiceEnvironmentException("Could not find a registered listener for ServiceScheduleRequested events in the database.");
            }

            try
            {
                using (var listener = new WcfClient <IServiceEnvironmentEventListener>(this, listenerInfo.EndpointName, listenerInfo.EndpointAddress))
                {
                    listener.Channel.ServiceScheduleRequestedEvent(args);
                }
            }
            catch (Exception ex)
            {
                throw new ServiceEnvironmentException(String.Format("Failed WCF call to the registered listener for ServiceScheduleRequested events ({0}).", listenerInfo.EndpointAddress), ex);
            }
        }