如何记录SOAP请求?

时间:2018-12-27 17:45:05

标签: c# visual-studio web-services wcf soap

众所周知,在Visual Studio 2017中,您可以使用相关的WSDL添加Connected Service并将其配置为指向您想要的SOAP服务。这样会在Reference.cs文件中创建一组代理类,用于与相关SOAP服务进行接口。

是否有一种方法可以在     过程-以编程方式-因此我们可以确切知道     发送到目标服务?我之所以这样问,是因为我需要能够记录发送出去的实际SOAP,因此不能使用诸如Fiddler之类的单独应用程序。

我已经仔细研究了Reference.cs文件中的代码,所有内容都被抽象得如此抽象,以至于我不知道转换实际上在哪里发生。

为了便于讨论,我使用了一个非常简单的免费SOAP服务,位于:http://www.dneonline.com/calculator.asmx

生成的Reference.cs文件的内容显示在本文的结尾。在该代码中,您可以看到客户端类的方法AddAsync(...)调用了base.Channel.AddAsync(intA, intB),该方法在CalculatorSoap界面中定义。因此,如果ChannelCalculatorSoap接口,那么实际使用的具体类在哪里?这就是这么多抽象的普遍性的一个例子。

我很高兴(也很感激)听到你们任何人关于这里幕后发生的一切想法。

谢谢。

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     //
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace CalculatorSoapServiceReference
{


    [System.CodeDom.Compiler.GeneratedCodeAttribute("dotnet-svcutil", "1.0.0.1")]
    [System.ServiceModel.ServiceContractAttribute(ConfigurationName="CalculatorSoapServiceReference.CalculatorSoap")]
    public interface CalculatorSoap
    {

        [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/Add", ReplyAction="*")]
        System.Threading.Tasks.Task<int> AddAsync(int intA, int intB);

        [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/Subtract", ReplyAction="*")]
        System.Threading.Tasks.Task<int> SubtractAsync(int intA, int intB);

        [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/Multiply", ReplyAction="*")]
        System.Threading.Tasks.Task<int> MultiplyAsync(int intA, int intB);

        [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/Divide", ReplyAction="*")]
        System.Threading.Tasks.Task<int> DivideAsync(int intA, int intB);
    }

    [System.CodeDom.Compiler.GeneratedCodeAttribute("dotnet-svcutil", "1.0.0.1")]
    public interface CalculatorSoapChannel : CalculatorSoapServiceReference.CalculatorSoap, System.ServiceModel.IClientChannel
    {
    }

    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.CodeDom.Compiler.GeneratedCodeAttribute("dotnet-svcutil", "1.0.0.1")]
    public partial class CalculatorSoapClient : System.ServiceModel.ClientBase<CalculatorSoapServiceReference.CalculatorSoap>, CalculatorSoapServiceReference.CalculatorSoap
    {

    /// <summary>
    /// Implement this partial method to configure the service endpoint.
    /// </summary>
    /// <param name="serviceEndpoint">The endpoint to configure</param>
    /// <param name="clientCredentials">The client credentials</param>
    static partial void ConfigureEndpoint(System.ServiceModel.Description.ServiceEndpoint serviceEndpoint, System.ServiceModel.Description.ClientCredentials clientCredentials);

        public CalculatorSoapClient(EndpointConfiguration endpointConfiguration) : 
                base(CalculatorSoapClient.GetBindingForEndpoint(endpointConfiguration), CalculatorSoapClient.GetEndpointAddress(endpointConfiguration))
        {
            this.Endpoint.Name = endpointConfiguration.ToString();
            ConfigureEndpoint(this.Endpoint, this.ClientCredentials);
        }

        public CalculatorSoapClient(EndpointConfiguration endpointConfiguration, string remoteAddress) : 
                base(CalculatorSoapClient.GetBindingForEndpoint(endpointConfiguration), new System.ServiceModel.EndpointAddress(remoteAddress))
        {
            this.Endpoint.Name = endpointConfiguration.ToString();
            ConfigureEndpoint(this.Endpoint, this.ClientCredentials);
        }

        public CalculatorSoapClient(EndpointConfiguration endpointConfiguration, System.ServiceModel.EndpointAddress remoteAddress) : 
                base(CalculatorSoapClient.GetBindingForEndpoint(endpointConfiguration), remoteAddress)
        {
            this.Endpoint.Name = endpointConfiguration.ToString();
            ConfigureEndpoint(this.Endpoint, this.ClientCredentials);
        }

        public CalculatorSoapClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) : 
                base(binding, remoteAddress)
        {
        }

        public System.Threading.Tasks.Task<int> AddAsync(int intA, int intB)
        {
            return base.Channel.AddAsync(intA, intB);
        }

        public System.Threading.Tasks.Task<int> SubtractAsync(int intA, int intB)
        {
            return base.Channel.SubtractAsync(intA, intB);
        }

        public System.Threading.Tasks.Task<int> MultiplyAsync(int intA, int intB)
        {
            return base.Channel.MultiplyAsync(intA, intB);
        }

        public System.Threading.Tasks.Task<int> DivideAsync(int intA, int intB)
        {
            return base.Channel.DivideAsync(intA, intB);
        }

        public virtual System.Threading.Tasks.Task OpenAsync()
        {
            return System.Threading.Tasks.Task.Factory.FromAsync(((System.ServiceModel.ICommunicationObject)(this)).BeginOpen(null, null), new System.Action<System.IAsyncResult>(((System.ServiceModel.ICommunicationObject)(this)).EndOpen));
        }

        public virtual System.Threading.Tasks.Task CloseAsync()
        {
            return System.Threading.Tasks.Task.Factory.FromAsync(((System.ServiceModel.ICommunicationObject)(this)).BeginClose(null, null), new System.Action<System.IAsyncResult>(((System.ServiceModel.ICommunicationObject)(this)).EndClose));
        }

        private static System.ServiceModel.Channels.Binding GetBindingForEndpoint(EndpointConfiguration endpointConfiguration)
        {
            if ((endpointConfiguration == EndpointConfiguration.CalculatorSoap))
            {
                System.ServiceModel.BasicHttpBinding result = new System.ServiceModel.BasicHttpBinding();
                result.MaxBufferSize = int.MaxValue;
                result.ReaderQuotas = System.Xml.XmlDictionaryReaderQuotas.Max;
                result.MaxReceivedMessageSize = int.MaxValue;
                result.AllowCookies = true;
                return result;
            }
            if ((endpointConfiguration == EndpointConfiguration.CalculatorSoap12))
            {
                System.ServiceModel.Channels.CustomBinding result = new System.ServiceModel.Channels.CustomBinding();
                System.ServiceModel.Channels.TextMessageEncodingBindingElement textBindingElement = new System.ServiceModel.Channels.TextMessageEncodingBindingElement();
                textBindingElement.MessageVersion = System.ServiceModel.Channels.MessageVersion.CreateVersion(System.ServiceModel.EnvelopeVersion.Soap12, System.ServiceModel.Channels.AddressingVersion.None);
                result.Elements.Add(textBindingElement);
                System.ServiceModel.Channels.HttpTransportBindingElement httpBindingElement = new System.ServiceModel.Channels.HttpTransportBindingElement();
                httpBindingElement.AllowCookies = true;
                httpBindingElement.MaxBufferSize = int.MaxValue;
                httpBindingElement.MaxReceivedMessageSize = int.MaxValue;
                result.Elements.Add(httpBindingElement);
                return result;
            }
            throw new System.InvalidOperationException(string.Format("Could not find endpoint with name \'{0}\'.", endpointConfiguration));
        }

        private static System.ServiceModel.EndpointAddress GetEndpointAddress(EndpointConfiguration endpointConfiguration)
        {
            if ((endpointConfiguration == EndpointConfiguration.CalculatorSoap))
            {
                return new System.ServiceModel.EndpointAddress("http://www.dneonline.com/calculator.asmx");
            }
            if ((endpointConfiguration == EndpointConfiguration.CalculatorSoap12))
            {
                return new System.ServiceModel.EndpointAddress("http://www.dneonline.com/calculator.asmx");
            }
            throw new System.InvalidOperationException(string.Format("Could not find endpoint with name \'{0}\'.", endpointConfiguration));
        }

        public enum EndpointConfiguration
        {

            CalculatorSoap,

            CalculatorSoap12,
        }
    }
}

1 个答案:

答案 0 :(得分:1)

对于第3个问题,您可以使用Message Inspector扩展名。有details here on how to set it up

使用消息检查器,您可以查看,保留,处理外发请求,传入回复,传入请求和传出回复的SOAP消息。

关于内部工作原理,WCF还使用Message对象来处理SOAP信封。您可以阅读更多about that here

该消息是渠道模型的一部分,其中有一个bit of info here