随笔 - 290  文章 - 10  评论 - 85  2

使用WSE,可以自定义SoapHeader信息.

 

1. 下载WSE3.0,它兼容WCF,方便以后的升级

http://download.microsoft.com/download/5/5/1/5511bfc6-e52f-4db0-bafb-fd5dcb91eff0/Microsoft%20WSE%203.0.msi

2。安装时选择VS.net Developer安装模式,这样它会在VS.net2005安装插件,方便使用

3。安装MS SoapToolkit 3.0,这样可以方便的Trace到Soap信息(其机理是Client --> Trace Utility--> Server)
http://download.microsoft.com/download/2/e/0/2e068a11-9ef7-45f5-820f-89573d7c4939/soapsdk.exe
安装之后启动Trace Utility, 选择new formatted trace. listen local port#:8080 (这个是client调用WebService的端口)
Forward to Destination Host & Port 是实际Server的WebService的地址.

4。在客户端项目启用WSE3.0,如图所示,在WSE Setting 3.0 里面勾上Enable this project for Web Services Enhancements. 这样它就会自动引用Microsoft.Web.Services3.dll 到项目. 然后添加Web Reference. 此时生成的代理类和未启用WSE有区别. 例如对于MathService,在未启用WSE之前,定义如下: public partial class MathService : System.Web.Services.Protocols.SoapHttpClientProtocol   而启用WSE之后, Reference.cs文件里面多了一个类
public partial class MathServiceWse : Microsoft.Web.Services3.WebServicesClientProtocol
要自定义SoapHeader信息,下面就要用到MathServiceWse这个类

5。启用Policy文件. 在WSE Setting 3.0 里面的Policy的Tab勾上Enable Policy,默认文件名是wse3policyCache.config. 点击[Add...],弹出[Add or Modify Policy Friendly Name],随便输入Policy的名字,如clientPolicy. 然后会出来详细的Config界面. 根据我们项目的具体情况,选择"Secure a client application", Choose client authentication method "Username", 输入用户名密码, 去掉Enable WS-Security 1.1 extensions. 点Finish.

 

 

 

 

我们在代码里增加SetPolicy的调用,就会在SoapHeader里面增加了<wsse:Security>的信息,包含了用户名密码
            MathServiceWse svr = new MathServiceWse();
            svr.SetPolicy("clientPolicy");
            svr.Add(1,2);

6。但是第5步生成的Soapheader和Server端要求的信息还是有差别,导致调用不成功. 这个时候我们就要通过SoapFilter 自定义Soap 信息. 新建一个ClassLibrary项目.编译成dll.
部分关键代码如下(不完整):

namespace WSE3.CustomAssertion.RemoveAddressingHeaders
{
    public class RemoveAddressingHeadersAssertion : PolicyAssertion
    {
        public override SoapFilter CreateClientOutputFilter(FilterCreationContext context)
        {
            return new ClientOutputFilter();
        }
    }

    public class ClientOutputFilter : SoapFilter
    {

        public override SoapFilterResult ProcessMessage(SoapEnvelope envelope)
        {
            //示例代码,请根据实际情况修改
            XmlNode actionNode = envelope.Header["XXXXXX"];
            envelope.Header.RemoveChild(actionNode);

            XmlNode AppParamNode = envelope.CreateNode(XmlNodeType.Element, "ExtraParameters""namespace");
           envelope.Header.AppendChild(AppParamNode);
           return SoapFilterResult.Continue;
        }
    }
}


7。在client Project引用RemoveAddressingHeadersAssertion.dll, 并修改wse3policyCache.config修改添加内容

 
wse3policy
 <policies xmlns="http://schemas.microsoft.com/wse/2005/06/policy">
  <extensions>
    <extension name="usernameOverTransportSecurity" type="Microsoft.Web.Services3.Design.UsernameOverTransportAssertion, Microsoft.Web.Services3, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
    <extension name="username" type="Microsoft.Web.Services3.Design.UsernameTokenProvider, Microsoft.Web.Services3, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
    <extension name="requireActionHeader" type="Microsoft.Web.Services3.Design.RequireActionHeaderAssertion, Microsoft.Web.Services3, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
    <extension name="RemoveAddressingHeadersAssertion" type="WSE3.CustomAssertion.RemoveAddressingHeaders.RemoveAddressingHeadersAssertion,RemoveAddressingHeadersAssertion"/>
  </extensions>
  <policy name="clientPolicy">
    <usernameOverTransportSecurity>
      <clientToken>
        <username username="MyUserName" password="MyPassword" />
      </clientToken>
    </usernameOverTransportSecurity>      
    <requireActionHeader />
    <RemoveAddressingHeadersAssertion />
  </policy>
</policies>

 

 

posted on 2011-09-26 11:18  Gu  阅读(...)  评论(...编辑  收藏