当invoke的返回值为null时,C#捕获soap消息

时间:2013-11-22 00:39:00

标签: c# wcf web-services soap attributes

当以下代理Web服务返回空值时,我需要将返回的soap消息存储在我的数据库中,以防止特定的UserID。 我目前正在将Trace消息捕获到TraceExtension类中的文本文件中。但由于属性元数据是常量,我无法在运行时将UserID传递给此类。这有什么解决方法吗?

[ TraceExtensions.TraceExtension(), System.Web.Services.Protocols.SoapDocumentMethodAttribute("", Use = System.Web.Services.Description.SoapBindingUse.Literal)]
[return: System.Xml.Serialization.XmlElementAttribute("qualifyServicesResponse", Namespace = "")]
public qualifyServicesResponse qualifyServices([System.Xml.Serialization.XmlElementAttribute(Namespace = "")] qualifyServicesRequest qualifyServicesRequest, int UserID)
{
    object[] results = this.Invoke("qualifyServices", new object[] {
            qualifyServicesRequest, UserID});
    return ((qualifyServicesResponse)(results[0]));
}

TraceExtension类如下所示:

<AttributeUsage(AttributeTargets.Method)> _
Public Class TraceExtensionAttribute
    Inherits SoapExtensionAttribute

    Private m_filename As String = "C:\trace\log.xml"

End Class

然后我们覆盖SoapExtension类中的ProcessMessage:

Public Overrides Sub ProcessMessage(ByVal message As SoapMessage)
    Select Case message.Stage
        Case SoapMessageStage.BeforeSerialize
        Case SoapMessageStage.AfterSerialize
            WriteOutput(message)
        Case SoapMessageStage.BeforeDeserialize
            WriteInput(message)
        Case SoapMessageStage.AfterDeserialize
    End Select
End Sub

1 个答案:

答案 0 :(得分:0)

我最终在BeforeSerialize中使用message.GetInParameterValue来获取UserID:

Public Overrides Sub ProcessMessage(ByVal message As SoapMessage)

    Select Case message.Stage
        Case SoapMessageStage.BeforeSerialize
            UserID = Convert.ToInt32(message.GetInParameterValue(1))
        Case SoapMessageStage.AfterSerialize
            WriteOutput(message)
        Case SoapMessageStage.BeforeDeserialize
            WriteInput(message)
        Case SoapMessageStage.AfterDeserialize
    End Select
End Sub