OperationBehavior
1.参数检查器
public class MyOperationBehavior : Attribute, IOperationBehavior { public void AddBindingParameters( OperationDescription operationDescription , BindingParameterCollection bindingParameters) { } /// <summary> /// 客户端Behavior /// </summary> /// <param name="operationDescription"></param> /// <param name="clientOperation"></param> public void ApplyClientBehavior( OperationDescription operationDescription , ClientOperation clientOperation) { } /// <summary> /// 服务端Behavior /// </summary> /// <param name="operationDescription"></param> /// <param name="dispatchOperation"></param> public void ApplyDispatchBehavior( OperationDescription operationDescription , DispatchOperation dispatchOperation) { dispatchOperation.ParameterInspectors.Add(new MyParameterInspector()); } public void Validate(OperationDescription operationDescription) { } } public class MyParameterInspector : IParameterInspector { public void AfterCall(string operationName, object[] outputs, object returnValue, object correlationState) { } /// <summary> /// /// </summary> /// <param name="operationName"></param> /// <param name="inputs">参数</param> /// <returns></returns> public object BeforeCall(string operationName, object[] inputs) { if (operationName == "") { var p1 = inputs[0]; if (p1 == null || string.IsNullOrEmpty(p1.ToString())) throw new ArgumentNullException("null parameter"); } return null; } }
[ServiceContract] public interface IHomeService { [OperationContract] void Do(string s); } public class HomeService : IHomeService { [Lib.MyOperationBehavior] public void Do(string s) { Console.WriteLine("HomeService is working"); } }