【问题标题】:how to trace soap xml as a webservice client in netcore?如何在网络核心中将soap xml跟踪为Web服务客户端?
【发布时间】:2020-11-04 16:50:38
【问题描述】:

以下代码是使用 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);
}

netcore 中没有soapExtensionAttribute 类。它只存在于网络框架中。所以我不知道netcore中的属性是什么。

【问题讨论】:

    标签: c# logging trace


    【解决方案1】:

    创建两个类文件

        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;
            }
        }
    
    

    当客户来电时

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

    【讨论】:

      猜你喜欢
      • 2014-07-04
      • 2011-05-22
      • 2010-12-12
      • 1970-01-01
      • 2011-10-11
      • 1970-01-01
      • 2019-08-23
      • 2016-07-07
      • 1970-01-01
      相关资源
      最近更新 更多