Tag Archives: Ignore Validation

Suppression mechanism for DevTrends’ WCFDataAnnotations

If you’ve ever developed a Windows Communication Service (WCF) project, you’ll know that there comes a time you’re gonna need to validate objects passing through your service’s pipeline and luckily enough there’s already a very well-designed, easy-to-use Nuget package available for you; it’s called WCFDataAnnotations.

There is only simething left out on this great plugin. An escape door! When you decorate your service implementation with [ValidateDataAnnotationsBehavior], all of the service operations will get validated and there’s no way to tell the plugin to ignore validation on some of your operations.

Fortunately, the solution was really easy. Add the following class to its source code and decorate the service operations with [SuppressValidation] to disable validation on those operations.

SuppressValidationAttribute.cs

/*
 * Suppression Mechanism for DevTrends' WCF validation plugin.
 * Created by Mohammad M. Saffari http://blog.saffari.space
 * 2015-05-13
*/
using System;
using System.Linq;
using System.ServiceModel.Description;
 
namespace DevTrends.WCFDataAnnotations {
	/// <summary>
	/// Disables all validation when applied to a WCF service operation
	/// </summary>
	[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
	public class SuppressValidationAttribute : Attribute, IOperationBehavior {
 
		private bool _isActive = true;
 
		/// <summary>
		/// Enables or Disables the <see cref="SuppressValidationAttribute"/><para/>
		/// Default is <value>True</value> which means the <see cref="SuppressValidationAttribute"/> is Active
		/// </summary>
		public bool IsActive {
			get { return _isActive; }
			set { _isActive = value; }
		}
 
		/// <summary>
		/// Disables all validation when applied to a WCF service operation
		/// </summary>
		/// <param name="isActive">Default is <value>True</value> which means the <see cref="SuppressValidationAttribute"/> is Active</param>
		public SuppressValidationAttribute(bool isActive = true) {
			_isActive = isActive;
		}
 
 
		#region IOperationBehavior Members
 
		/// <summary>
		/// 
		/// </summary>
		/// <param name="operationDescription"></param>
		/// <param name="bindingParameters"></param>
		public void AddBindingParameters(OperationDescription operationDescription, System.ServiceModel.Channels.BindingParameterCollection bindingParameters) { }
 
		/// <summary>
		/// 
		/// </summary>
		/// <param name="operationDescription"></param>
		/// <param name="clientOperation"></param>
		public void ApplyClientBehavior(OperationDescription operationDescription, System.ServiceModel.Dispatcher.ClientOperation clientOperation) { }
 
		/// <summary>
		/// Removes the <see cref="ValidatingParameterInspector"/> from inspectors.
		/// </summary>
		/// <param name="operationDescription"></param>
		/// <param name="dispatchOperation"></param>
		public void ApplyDispatchBehavior(OperationDescription operationDescription, System.ServiceModel.Dispatcher.DispatchOperation dispatchOperation) {
			//dispatchOperation.ParameterInspectors.Add(new SuppressValidationParameterInspector());
			if (_isActive) {
				ValidatingParameterInspector[] pia = dispatchOperation.ParameterInspectors
						.Where(pi => pi is ValidatingParameterInspector)
						.Cast<ValidatingParameterInspector>()
						.ToArray();
				foreach (var vpi in pia) {
					dispatchOperation.ParameterInspectors.Remove(vpi);
				}
			}
		}
 
		/// <summary>
		/// 
		/// </summary>
		/// <param name="operationDescription"></param>
		public void Validate(OperationDescription operationDescription) { }
 
		#endregion
	}
}