SOAP 异常删除自定义 HTTP Headers

SOAP Exception Removes Custom HTTP Headers

我有一个 C# ASMX Web 服务(遗留项目,无能为力)。它必须接收的请求之一带有动态 HTTP header "TransactionID",并且必须在响应中以 HTTP header 返回。

返回成功消息时 objects 这工作正常,但是当返回 SOAPFault 消息时,所有自定义 HTTP headers 都被清除,我找不到恢复它们的方法。客户端坚持必须发送失败消息。我试过使用 SOAPExtension,但它没有 HTTP headers 的可见性。我试过放置

HttpContext.Current.Response.AddHeader("TransactionID", TransactionId);

在一系列不同的地方,无济于事。

代码如下:

[WebMethod(MessageName = "name")]
[SoapHeader("Header")]
[SoapMessageLoggingExtension]
[SoapDocumentMethod("http://www.contoso.com/DocumentLiteral", RequestElementName = "requestName", ResponseElementName = "responseName")]
[return: System.Xml.Serialization.XmlElementAttribute("ReturnType")]
public ResponseType WebMethodName(object RequestObject)
{
    bool success = true;
    int errorType = 0;
    string errorMsg = String.Empty;
    string TransactionId = String.Empty;
    SoapException soapEx = null;

    try
    {
        TransactionId = HttpContext.Current.Request.Headers["TransactionID"].ToString();

        //Determine success or throw appropriate exception
    }
    catch   //example exception
    {
        success = false;
        ErrorType = 1;
        errorMsg = "ErrorText";
        soapEx = new SoapException(_errorType.ToString() + " : " + errorMsg, SoapException.ServerFaultCode);
    }
    finally
    {
        HttpContext.Current.Response.AddHeader("TransactionID", TransactionId);
        if (success)
        {
            resp = new SuccessResponse();
        }
    }

    if (soapEx != null) throw soapEx;
    return resp;
}

我不知所措...这是预期的行为吗?如果是这样,有什么办法可以解决吗?还是我太笨了?

如果我遗漏了任何重要信息,请告诉我,谢谢。

对于将来遇到此问题的任何人,我最终确实解决了这个问题(在同事的帮助下,归功于它应得的部分)。

我将 header 添加到响应 body 中,然后在发送响应后在 SoapMessageLoggingExtension 中搜索该内容,从中添加回 HTTP header 然后将其从 body 中删除。凌乱,但有效。

郑重声明,我仍然认为这是框架中的一个错误,除非有人可以解释为什么不是。