如何在AfterReceiveRequest方法中从WCF获取datacontract的datamember的SOAP请求值

时间:2015-01-27 11:50:58

标签: wcf soap

如何在AfterReceiveRequest方法中从WCF获取datacontract的datamember的SOAP请求值* /

SOAP请求

<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing">
<s:Header>
<a:Action s:mustUnderstand="1">http://tempuri.org/ITcpService/GetData</a:Action>
<a:MessageID>urn:uuid:fa410c1f-4b74-42e5-998d-565cd5c7b092</a:MessageID>
<a:ReplyTo>
<a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address>
</a:ReplyTo>
<VsDebuggerCausalityData xmlns="http://schemas.microsoft.com/vstudio/diagnostics/servicemodelsink">uIDPoyXtX3m/PSJBo+4Vd2taPwgAAAAAB+2JFplBDU6oJYlfkZ/um28yri7PLBZFmtLNf3F75EMACQAA</VsDebuggerCausalityData>
<a:To s:mustUnderstand="1">net.tcp://localhost/mytcpservice/TcpService.svc</a:To>
</s:Header>
<s:Body>
<GetData xmlns="http://tempuri.org/">
<value xmlns:b="http://schemas.datacontract.org/2004/07/mytcpservice.requests" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<b:Token>0</b:Token>
<b:Data>7</b:Data>
<b:Data2>0</b:Data2>
</value>
</GetData>
 </s:Body>
 </s:Envelope>

public class TestActivation : IDispatchMessageInspector
{
public object AfterReceiveRequest(ref Message request, IClientChannel     channel, InstanceContext instanceContext)
{
//here fetch Token value if exists
}
}

1 个答案:

答案 0 :(得分:0)

您可以使用“request”变量获取对请求数据的访问权限。例如您可以按如下方式检索用户名等标头值:

public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
    {
        var requestHeaders = (HttpRequestMessageProperty)request.Properties[HttpRequestMessageProperty.Name];
        var user = requestHeaders .Headers["Username"];

        //Way to access body. 
        MemoryStream ms = new MemoryStream();
        XmlWriter writer = XmlWriter.Create(ms);
        request.WriteMessage(writer); 
        writer.Flush();
        ms.Position = 0;
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(ms);
        //Now you have xml document object which can be used for further processing
        //Do whatever you want to do here   
        return null;
    }