c# - 将信息传递给应用了 SoapExtensionAttribute 的对象

标签 c# soap custom-attributes soap-extension

在我的应用程序中,我正在调用 Web 服务,通过使用 SoapExtension 和 SoapExtensionAttribute,我可以拦截传入和传出的 SOAP 消息以进行日志记录。我使用了 http://msdn.microsoft.com/en-us/magazine/cc164007.aspx 中的示例作为输入。 但现在我想更进一步。我有一个 Windows 客户端调用我的类(在一个单独的项目中),然后类调用 Web 服务。我现在可以截获 SOAP 消息,但我不想将它们直接记录到文件中,而是想将这些消息传递回调用 Web 服务的类以及调用类的客户端。这些是我到目前为止所做的代码更改:

        private String ExtractFromStream(Stream target)
    {
        if (target != null)
            return (new StreamReader(target)).ReadToEnd();

        return "";
    }

    public void WriteOutput(SoapMessage message)
    {
        newStream.Position = 0;
        string soapOutput = ExtractFromStream(newStream);

        newStream.Position = 0;
        Copy(newStream, oldStream);
    }

    public void WriteInput(SoapMessage message)
    {
        Copy(oldStream, newStream);

        newStream.Position = 0;
        string soapInput= ExtractFromStream(newStream);
        newStream.Position = 0;
    }

我现在想将 soapInput 和 soapOutput 传递回包含应用此属性的方法的类。关于我应该如何做的任何线索?

最佳答案

对于任何碰巧路过的人,这里是解决方案:

SoapMessage 对象不包含有关我的客户端的任何信息。但是,我可以将此对象转换为 SoapClientMessage 对象,然后我就可以访问我的 Web 服务。如果我现在向此 Web 服务添加一个方法(通过创建一个新的公共(public)部分类),我可以像这样访问它的属性和方法(纯粹是一个例子!):

 private String ExtractFromStream(Stream target)
    {
        if (target != null)
            return (new StreamReader(target)).ReadToEnd();

        return "";
    }



    public void WriteOutput(SoapMessage message)
    {
        newStream.Position = 0;

        string soapOutput = ExtractFromStream(newStream);
        SoapClientMessage soapClient = (SoapClientMessage)message;
        WebServiceClass webservice = (WebServiceClass)soapClient.Client;
        webservice.MyMethod(soapOutput); //Use your own method here!


        newStream.Position = 0;
        Copy(newStream, oldStream);
    }

    public void WriteInput(SoapMessage message)
    {
        Copy(oldStream, newStream);           
        newStream.Position = 0;

        string soapInput= ExtractFromStream(newStream);
        SoapClientMessage soapClient = (SoapClientMessage)message;
        WebServiceClass webservice = (WebServiceClass)soapClient.Client;
        webservice.MyMethod(soapInput);
        newStream.Position = 0;
    }

您可以通过创建一个新的公共(public)分部类并向其中添加方法、属性和任何您喜欢的内容来向您的 WebServiceClass 添加方法(如本例中的 MyMethod)。

关于c# - 将信息传递给应用了 SoapExtensionAttribute 的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4094702/

相关文章:

soap - 不能将 HTTPS 与 ServerXMLHTTP 对象一起使用

c# - 从 SOAP 响应中获取附件并保存文件

ios - NSURLConnection 失去连接

c# - 功能 'using declarations' 在 C# 7.3 中不可用。请使用语言版本 8.0 或更高版本 - 在一台计算机上出错,但在另一台计算机上正常

c# - Android 到 Web 服务。未传递的值

c# - 升级到 Visual Studio 2012.2 后发布网站停止工作

asp.net-core - 将 httpcontext 注入(inject)到 .net core Controller 外部的自定义属性中

C# 属性 hell - 在两个不同 SQL 平台上的移动设备和服务器之间共享一个类

c# - 是否可以将两个 C# 属性合并为一个?

c# - 创建泛型类型的实例