如何使用WCF客户端从响应中读取自定义SOAP标头

时间:2012-04-11 16:34:22

标签: .net wcf service header

所以有我的问题:我无法通过使用生成的wcf代理(服务引用)获取Web服务响应的头,而服务是asmx。

ManagerSoapClient client = new ManagerSoapClient();
client.Authenticate(...);
using (OperationContextScope scope = new OperationContextScope(client.InnerChannel))
{
    //headers = null
    var headers = OperationContext.Current.IncomingMessageHeaders;
}

这很奇怪,因为SOAP响应有一些标题:

<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:Header>
      <OperationResult xmlns="http://tempuri.org/">
         <Status>Success</Status>
      </OperationResult>
   </soap:Header>
   <soap:Body>
      ...
   </soap:Body>
</soap:Envelope>

好的,我添加了自定义IClientMessageInspector来检查标题:

public class OperationResultMessageInspector : IClientMessageInspector
{
    public object BeforeSendRequest(ref Message request, IClientChannel channel)
    {           
        return channel;
    }

    public void AfterReceiveReply(ref Message reply, object correlationState)
    {
        //It founds header at position 0!!
        int headerIndex = reply.Headers.FindHeader("OperationResult", "http://tempuri.org/");
    }
}

毕竟有一个标题......但为什么我无法使用OperationContext.Current.IncomingMessageHeaders访问它?

确定。现在我只想在某个地方保存这个标题,以便在服务调用后能够访问它。我决定使用扩展程序IExtension<OperationContext>
所以我的代码现在是下一个:

public class ManagerProxy
{
    public void Authenticate()
    {
        ManagerSoapClient client = new ManagerSoapClient();
        client.Endpoint.Behaviors.Add(new OperationResultBehavior());
        client.Authenticate(...);

        using (OperationContextScope scope = new OperationContextScope(client.InnerChannel))
        {
            //headers = null
            var headers = OperationContext.Current.IncomingMessageHeaders;
            //header = null
            OperationResultContextExtension header = OperationContext.Current.Extensions.Find<OperationResultContextExtension>();
        }
    }
}

public class OperationResultMessageInspector : IClientMessageInspector
{
    public object BeforeSendRequest(ref Message request, IClientChannel channel)
    {           
        return channel;
    }

    public void AfterReceiveReply(ref Message reply, object correlationState)
    {
        IClientChannel channel = (IClientChannel)correlationState;

        //It founds header at position 0!!
        int headerIndex = reply.Headers.FindHeader("OperationResult", "http://tempuri.org/");
        XmlDictionaryReader reader = reply.Headers.GetReaderAtHeader(headerIndex);

        OperationResultContextExtension extension = new OperationResultContextExtension
        {
            SomeData = reader.ReadString()
        };          

        using (OperationContextScope scope = new OperationContextScope(channel))
        {               
            OperationContext.Current.Extensions.Add(extension);
        }
        reader.Close();
    }

}

public class OperationResultContextExtension : IExtension<OperationContext>
{
    public string SomeData { get; set; }
    public void Attach(OperationContext owner) { }
    public void Detach(OperationContext owner) { }
}

public class OperationResultBehavior : IEndpointBehavior
{
    public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters) { }
    public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher) { }
    public void Validate(ServiceEndpoint endpoint) { }
    public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
    { clientRuntime.MessageInspectors.Add(new OperationResultMessageInspector()); }
}

它应该有用,但我OperationContext.Current.Extensions.Find<OperationResultContextExtension>();出乎意料的是null

所以有我的问题:
1. OperationContext.Current.IncomingMessageHeadersnullreply.Headers.FindHeader找到标题时,为什么AfterReceiveReply会返回OperationResultContextExtension 2.分机null看起来非常流畅,为什么我在OperationContext.Current.Extensions.Find<OperationResultContextExtension>()期间获得{{1}} 3.是否有更好/更简单或至少运行良好的方式从响应中获取此标头?

1 个答案:

答案 0 :(得分:3)

我有一个类似的问题,在我的情况下,我通过将客户端调用放在OperationContestScope

中来解决它
ManagerSoapClient client = new ManagerSoapClient();

using (OperationContextScope scope = new OperationContextScope(client.InnerChannel))
{
    client.Authenticate(...); // <---- The change
    var headers = OperationContext.Current.IncomingMessageHeaders;
}