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;
        }
    }
View Code
 [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");
        }
    }
View Code

 

posted @ 2020-02-07 23:52  vvf  阅读(175)  评论(0编辑  收藏  举报