zoukankan      html  css  js  c++  java
  • webserviceSoapExtensionAttribute和SoapExtension

    SoapExtension和SoapExtensionAttribute两个类用于控制webservice序列化和反序列化的一般过程,可对webservice进行压缩和日志等功能进行控制,关于对整个webservice传输的wsdl进行压缩的功能我没做过,不过下周有时间应该会先试试

    SoapExtensionAttribute类:

    public class ExtensionAttribute:SoapExtensionAttribute
        {
            int _priority = 1;

            public override int Priority
            {
                get
                {
                    return _priority;
                }
                set
                {
                    _priority = value;
                }
            }

            public override Type ExtensionType
            {
                get { return typeof(MyExtension); }//定义扩展soap的类型
            }
        }

    这个SoapExtensionAttribute抽象类定义如下:

    protected SoapExtensionAttribute();
            // 返回结果:
            //     SOAP 扩展的 System.Type。
            public abstract Type ExtensionType { get; }
            // 返回结果:
            //     SOAP 扩展的优先级。

     SOAP 扩展分配优先级,该优先级有助于确定当配置多个 SOAP 扩展以便使用 XML Web 服务方法时执行的相对顺序。SOAP 扩展的优先级越高,它的执行与通过网络发送或接收 SOAP 消息越近。SOAP 扩展属于三个优先级组中的任意一个。在每个组内,priority 属性区别每个成员。priority 属性越低,相对优先级就越高(0 是最高的)。
            public abstract int Priority { get; set; }

    //以下为扩展的soapextension类

        public class MyExtension:SoapExtension
        {
           
            public override object GetInitializer(Type serviceType)
            {
                return GetType();
            }

            public override object GetInitializer(LogicalMethodInfo methodInfo, SoapExtensionAttribute attribute)
            {
                return null;
            }

            public override void Initialize(object initializer)
            {
              
            }

            //这个override的方法会被调用四次
            //分别是SoapMessageStage的BeforeSerialize,AfterSerialize,BeforeDeserialize,AfterDeserialize
            public override void ProcessMessage(SoapMessage message)
            {
                if (message.Stage == SoapMessageStage.AfterDeserialize) //反序列化后处理
                {
                    bool check = false;
                    foreach (SoapHeader header in message.Headers)
                    {
                        if (header is CertficateSoapHeader)
                        {
                            CertficateSoapHeader myHeader = (CertficateSoapHeader)header;

                            if (myHeader.UserName == null || myHeader.PassWord == null)
                            {                          
                                break;
                            }

                            if (myHeader.UserName.Equals("LY") && myHeader.PassWord.Equals("LY"))
                            {
                                check = true;
                                break;
                            }
                        }
                    }

                    if (!check)
                    {
                        throw new SoapHeaderException("认证失败", SoapException.ClientFaultCode);
                    }
                }
            }
        }

    通过SoapMessage中的stream可以获取发送和收到的流数据

    上面的功能实现了soapheader的权限认证

    实现上面的类后,我们就不需要在webmethod中进行判断,只需要加入相关的属性即可

    如:

            public CertficateSoapHeader soapHeader;


            [ExtensionAttribute]
            [SoapHeader("soapHeader",Direction=SoapHeaderDirection.In)]
            [WebMethod]
            public string HelloWorld()
            {
                 return "Hello World";
            }

    客户端的调用和前面一样,可见在webservice中soapextension中的ProcessMessage就相当于一个拦截器,能对进入webmethod的stream进行相关的截获

    测试代码如下

    :下载/Files/fujinliang/WebService.rar

  • 相关阅读:
    第一章 jQuery基础方法回顾
    php无法执行python
    echarts
    logstash配置
    storm结合kafka
    spark中读取elasticsearch数据
    hadoop中读取protobuf数据
    spark1.3.1配置模板
    hadoop2.6.0配置模板
    使用jnetpcap捕获数据包进行流量检测
  • 原文地址:https://www.cnblogs.com/fujinliang/p/2542955.html
Copyright © 2011-2022 走看看