public static byte[] SerializeRuleToBuffer(FaultRule rule)
        {
            BinaryFormatter formatter = new BinaryFormatter();

            using (MemoryStream stream = new MemoryStream())
            {
                formatter.Serialize(stream, rule);
                return(stream.GetBuffer());
            }
        }
Example #2
0
        public static FaultRule[] Load()
        {
            // Using FaultScope
            FaultScope currentFaultScope = FaultScope.Current; // save in case it changes on another thread

            if (currentFaultScope != null)
            {
                return(currentFaultScope.FaultRules);
            }

            // Using FaultSession / serialization file
            if (string.IsNullOrEmpty(serializationFileName))
            {
                return(null);
            }

            lock (initializeLock)
            {
                if (fileReadWriteMutex == null)
                {
                    fileReadWriteMutex = new Mutex(false, Path.GetFileName(serializationFileName));
                    currentRules       = Serializer.DeserializeRules(serializationFileName, fileReadWriteMutex);

                    return(currentRules);
                }
            }

            FaultRule[] swapBuffer;
            lock (accessCurrentRuleLock)
            {
                swapBuffer = new FaultRule[currentRules.Length];
                currentRules.CopyTo(swapBuffer, 0);
            }

            FaultRule[] loadedRules = Serializer.DeserializeRules(serializationFileName, fileReadWriteMutex);
            MergeRuleArray(swapBuffer, loadedRules);
            lock (accessCurrentRuleLock)
            {
                currentRules = swapBuffer;
            }

            return(currentRules);
        }
Example #3
0
        public static FaultRule[] Load()
        {
            string serializationFileName = Environment.GetEnvironmentVariable(EnvironmentVariable.RuleRepository);

            if (serializationFileName == null)
            {
                throw new FaultInjectionException(string.Format(CultureInfo.CurrentCulture, ApiErrorMessages.UnableToFindEnvironmentVariable, EnvironmentVariable.RuleRepository));
            }

            lock (initializeLock)
            {
                if (fileReadWriteMutex == null)
                {
                    fileReadWriteMutex = new Mutex(false, Path.GetFileName(serializationFileName));
                    currentRules       = Serializer.DeserializeRules(serializationFileName, fileReadWriteMutex);

                    return(currentRules);
                }
            }

            FaultRule[] swapBuffer;
            lock (accessCurrentRuleLock)
            {
                swapBuffer = new FaultRule[currentRules.Length];
                currentRules.CopyTo(swapBuffer, 0);
            }

            FaultRule[] loadedRules = Serializer.DeserializeRules(serializationFileName, fileReadWriteMutex);
            MergeRuleArray(swapBuffer, loadedRules);
            lock (accessCurrentRuleLock)
            {
                currentRules = swapBuffer;
            }

            return(currentRules);
        }
Example #4
0
 /// <summary>
 /// Copies the number of times the FaultRule has been called from another fault rule.
 /// This is used when this is a new fault rule that has just been deserialized and it
 /// is replacing an old fault rule.
 /// </summary>
 /// <param name="f">the old FaultRule that internal data should be copied from</param>
 internal void CopyNumTimesCalled(FaultRule f)
 {
     numTimesCalled = f.numTimesCalled;
 }
Example #5
0
        /// <summary>
        /// Injected into the prologue of the target method.
        /// </summary>
        /// <param name="exceptionValue">Exception thrown by fault</param>
        /// <param name="returnValue">Value to return from fault</param>
        /// <returns></returns>
        /// <remarks>
        /// Trap creates a RuntimeContext for the current call and evaluates
        /// the fault condition's Trigger method.  If it evaluates to true
        /// the fault's Retrieve method is called.
        /// </remarks>
        public static bool Trap(out Exception exceptionValue, out Object returnValue)
        {
            StackTrace stackTrace = new StackTrace(1);
            CallStack  callStack  = new CallStack(stackTrace);
            //stackFrame does not include Trap()
            StackFrame stackFrame      = stackTrace.GetFrame(0);
            String     currentFunction = callStack[0];

            exceptionValue = null;
            returnValue    = null;
            FaultRule[] newRules;


            if (GetNewRulesAndPrepareContext(out newRules, currentFunction) == false)
            {
                return(false);
            }

            RuntimeContext currentContext = null;
            FaultRule      rule           = null;


            try
            {
                //Get Fault Rule and Update current RuntimeContext
                int len = contexts.Length;
                for (int i = 0; i < len; ++i)
                {
                    if (newRules[i].FormalSignature != null &&
                        currentFunction.Equals(newRules[i].FormalSignature) == true)
                    {
                        rule           = newRules[i];
                        currentContext = new RuntimeContext();
                        lock (contexts)
                        {
                            contexts[i].CalledTimes++;
                            currentContext.CalledTimes = contexts[i].CalledTimes;
                        }
                        currentContext.CallStack      = callStack;
                        currentContext.CallStackTrace = stackTrace;

                        break;
                    }
                }

                //Using ICondition and IFault
                if (rule == null)
                {
                    return(false);
                }
                bool triggered = false;
                try
                {
                    lock (contexts)
                    {
                        triggered = rule.Condition.Trigger(currentContext);
                        if (triggered == true)
                        {
                            rule.Fault.Retrieve(currentContext, out (exceptionValue), out returnValue);
                        }
                    }
                }
                catch (System.Exception e)
                {
                    throw new FaultInjectionException(FaultDispatcherMessages.NoExceptionAllowedInTriggerAndRetrieve, e);
                }
                if (!triggered)
                {
                    return(false);
                }
            }
            catch (System.Exception e)
            {
                throw new FaultInjectionException(FaultDispatcherMessages.UnknownExceptionInTrap, e);
            }


            // check return-value's type if not to throw exception
            if (null == exceptionValue)
            {
                if (stackFrame.GetMethod() is ConstructorInfo)
                {
                    return(true);
                }
                Type returnTypeOfTrappedMethod = ((MethodInfo)stackFrame.GetMethod()).ReturnType;
                return(CheckReturnType(returnTypeOfTrappedMethod, returnValue, currentFunction));
            }
            return(true);
        }