/// <summary> /// Throws a <see cref="TransactionException"/> if the transaction failed. /// If a logging service is supplied in the <paramref name="log"/> parameter, then the failure is logged as an error. /// </summary> /// <param name="transaction">The transaction that may have failed.</param> /// <param name="log">Optional. A logging service that can be used to log the error.</param> public static void ThrowIfFailed(this DeviceTransaction transaction, ILog log = null) { if (transaction.Failed) { transaction.RaiseException(log); } }
public void CommitTransaction(DeviceTransaction transaction) { Contract.Requires(transaction != null); Transaction = transaction; transaction.ObserveResponse(response); transaction.MakeHot(); }
/// <summary> /// Marks a transaction as Failed and provides a <see cref="TimeoutException" /> as the source of failure. /// This also completes the transaction and releases any waiting threads. /// </summary> /// <param name="transaction">The transaction.</param> /// <param name="message">The message.</param> public static void TimedOut(this DeviceTransaction transaction, string message = null) { var exception = new TimeoutException(message ?? "Timeout"); var type = transaction.GetType(); var onErrorMethod = type.GetMethod("OnError", BindingFlags.Instance | BindingFlags.NonPublic); onErrorMethod.Invoke(transaction, BindingFlags.NonPublic | BindingFlags.Instance, Type.DefaultBinder, new object[] { exception }, null); }
/// <summary> /// Sets the protected response property. /// </summary> /// <param name="transaction">The transaction.</param> /// <param name="response">The response string, which can be null or empty.</param> /// <seealso cref="SimulateCompletionWithResponse" /> public static void SetResponse(this DeviceTransaction transaction, [CanBeNull] string response) { var maybeResponse = response == null ? Maybe <string> .Empty : new Maybe <string>(response); var transactionType = typeof(DeviceTransaction); var responseProperty = transactionType.GetProperty("Response"); responseProperty.SetValue(transaction, maybeResponse, BindingFlags.Instance | BindingFlags.NonPublic, null, null, null); }
/// <summary> /// Raises a TransactionException for the given transaction. /// </summary> /// <param name="transaction">The transaction causing the exception.</param> /// <param name="log"> /// An <see cref="ILogger" /> instance to which the exception message will be logged at /// Error severity. /// </param> /// <exception cref="TransactionException">Always thrown.</exception> public static void RaiseException([NotNull] this DeviceTransaction transaction, ILogger log = null) { Contract.Requires(transaction != null); string message = $"Transaction {transaction} failed: {transaction.ErrorMessage}"; log?.Error(message); throw new TransactionException(message) { Transaction = transaction }; }
public void CommitTransaction(DeviceTransaction transaction) { bool moreResponses = responseEnumerator.MoveNext(); if (moreResponses) { transaction.SimulateCompletionWithResponse(responseEnumerator.Current); } else { transaction.TimedOut("Timeout"); } ProcessedTransactions.Add(transaction); }
/// <summary> /// Marks a transaction as completed with the supplied response string, which will release any waiting threads. /// </summary> /// <param name="transaction">The transaction.</param> /// <param name="response">The response.</param> public static void SimulateCompletionWithResponse(this DeviceTransaction transaction, [NotNull] string response) { transaction.SetResponse(response); var transactionType = transaction.GetType(); var makeHotMethod = transactionType.GetMethod("MakeHot", BindingFlags.Instance | BindingFlags.NonPublic); makeHotMethod.Invoke(transaction, BindingFlags.NonPublic | BindingFlags.Instance, Type.DefaultBinder, new object[] { }, null); var onCompletedMethod = transactionType.GetMethod("OnCompleted", BindingFlags.Instance | BindingFlags.NonPublic); onCompletedMethod.Invoke(transaction, BindingFlags.NonPublic | BindingFlags.Instance, Type.DefaultBinder, new object[] { }, null); }
/// <summary>Raises a TransactionException for the given transaction.</summary> /// <param name="transaction">The transaction causing the exception.</param> /// <param name="log"> /// An <see cref="ILogger" /> instance to which the exception message will be logged at Error /// severity. /// </param> /// <exception cref="TransactionException">Always thrown.</exception> public static void RaiseException([NotNull] this DeviceTransaction transaction, ILog log = null) { Contract.Requires(transaction != null); string message = $"Transaction {transaction} failed: {transaction.ErrorMessage}"; var ex = new TransactionException(message) { Transaction = transaction }; log?.Error() .Exception(ex) .Transaction(transaction) .Message(message).Write(); throw ex; }
public static IFluentLogBuilder Transaction(this IFluentLogBuilder builder, DeviceTransaction transaction) { builder.Property("transaction", transaction); builder.Property("transactionId", transaction.TransactionId); builder.Property("transactionCommand", transaction.Command); builder.Property("transactionState", transaction.State); if (transaction.ErrorMessage.Any()) { builder.Property("transactionError", transaction.ErrorMessage.Single()); } if (transaction.Response.Any()) { builder.Property("transactionResponse", transaction.Response.Single()); } return(builder); }
public TestableDeviceTransaction(DeviceTransaction sourceTransaction) : base(sourceTransaction.Command) { this.sourceTransaction = sourceTransaction; }