我的WCF之旅(9):如何在WCF中使用tcpTrace来进行Soap Trace

转帖地址:http://www.cnblogs.com/artech/archive/2007/06/14/782845.html

 

无论对于Web Service还是WCF,Client和Service之间交互的唯一形式是通过发送和接收Soap Message。在我们对Web Service和WCF进行深入学习的时候,借助一些Soap Trace 工具对Soap Message进行深入剖析是非常有必要的。在这些工具之中,我觉得最好用的就是Microsoft Soap Toolkit中的Soap Trace Utility和tcpTrace。我们今天就来讲讲如何在WCF中使用tcpTrace这个工具。

首先我们来讲讲tcpTrace实现的基本原 理。说的简单点TcpTrace就是一个监听/转发器(Listening/Forwarding)。当我们启动这个工具的时候,通过设置它监听的 Port,和它将要转发的Host和Port(Destination Server& Destination Port),随后它就开始在本机的Listening Port开始监听,如果这时候一个针对该Listening Port 的Http Request,它就会把Request的内容取下来展现在我们的面前,随后将该Request转发到我们预先设定的Host和Port。

对于WCF来说,如果Client要访问 Service,一般情况下交互的只有Client和Service,Soap Message直接从Client到Service。但是在某些情况,我们需要在Client和Service之间加入一些额外的节点,我们把这些额外的 节点Intermediary Node。我们可以通过这些Intermediary Node实现一些额外的功能,比如把不同的Request forward到不同的Server从而实现负载平衡(Load Balance)。按照面向服务的原则,服务具有高度的自治性(Automation),Soap Message一旦被Service发送出去,就不能再被该Service所控制,所以Soap来说,它需要具有高度的自描述性(Self- Describing),它自身必须包含所有必须的控制信息来指导任何接收到该Soap的节点如何去处理它。SOAP的无限扩展的Header在实现此功 能上可谓功不可没,原则上任何控制信息都可以放在Soap Header之中,Header的可扩展性也使一系列的WS-* Specification的实现 成为可能。对于每次的Message Exchange来说,寻址(Addressing)是首先需要解决的问题,在Intermediary Node的场景中,实际上涉及到两个Address,其中一个是最终Service Endpoint的Address,另一个则是实际接收该Soap的Intermediary Node的Address。在WCF中通过ClientViaBehavior实现这样的功能,我将在 后面讲到。而我们今天所介绍的通过tcpTrace来获取Soap的情况下,tcpTrace实际是就是充当了Intermediary Node的角色。

我们现在就来介绍如果使用tcpTrace。

假设我们在Local host有一个Calculator Service, Endpoint的Address的Uri为:http://localhost:8888/Calculator (Port为8888)。为了使大家有一个具体的认识,我给出了Host该Service的configuration:

<? xml version="1.0" encoding="utf-8"  ?>
< configuration >
    
< system .serviceModel >
        
< services >
            
< service  name ="Artech.ExceptionHandling.Service.CalculatorService" >
                
< endpoint  binding ="wsHttpBinding"  contract ="Artech.ExceptionHandling.Contract.ICalculator"  address ="http://localhost:8888/Calculator"   />                 
            
</ service >
        
</ services >
    
</ system.serviceModel >
</ configuration >

在一般的情况下,Client具有下面一段对应的Configuration(Port为8888)

<? xml version="1.0" encoding="utf-8"  ?>
< configuration >
    
< system .serviceModel >
        
< client >
            
< endpoint  address ="http://localhost:8888/Calculator"                 binding ="wsHttpBinding"  contract ="Artech.ExceptionHandling.Contract.ICalculator"
                name
="defualtEndpoint"   />
        
</ client >
    
</ system.serviceModel >
</ configuration >

上面实际上是Client直接和Service 进行交互的方式。现在我们需要做的是,先把Soap发送给tcpTrace,tcpTrace进行Soap trace之后再把Soap Message传到真正的Service。就需要一个特殊的Client端的Endpoint Behavior:ClientViaBehavior。假设tcpTrace进行监听的Port为8080,那么Client实现了 ClientViaBehavior的configuration将会是如下的样子:

<? xml version="1.0" encoding="utf-8"  ?>
< configuration >
    
< system .serviceModel >
        
< behaviors >
            
< endpointBehaviors >
                
< behavior  name ="calculatorEndpointBehavior" >
                    
< clientVia  viaUri ="http://localhost:8080/Calculator"   />
                
</ behavior >
            
</ endpointBehaviors >
        
</ behaviors >
        
< client >
            
< endpoint  address ="http://localhost:8888/Calculator"  behaviorConfiguration ="calculatorEndpointBehavior"
                binding
="wsHttpBinding"  contract ="Artech.ExceptionHandling.Contract.ICalculator"
                name
="defualtEndpoint"   />
        
</ client >
    
</ system.serviceModel >
</ configuration >

我们现在就可以来进行Soap Trace了,现在我们启动tcpTrace。进行如下的设置,Destination Server和Destination Port为Service Endpoint对应的Host和Port。我们甚至还可以通过Log文件把Trace保存起来。


然后先后运行Service和Client,你将会在tcpTrace上看到他所截获的Request和Response的内容:


而且相应的内容被记录到我们指定的Log文件中:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值