SOAP 异常删除自定义 HTTP 标头

本文关键字:HTTP 标头 自定义 删除 异常 SOAP | 更新日期: 2023-09-27 17:57:21

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

返回成功消息对象时,这工作正常,但是在返回 SOAPFault 消息时,所有自定义 HTTP 标头都会被清除,我找不到恢复它们的方法。客户端坚持必须发送失败消息。我尝试过使用SOAPExtension,但它看不到HTTP标头。我试过放置

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;
}

我不知所措...这是预期行为吗?如果是这样,有什么办法吗?还是我只是密集?

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

SOAP 异常删除自定义 HTTP 标头

对于将来遇到此问题的任何人,我最终确实破解了它(在同事的帮助下,功劳归于它)。

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

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