对WSE使用总结的补充

来源:互联网 发布:js中window的方法 编辑:程序博客网 时间:2024/04/30 22:44

WSE使用总结的补充

 

   随着项目的进行,对WSE的了解也多了一点,前几天突然想到了一个在Server端动态加入Filter的办法。个人认为这个办法是可行的,赶紧拿出来和大家分享。

    WSE总结一文中我所说的无对整个站点的服务请求进行动态的加载自定义Filter其实是不正确的。例如,我们可以继承HttpMoudle来对所有进入的http 请求进行处理,微软网站有相关的例子。只是就实际情况出发,在我所说的案例当中,我们的自定义Filter其实是与Soap中的一个ServiceID相关联的。也就是说,不同的serviceID对应不同的Filter。如果我们自己编写相应的Http处理程序而不使用WSE提供的功能,无疑是一种费时费力的事情。但是,这些Filter却只能在global.asax中添加,那么我们应该如何处理?

    有一种变通的方法可以很好的解决上面的问题。那就是将自定义的Filterglobal.asax结合起来使用。为了方便说明我假设一共有4个自定义的Filter,它们是,

,

名字

用途

InputHeaderFilter

验证Soap Header是否符合Schema

InputAAFilter

加密

OutputHeaderFilter

加入oap Header信息

OutputAAFilter

解密

OutputAllFilter

处理输出的Soap信息(转换用途)

     假设ServiceID=”001”的用户请求只需要经过InputAAFilter,而服务器再返回时只需要经过OutputAAFilter。但是,ServiceID = “002”的用户请求可能需要经过上面的4Filter。那么我们就无法再global.asax 或者web.config文件中静态的加入Filter。不同的ServiceID对应的Filter以及Filter顺序可能是不同的,因此我们需要动态的加入这些Filter并执行。

 

InputFilter的处理   

首先,我们可以如此假定,我们可以在Web Method中得到ServiceID并且创建一个新的pipeline对象,并把所有的Filter按一定顺序加入(数据库有相应字段表示顺序),然后利用pipelineIprocessInputMessage()方法完成设计中Input Filter应执行的操作,代码如下:

//Input Filter

Pipeline replyPip = new Pipeline();

//加入一个filter

//为了便于说明,没有使用从数据库读取的方法添加,其原理同样

//需要注意添加Filter的顺序不能颠倒!

replyPip.InputFilters.Add (new AAInboundFilter());   

//执行加入的所有InputFilter

replyPip.ProcessInputMessage(requestContext.Envelope);

     这样,我们便完成了服务器端的InputFilter的功能,上述代码将在WebMethod中被调用。

 

OutputFilter 的处理

     对于OutPutFilter情况要复杂一些,因为如果我们使用类似上面的技术处理OutputFilterresponseContext.Envelope)由于responseContext.Envelopereadonly,我们无法在程序中人为的创建并修改这个属性,因此我将使用一个替身转换的方法来实现。首先,创建一个新的SoapEnvelope对象,然后利用和InputFilter类似的技术加入OutFilter并处理,得到一个和真实的需要的Response Sope信息一致的SoapEnvelope。然后将这个SoapEnvelope保存到ResponseContext的中,然后在OutputAllFIlter中取出,赋给服务器返回的Soap信息即可。整个代码如下:

WebMethod中:

//Add Output Filter

replyPip.OutputFilters.Add (new AAOutboundFilter());

SoapEnvelope env = new SoapEnvelope();

//处理Output SoapEnvelope

//此处的envelope并不是真正的返回信息,只是替身

//用户可以控制哪些Output filter可以使用

replyPip.ProcessOutputMessage(env);

//将修改后的模拟的envelope信息保存在ResponseContext对象中

HttpSoapContext.ResponseContext.Add("env",env);   

 

global.asaxapplication_start()加入下列代码:

WebServicesConfiguration.FilterConfiguration.OutputFilters.Add

(new OutputAllFIlter());

这样这个站点的所有响应的Soap信息都会经过这个OutFilter   

 

最后,OutputAllFIlter.cs中代码如下:

using System;

using System.Xml;

using Microsoft.Web.Services;

using System.Xml.Schema;

using System.Collections;

namespace rottenapple

{

        public class OutputAllFIlter: SoapOutputFilter

        {

                public OutputAllFIlter ()

                {

                        //

                        // TODO: Add constructor logic here

                        //

                }

 

                public override void ProcessMessage(SoapEnvelope envelope)

                {

                        //如果env这个object则执行,否则不执行转换

                     //在调用pipeline/ProcessOutFilter的时候也会执行这个Filter,但那时

//候不做任何处理

                        if (envelope.Context.Contains("env") )

                        {

                                SoapEnvelope env = (SoapEnvelope)envelope.Context["env"];

                               

                                XmlNode header = envelope.Header;

                                if (header == null)

                                        header = envelope.CreateHeader();

                                header.InnerXml = env.Header.InnerXml ;

                        }

 

                }

        }

}

 

以上代码在VS2003,WSE1.0sp1下测试通过。

 

原创粉丝点击