示例#1
0
        /// <summary>
        /// Called before client calls are sent and after service responses are returned.
        /// </summary>
        /// <param name="operationName">The name of the operation.</param>
        /// <param name="inputs">The objects passed to the method by the client.</param>
        /// <returns>The correlation state that is returned as the <c>correlationState</c> parameter in
        /// <see cref="M:System.ServiceModel.Dispatcher.IParameterInspector.AfterCall(System.String,System.Object[],System.Object,System.Object)" />.
        /// Return null if you do not intend to use correlation state.</returns>
        /// <exception cref="System.ArgumentNullException">inputs</exception>
        /// <exception cref="FaultException{ValidationFault}"></exception>
        /// <exception cref="InvalidObjectFault"></exception>
        public object BeforeCall(
            string operationName,
            object[] inputs)
        {
            if (inputs == null)
            {
                throw new ArgumentNullException(nameof(inputs));
            }

            var results = new ValidationResults();

            for (var i = 0; i < InputValidators.Count(); i++)
            {
                InputValidators[i].DoValidate(inputs[i], inputs[i], InputValidatorParameterNames[i], results);
            }

            if (results.IsValid)
            {
                return(null);
            }

            var validationFault = new InvalidObjectFault();

            if (WebOperationContext.Current != null)
            {
                throw new WebFaultException <InvalidObjectFault>(
                          AddFaultDetails(validationFault, results), validationFault.HttpStatusCode);
            }
            else
            {
                throw new FaultException <InvalidObjectFault>(
                          AddFaultDetails(validationFault, results));
            }
        }
示例#2
0
        /// <summary>
        /// Adds the fault details.
        /// </summary>
        /// <param name="fault">The fault.</param>
        /// <param name="results">The results.</param>
        /// <returns>ValidationFault.</returns>
        static InvalidObjectFault AddFaultDetails(
            InvalidObjectFault fault,
            ValidationResults results)
        {
            if (results == null)
            {
                throw new ArgumentNullException(nameof(results));
            }
            if (fault == null)
            {
                throw new ArgumentNullException(nameof(fault));
            }

            if (!results.IsValid)
            {
                foreach (ValidationResult result in results)
                {
                    fault.Add(CreateValidationDetail(result));
                }
            }

            return(fault);
        }