1 public class MyOperationBehavior:Attribute, IOperationBehavior 2 { 3 public void AddBindingParameters(OperationDescription operationDescription, 4 System.ServiceModel.Channels.BindingParameterCollection bindingParameters) 5 { 6 Logger.Log("MyOperationBehavior", 7 "AddBindingParameters", operationDescription.Name); 8 } 9 10 public void ApplyClientBehavior(OperationDescription operationDescription, 11 System.ServiceModel.Dispatcher.ClientOperation clientOperation) 12 { 13 clientOperation.ParameterInspectors.Add(new MyParameterInspector()); 14 Logger.Log("MyOperationBehavior", 15 "ApplyClientBehavior", operationDescription.Name); 16 } 17 18 public void ApplyDispatchBehavior(OperationDescription operationDescription, 19 System.ServiceModel.Dispatcher.DispatchOperation dispatchOperation) 20 { 21 dispatchOperation.ParameterInspectors.Add(new MyParameterInspector()); 22 Logger.Log("MyOperationBehavior", 23 "ApplyDispatchBehavior", operationDescription.Name); 24 } 25 26 public void Validate(OperationDescription operationDescription) 27 { 28 Logger.Log("MyOperationBehavior", "Validate", operationDescription.Name); 29 } 30 }
1 public class MyServiceBehaviorAttribute : Attribute, IServiceBehavior 2 { 3 4 public void AddBindingParameters(ServiceDescription serviceDescription, 5 ServiceHostBase serviceHostBase, 6 Collection<ServiceEndpoint> endpoints, 7 BindingParameterCollection bindingParameters) 8 { 9 Logger.Log("MyServiceBehaviorAttribute", 10 "AddBindingParameters", serviceDescription.Name); 11 } 12 public void ApplyDispatchBehavior(ServiceDescription serviceDescription, 13 ServiceHostBase serviceHostBase) 14 { 15 Logger.Log("MyServiceBehaviorAttribute", 16 "ApplyDispatchBehavior", serviceDescription.Name); 17 } 18 public void Validate(ServiceDescription serviceDescription, 19 ServiceHostBase serviceHostBase) 20 { 21 Logger.Log("MyServiceBehaviorAttribute", 22 "Validate", serviceDescription.Name); 23 } 24 }
1 public class MyParameterInspector:IParameterInspector 2 { 3 public void AfterCall(string operationName, object[] outputs, 4 object returnValue, object correlationState) 5 { 6 Logger.Log("MyParameterInspector", "AfterCall", operationName); 7 } 8 9 public object BeforeCall(string operationName, object[] inputs) 10 { 11 Logger.Log("MyParameterInspector", "BeforeCall", operationName); 12 return null; 13 } 14 }
1 public class MyParameterInspector: IOperationBehavior, IParameterInspector 2 { 3 #region IOperationBehavior Members 4 /// <summary> 5 /// 6 /// </summary> 7 /// <param name="operationDescription"></param> 8 /// <param name="bindingParameters"></param> 9 public void AddBindingParameters(OperationDescription operationDescription, System.ServiceModel.Channels.BindingParameterCollection bindingParameters) 10 { 11 12 } 13 /// <summary> 14 /// 15 /// </summary> 16 /// <param name="operationDescription"></param> 17 /// <param name="clientOperation"></param> 18 public void ApplyClientBehavior(OperationDescription operationDescription, ClientOperation clientOperation) 19 { 20 21 } 22 /// <summary> 23 /// 24 /// </summary> 25 /// <param name="operationDescription"></param> 26 /// <param name="dispatchOperation"></param> 27 public void ApplyDispatchBehavior(OperationDescription operationDescription, DispatchOperation dispatchOperation) 28 { 29 dispatchOperation.ParameterInspectors.Add(this); 30 } 31 32 /// <summary> 33 /// 34 /// </summary> 35 /// <param name="operationDescription"></param> 36 public void Validate(OperationDescription operationDescription) 37 { 38 39 } 40 41 #endregion 42 43 44 /// <summary> 45 /// 调用方法后 输出结果值 46 /// </summary> 47 /// <param name="operationName"></param> 48 /// <param name="outputs"></param> 49 /// <param name="returnValue"></param> 50 /// <param name="correlationState"></param> 51 public void AfterCall(string operationName, object[] outputs, object returnValue, object correlationState) 52 { 53 Console.WriteLine("*************返回操作名称:" + operationName+"*************"); 54 Console.WriteLine("*************返回操作编号:" + correlationState.ToString() + "**************"); 55 for (int i = 0; i < outputs.Length; i++) 56 { 57 58 Type T = outputs[i].GetType(); 59 Console.WriteLine("返回操作参数" + i.ToString() + " 类型为:" + T.ToString()); 60 Console.WriteLine("返回操作参数" + i.ToString() + " ToString为:" + outputs[i].ToString()); 61 Console.WriteLine("返回操作参数" + i.ToString() + " 属性:"); 62 PropertyInfo[] PIs = T.GetProperties(); 63 foreach (PropertyInfo PI in PIs) 64 { 65 Console.Write(PI.Name + ":"); 66 Console.WriteLine(PI.GetValue(outputs[i], null)); 67 } 68 69 70 } 71 72 Type Treturn = returnValue.GetType(); 73 Console.WriteLine("操作返回值" + " 类型为:" + Treturn.ToString()); 74 Console.WriteLine("操作返回值" + " ToString为:" + Treturn.ToString()); 75 Console.WriteLine("操作返回值" + " 属性:"); 76 77 if (Treturn.ToString() != "System.String") 78 { 79 PropertyInfo[] PIreturns = Treturn.GetProperties(); 80 foreach (PropertyInfo PI in PIreturns) 81 { 82 Console.Write(PI.Name + ":"); 83 Console.WriteLine(PI.GetValue(returnValue, null)); 84 } 85 } 86 87 88 } 89 /// <summary> 90 /// 调用方法前 输出参数值 91 /// </summary> 92 /// <param name="operationName"></param> 93 /// <param name="inputs"></param> 94 /// <returns></returns> 95 public object BeforeCall(string operationName, object[] inputs) 96 { 97 Guid guid = Guid.NewGuid(); 98 99 Console.WriteLine("*************调用操作名称:" + operationName+"**************"); 100 Console.WriteLine("*************调用操作编号:" + guid.ToString () + "**************"); 101 for (int i = 0; i < inputs.Length ; i++) 102 { 103 104 Type T = inputs[i] .GetType (); 105 Console.WriteLine("操作参数"+i.ToString ()+" 类型为:"+T.ToString ()); 106 Console.WriteLine("操作参数" + i.ToString() + " ToString为:" + inputs[i].ToString()); 107 Console.WriteLine("操作参数" + i.ToString() + " 属性:"); 108 PropertyInfo [] PIs = T.GetProperties(); 109 foreach (PropertyInfo PI in PIs) 110 { 111 Console.Write ( PI.Name +":"); 112 Console.WriteLine (PI.GetValue(inputs[i], null)); 113 } 114 115 } 116 return guid; 117 } 118 119 120 }