} // Footnote #1 /// <summary> /// Sends a command through the MicroBus and raises events before and after the <see cref="System.Threading.Tasks.Task"/> is issued. /// </summary> /// <param name="cmd"></param> protected void SendCommand(ICommand cmd) { // Check that the DI has provided the IMicroBus instance. if (MicroBus == null) { throw new MicroBusInstanceNullReferenceException(GetType().Name); } // Issue the command MicroBusCommandIssuedEventArgs evt = new MicroBusCommandIssuedEventArgs(cmd); Task task = null; try { task = MicroBus.SendAsync(cmd); OnMicroBusCommandIssued(evt); task.Wait(); if (task.IsCompleted) { OnMicroBusCommandCompleted(evt); } if (task.IsCanceled) { OnMicroBusCommandCanceled(evt); } } catch (Exception) { // TODO: it should be handled/logged if (task.IsFaulted) { OnMicroBusCommandFaulted(new MicroBusCommandFaultedEventArgs(cmd, task.Exception)); } } }
/// <summary> /// Raised immediately after calling MicroBus.SendAsync(). /// </summary> /// <param name="e"></param> protected virtual void OnMicroBusCommandIssued(MicroBusCommandIssuedEventArgs e) { var handler = MicroBusCommandIssued; if (handler != null) { handler(this, e); } }
/// <summary> /// Raised when the MicroBus.SendAsync() Task is canceled. /// </summary> /// <param name="e"></param> protected virtual void OnMicroBusCommandCanceled(MicroBusCommandIssuedEventArgs e) { MicroBusCommandCanceled?.Invoke(this, e); }
/// <summary> /// Raised when the MicroBus.SendAsync() Task is completed. /// </summary> /// <param name="e"></param> protected virtual void OnMicroBusCommandCompleted(MicroBusCommandIssuedEventArgs e) { // A more terse way of invoking than shown in OnMicroBusCommandIssued MicroBusCommandCompleted?.Invoke(this, e); }