Search code examples
c#loggingtrace

how to trace soap xml as a webservice client in netcore?


the below code is the auto generated using vs.

[System.ServiceModel.OperationContractAttribute(Action="", ReplyAction="*")]
[System.ServiceModel.XmlSerializerFormatAttribute(SupportFaults=true)]
[System.ServiceModel.ServiceKnownTypeAttribute(typeof(response))]
[System.ServiceModel.ServiceKnownTypeAttribute(typeof(request))]
[System.ServiceModel.ServiceKnownTypeAttribute(typeof(product[]))]
System.Threading.Tasks.Task<PartnerService.cancelOrderResponse> cancelOrderAsync(PartnerService.cancelOrderRequest1 request);
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
System.Threading.Tasks.Task<PartnerService.cancelOrderResponse> PartnerService.PartnerServicePortType.cancelOrderAsync(PartnerService.cancelOrderRequest1 request)
{
    return base.Channel.cancelOrderAsync(request);
}

There is no soapExtensionAttribute class in netcore. it only exists in netframework. so i donot know what is the attribute in netcore.


Solution

  • create two class file

        public class InspectorBehavior : IEndpointBehavior
        {
            public string LastRequestXML
            {
                get
                {
                    return myMessageInspector.LastRequestXML;
                }
            }
    
            public string LastResponseXML
            {
                get
                {
                    return myMessageInspector.LastResponseXML;
                }
            }
    
            public int TimeSpan { 
                get {
                    return myMessageInspector.TimeSpan;
                } 
            }
            public int ResponseCode { get { return myMessageInspector.ResponseCode; } }
    
            public string URL { get { return myMessageInspector.URL; } }
    
            private MyMessageInspector myMessageInspector = new MyMessageInspector();
            public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
            {
    
            }
    
            public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
            {
    
            }
    
            public void Validate(ServiceEndpoint endpoint)
            {
    
            }
    
    
            public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
            {
                clientRuntime.ClientMessageInspectors.Add(myMessageInspector);
            }
        }
    
        public class MyMessageInspector : IClientMessageInspector
        {
            public string LastRequestXML { get; private set; }
            public string LastResponseXML { get; private set; }
    
            public int TimeSpan { get; private set; }
    
            public int ResponseCode { get; private set; }
    
            public string URL { get; private set; }
    
            private Stopwatch stopwatch { get; set; }
    
            public void AfterReceiveReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
            {
                stopwatch.Stop();
                LastResponseXML = reply.ToString();
                this.TimeSpan = (int)stopwatch.Elapsed.TotalSeconds;
            }
    
            public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel)
            {
                LastRequestXML = request.ToString();
                URL = channel.RemoteAddress.Uri.AbsoluteUri;
                stopwatch = new Stopwatch();
                stopwatch.Start();
                return request;
            }
        }
    
    

    when client call

    var requestInterceptor = new InspectorBehavior();
    this.soapClient.Endpoint.EndpointBehaviors.Add(requestInterceptor);
    await this.soapClient.cancelOrderAsync(...);
    Console.WriteLine(requestInsterceptor.LastRequestXML);
    Console.WriteLine(requestInsterceptor.LastResponseXML);