WCF 拦截过滤跟踪Request替代消息Response

参考文章:

http://www.cnblogs.com/mushishi/p/4779826.html (wcf利用IDispatchMessageInspector实现接口监控日志记录和并发限流)

http://www.programgo.com/article/68032711246/ (WCF笔记(6)消息拦截与篡改)

http://www.cnblogs.com/artech/archive/2008/09/01/1280939.html ( 创建一个简单的WCF SOAP Message拦截、转发工具[上篇])

http://www.cnblogs.com/huangxincheng/p/4621971.html (高级玩法之自定义Behavior)

http://www.cnblogs.com/Terrylee/archive/2008/11/17/wcf-messaging-features-core.html (消息处理功能核心)

https://msdn.microsoft.com/en-us/library/system.servicemodel.channels.message.aspx

http://www.wiktorzychla.com/2015/11/string-trimming-wcf-inspector-for-json.html (String trimming WCF inspector for JSON binding)

InterfaceCallHelper
public class InterfaceCallHelper
    {

     /// <summary>
        /// 获取调用接口名称
        /// </summary>
        public void GetInterfaceName()
        {

            //PathInfo : /Get_InfoHotList/MjQ=&MA==
            string pathInfo = HttpContext.Current.Request.PathInfo;
            if (string.IsNullOrEmpty(pathInfo)) return;
            pathInfo = pathInfo.Substring(1); //先截取第一个斜杠
            int indexOf = pathInfo.IndexOf("/", StringComparison.Ordinal);
            InterfaceName = indexOf > 0 ? pathInfo.Substring(0, indexOf) : pathInfo;
            
        }

      /// <summary>
        /// 判断接口是否可用
        /// </summary>
        /// <returns></returns>
        public bool GetInterFaceEnabled()
        {

        }

      /// <summary>
        /// 返回接口不可用
        /// </summary>
        /// <returns></returns>
        public returnresult ReturnInterFaceDisabled()
        {
            var checkDataresult = new returnresult
            {
                msg = string.Format("{0}接口,当前状态为不可用", InterfaceName),
                code = "9999"
            };

            return checkDataresult;
        }
IDispatchMessageInspector

public class InterfaceInspector : IDispatchMessageInspector
    {
        InterfaceCallHelper icHelper = new InterfaceCallHelper();

        /// <summary>
        /// // 此方法的返回值 将作为方法BeforeSendReply的第二个参数 object correlationState传入
        /// </summary>
        /// <param name="request"></param>
        /// <param name="channel"></param>
        /// <param name="instanceContext"></param>
        /// <returns></returns>
        public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
        {
            icHelper.GetInterfaceName();

            icHelper.UpdateInterfaceClickCount();
            bool isInterFaceDisabled = !icHelper.GetInterFaceEnabled();

            if (isInterFaceDisabled)
                instanceContext.Abort();
            return isInterFaceDisabled;
        }
        public void BeforeSendReply(ref Message reply, object correlationState)
        {

            var isInterFaceDisabled = (bool)correlationState;
            if (!isInterFaceDisabled) return;

            dataresult dr = icHelper.ReturnInterFaceDisabled();
            string convertedBody = JsonHelper.Serialize(dr);

            //构造消息
            MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(convertedBody));
            XmlDictionaryReader reader = JsonReaderWriterFactory.CreateJsonReader(ms,
                XmlDictionaryReaderQuotas.Max);
            Message newMessage = Message.CreateMessage(reader, int.MaxValue, MessageVersion.None);

            //构造消息属性
            var hrmProperty = new HttpResponseMessageProperty();
            hrmProperty.Headers.Add(new NameValueCollection
                {
                    {"Content-Type", "application/json; charset=utf-8"}
                });
            var mp = new MessageProperties
                {
                    {"WebBodyFormatMessageProperty", new WebBodyFormatMessageProperty(WebContentFormat.Json)},
                    {"httpResponse", hrmProperty}
                };
            newMessage.Properties.CopyProperties(mp);

            //替换消息
            reply = newMessage;


        }
    }

InterfaceInspectorBehaviorAttribute

  public class InterfaceInspectorBehaviorAttribute : Attribute, IServiceBehavior
    {
        #region implement IServiceBehavior

        public void AddBindingParameters(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase, System.Collections.ObjectModel.Collection<ServiceEndpoint> endpoints, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
        {

        }

        public void ApplyDispatchBehavior(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase)
        {
            foreach (ChannelDispatcher channelDispather in serviceHostBase.ChannelDispatchers)
            {
                foreach (var endpoint in channelDispather.Endpoints)
                {
                    // holyshit DispatchRuntime 
                    endpoint.DispatchRuntime.MessageInspectors.Add(new InterfaceInspector());
                }
            }
        }

        public void Validate(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase)
        {

        }
        #endregion

       
    }
ISERVICE
 [ServiceContract]
public interface ISERVICE
{

}
MySERVICE 
 [InterfaceInspectorBehavior]
    public class MySERVICE : ISERVICE
    {
    }

 

转载于:https://www.cnblogs.com/FH-cnblogs/p/6118896.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值