享受代码,享受人生

SOA is an integration solution. SOA is message oriented first.
The Key character of SOA is loosely coupled. SOA is enriched
by creating composite apps.
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Applied WSE 3.0 to Web Service Project

Posted on 2006-06-27 12:51  idior  阅读(4668)  评论(20编辑  收藏  举报

源代码下载 (Web Service 项目需要手动添加)

在介绍过有关WS-Addressing的理论知识后,可能很多人觉得离自己很远,而且没有实际的例子说明它们的作用,觉得实际作用不大。因此在后继的文章中将结合WS-Addressing规范和WSE3.0从实践中说明它们的作用。

如果你曾经Trace过调用Web Service的SOAP消息,你会发现其中根本没有诸如<wsa:To>,<wsa:Action>之类的元素,因此实践中的第一步就是让Message Addressing Properties出现在SOAP消息中。

首先建立一个最简单的Web Service,如下所示:
    [WebServiceBinding(
      Name = "MathService",
      Namespace = "http://idior.cnblogs.com/mathservice/",
      ConformsTo = WsiProfiles.BasicProfile1_1,
      EmitConformanceClaims = true)]
    public interface IMathService
    {
        [WebMethod()]
        double Add(double a, double b);

        [WebMethod()]
        double Sub(double a, double b);
    }


    [WebService(Namespace = "http://idior.cnblogs.com/mathservice/")]
    public class MathService : IMathService
    {
        public double Add(double a, double b)
        {
            return a + b;
        }

        public double Sub(double a, double b)
        {
            return a - b;
        }
     }

然后在客户端添加Web引用,并使用该Web Service:

    class Program
    {
        static void Main()
        {
            MathService mathService=new MathService();
            double result=mathService.Sub(12, 10);
            Console.Out.WriteLine(result);
        }
    }

项目的结构如下所示:


以上过程非常简单,如果正常的话,你将在客户端的Console中看到输出结果。
使用Soap Toolkit 3.0中的Trace Utility来Trace一下调用Web Service过程的SOAP消息:

Request Message:
<?xml version="1.0" encoding="utf-8" ?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Body>
    <Sub xmlns="http://idior.cnblogs.com/mathservice/">
      <a>12</a>
      <b>10</b>
    </Sub>
  </soap:Body>
</soap:Envelope>

Response Message:
<?xml version="1.0" encoding="utf-8" ?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Body>
    <SubResponse xmlns="http://idior.cnblogs.com/mathservice/">
      <SubResult>2</SubResult>
    </SubResponse>
  </soap:Body>
</soap:Envelope>

其中确实没有出现任何的Message Addressing Properties。OK, 让我们使用WSE来开启这项功能。

      

如上图所示,在Project Client上点击右键,出现WSE Setting 3.0的选项,点击该选项。

 
并勾选Enable this project for Web Service Enhancements,然后确认。此时,查看添加Web 引用时生成的Proxy文件---Reference.cs,在其中我们找到MathService这个代理类。

public
partial class MathService : System.Web.Services.Protocols.SoapHttpClientProtocol {//...
        
MathService继承于SoapHttpClientProtocol,为了让Client使用到WSE的功能,需要让MathService继承于另一个基类 --- WebServicesClientProtocol。

public partial class MathService : Microsoft.Web.Services3.WebServicesClientProtocol { //...

如此Client便具有了WSE的增强功能。此时再次Trace一下调用Web Service的SOAP 消息,得到结果如下:

Request Message:
<?xml version="1.0" encoding="utf-8" ?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xmlns:xsd="http://www.w3.org/2001/XMLSchema"
              xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing"
              xmlns:wsse="http://.../oasis-200401-wss-wssecurity-secext-1.0.xsd"
              xmlns:wsu="http://.../oasis-200401-wss-wssecurity-utility-1.0.xsd">
  <soap:Header>
    <wsa:Action>http://idior.cnblogs.com/mathservice/Sub</wsa:Action>
    <wsa:MessageID>urn:uuid:201c61e1-d816-4855-9436-e66c53e7ab73</wsa:MessageID>
    <wsa:ReplyTo>
       <wsa:Address>http://.../addressing/role/anonymous</wsa:Address>
    </wsa:ReplyTo>
    <wsa:To>http://localhost:1274/MathService/MathService.asmx</wsa:To>
    <wsse:Security>
       <wsu:Timestamp wsu:Id="Timestamp-e4092262-c294-476d-bf80-a6bb61161356">
         <wsu:Created>2006-06-27T03:42:52Z</wsu:Created>
         <wsu:Expires>2006-06-27T03:47:52Z</wsu:Expires>
      </wsu:Timestamp>
    </wsse:Security>
  </soap:Header>
  <soap:Body>
    <Sub xmlns="http://idior.cnblogs.com/mathservice/">
      <a>12</a>
      <b>10</b>
    </Sub>
  </soap:Body>
</soap:Envelope>

Response Message:
<?xml version="1.0" encoding="utf-8" ?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Body>
    <SubResponse xmlns="http://idior.cnblogs.com/mathservice/">
      <SubResult>2</SubResult>
    </SubResponse>
  </soap:Body>
</soap:Envelope>

不出所料,在Client发出的Request Message中我们在SOAP Head中找到了与WS-Adressing相关的Message Addressing Properties,阅读WS-Addressing Message Addressing Properties一文,你会发现这里的内容是与WS-Addressing规范一致的。(其中还包含了一块与ws-Security相关的内容,读者可以暂时不用考虑。)然而Response Message依旧和以前一样,没有与WS-Adressing相关的属性出现,原因很简单,我们只是在Client端开启了WSE,在Service端并没有。
为了让Response Message也使用WS-Adressing,我们需要为MathService项目也开启WSE的功能。与Client项目不同,此时需要将两个选项同时选上。


这样就完成了Service端的配置,再次Trace SOAP消息你会发现Request Message,Response Message中都出现了Message Addressing Properties,并且它们的结果与WS-Addresing规范是一致的:

Request Message:
<?xml version="1.0" encoding="utf-8" ?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xmlns:xsd="http://www.w3.org/2001/XMLSchema"
              xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing"
              xmlns:wsse="http://.../oasis-200401-wss-wssecurity-secext-1.0.xsd"
              xmlns:wsu="http://.../oasis-200401-wss-wssecurity-utility-1.0.xsd">
  <soap:Header>
    <wsa:Action>http://idior.cnblogs.com/mathservice/Sub</wsa:Action>
    <wsa:MessageID>urn:uuid:5b8d0882-d2ad-40f1-ad7c-8ecf614b8afc</wsa:MessageID>
    <wsa:ReplyTo>
       <wsa:Address>http://.../addressing/role/anonymous</wsa:Address>
    </wsa:ReplyTo>
    <wsa:To>http://localhost:1274/MathService/MathService.asmx</wsa:To>
    <wsse:Security>
       <wsu:Timestamp wsu:Id="Timestamp-ba231e59-2ac9-44f0-ba86-441dea7aebb6">
         <wsu:Created>2006-06-27T04:06:44Z</wsu:Created>
         <wsu:Expires>2006-06-27T04:11:44Z</wsu:Expires>
      </wsu:Timestamp>
    </wsse:Security>
  </soap:Header>
  <soap:Body>
    <Sub xmlns="http://idior.cnblogs.com/mathservice/">
      <a>12</a>
      <b>10</b>
    </Sub>
  </soap:Body>
</soap:Envelope>

Response Message:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns:xsd="http://www.w3.org/2001/XMLSchema"
             xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing"
             xmlns:wsse="http://.../oasis-200401-wss-wssecurity-secext-1.0.xsd"
             xmlns:wsu="http://.../oasis-200401-wss-wssecurity-utility-1.0.xsd">
  <soap:Header>
     <wsa:Action>http://idior.cnblogs.com/mathservice/SubResponse</wsa:Action>
     <wsa:MessageID>urn:uuid:a4a115f6-bcaa-4245-bb65-27131a0ebadc</wsa:MessageID>
     <wsa:RelatesTo>urn:uuid:5b8d0882-d2ad-40f1-ad7c-8ecf614b8afc</wsa:RelatesTo>
     <wsa:To>http://.../addressing/role/anonymous</wsa:To>
     <wsse:Security>
        <wsu:Timestamp wsu:Id="Timestamp-a95201b2-d3d1-40a9-9cc4-8632b10b438e">
           <wsu:Created>2006-06-27T04:06:47Z</wsu:Created>
           <wsu:Expires>2006-06-27T04:11:47Z</wsu:Expires>
        </wsu:Timestamp>
     </wsse:Security>
  </soap:Header>
  <soap:Body>
     <SubResponse xmlns="http://idior.cnblogs.com/mathservice/">
        <SubResult>2</SubResult>
     </SubResponse>
  </soap:Body>
</soap:Envelope>


参考资料:
WS-Addressing Message Addressing Properties