繁体   English   中英

从WCF(vb.net)中将SOAP正文提取为XML

[英]Extract SOAP body as XML from WCF (vb.net)

我要在vb.net中建立一个要联系JAX-WS Web服务的客户端。

我打算使用WCF框架,因为这简化了安全性设置。

该请求也是以标准方式构建的,但是我需要将主体响应直接提取到XML(流),因为我需要通过xlst样式表对其进行转换。 此外,Web服务的响应会随着时间而变化,我不想每次将新属性添加到Web服务时都进行新的构建,而只能在样式表中进行处理。

我用谷歌搜索了一个例子,特别是研究了这些文章:

使用WCF 1的自定义消息处理

使用WCF 2的自定义消息处理

但是,我不清楚它是如何工作的。 从其他文章中,我看到消息类似乎起着重要的作用,但是我看不到如何通过WCF来控制该对象。

我在C#中的能力有限,因此希望在vb.net中提供示例。

有什么帮助吗?

我最终(按照建议)使用了IMessageInspector。 通过将以下代码添加到“服务参考”下的自动生成的Reference.vb中,可以做到这一点:

Public Class WindchillResponseInspector 'System.ServiceModel.Description.IEndpointBehavior
    Implements IClientMessageInspector


    Public Sub AfterReceiveReply(ByRef reply As Message, correlationState As Object) Implements IClientMessageInspector.AfterReceiveReply


        Dim buffer As MessageBuffer = reply.CreateBufferedCopy(Integer.MaxValue)
        Dim replycopy As Message = buffer.CreateMessage()
        reply = buffer.CreateMessage

        ResponseStream = New IO.MemoryStream

        'Dim stream As New FileStream("myfile.xml", FileMode.Create)
        Dim xdw As Xml.XmlDictionaryWriter = Xml.XmlDictionaryWriter.CreateTextWriter(ResponseStream)
        '    reply.WriteMessage(xdw)
        replycopy.WriteBodyContents(xdw)
        xdw.Flush()
        'printStream(ResponseStream)
    End Sub

    Public Function BeforeSendRequest(ByRef request As Message, channel As IClientChannel) As Object Implements IClientMessageInspector.BeforeSendRequest
        Dim buffer As MessageBuffer = request.CreateBufferedCopy(Integer.MaxValue)
        Dim requestcopy As Message = buffer.CreateMessage()
        request = buffer.CreateMessage

        Dim requestStream = New IO.MemoryStream

        'Dim stream As New FileStream("myfile.xml", FileMode.Create)
        Dim xdw As Xml.XmlDictionaryWriter = Xml.XmlDictionaryWriter.CreateTextWriter(requestStream)
        '    reply.WriteMessage(xdw)
        requestcopy.WriteMessage(xdw)
        xdw.Flush()
        printStream(requestStream)
        Return Nothing
    End Function

End Class

为了使此代码正常工作,我必须将其添加到行为中:

Public Class WindchillInspectorBehavior
    Implements IEndpointBehavior

    Public Sub AddBindingParameters(endpoint As ServiceEndpoint, bindingParameters As BindingParameterCollection) Implements IEndpointBehavior.AddBindingParameters

    End Sub

    Public Sub ApplyClientBehavior(endpoint As ServiceEndpoint, clientRuntime As ClientRuntime) Implements IEndpointBehavior.ApplyClientBehavior
        Dim inspector As New WindchillResponseInspector
        clientRuntime.ClientMessageInspectors.Add(inspector)

    End Sub

    Public Sub ApplyDispatchBehavior(endpoint As ServiceEndpoint, endpointDispatcher As EndpointDispatcher) Implements IEndpointBehavior.ApplyDispatchBehavior

    End Sub

    Public Sub Validate(endpoint As ServiceEndpoint) Implements IEndpointBehavior.Validate

    End Sub
End Class

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM