简体   繁体   English

如何在netcore中将soap xml作为webservice客户端进行跟踪?

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

the below code is the auto generated using vs.下面的代码是使用 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. netcore 中没有soapExtensionAttribute 类。 it only exists in netframework.它只存在于网络框架中。 so i donot know what is the attribute in netcore.所以我不知道 netcore 中的属性是什么。

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

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

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