1

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

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.IncomingMessageHeaders返回 2. 扩展看起来很顺利,为什么我在. 3.是否有更好/更简单或至少工作良好的方法来从响应中获取此标头? nullreply.Headers.FindHeaderAfterReceiveReply
OperationResultContextExtensionnullOperationContext.Current.Extensions.Find<OperationResultContextExtension>()

4

1 回答 1

3

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

ManagerSoapClient client = new ManagerSoapClient();

using (OperationContextScope scope = new OperationContextScope(client.InnerChannel))
{
    client.Authenticate(...); // <---- The change
    var headers = OperationContext.Current.IncomingMessageHeaders;
}
于 2013-10-18T00:37:44.330 回答