public void ListenForEvents(ServiceEnvironmentEventType eventType) { if (_eventListener == null) { _eventListener = new WcfHost(this, this); ServiceEndpoint[] rawEndpoints = _eventListener.Description.Endpoints.ToArray(); _eventListener.Description.Endpoints.Clear(); foreach (ServiceEndpoint endpoint in rawEndpoints) { endpoint.Address = new EndpointAddress(new Uri(endpoint.Address.Uri.ToString().Replace("{guid}", Guid.NewGuid().ToString("N")))); _eventListener.AddServiceEndpoint(endpoint); } _eventListener.Open(); } var env = this.EnvironmentConfiguration; using (var connection = new SqlConnection(env.ConnectionString)) { // FUTURE: in the future save all endpoints to DB var command = new SqlCommand(env.SP_EnvironmentEventRegister, connection); command.CommandType = CommandType.StoredProcedure; command.Parameters.AddWithValue("@eventType", eventType.ToString()); command.Parameters.AddWithValue("@endpointName", _eventListener.Description.Endpoints.First(endpoint => endpoint.Name == typeof(ServiceEnvironment).FullName).Name); command.Parameters.AddWithValue("@endpointAddress", _eventListener.Description.Endpoints.First(endpoint => endpoint.Name == typeof(ServiceEnvironment).FullName).Address.ToString()); connection.Open(); command.ExecuteNonQuery(); } }
// ------------------------------ #endregion #region Event wiring // ------------------------------ void Ensure(ServiceEnvironmentEventType eventType) { if (!EventTypes.Contains(eventType)) { throw new InvalidOperationException(String.Format("Cannot connect listener to event {0} because it is not registered to receive this event.", eventType)); } }
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); } } } }
void IServiceEnvironmentEventSender.SendEnvironmentEvent(ServiceEnvironmentEventType eventType, Action <IServiceEnvironmentEventListener> listenerAction, bool throwExIfNobodyListen) { SendEnvironmentEvent(eventType, listenerAction, throwExIfNobodyListen); }