记录由ClientBase对象接收的SOAP原始响应 [英] Logging SOAP Raw Response Received by a ClientBase object

查看:84
本文介绍了记录由ClientBase对象接收的SOAP原始响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,该应用程序使用由服务引用生成的ClientBase对象来调用第三方WCF SOAP服务.服务调用每隔一段时间就会返回错误异常,而不是异常消息处理请求时发生错误",这是完全通用的.

I have an application that uses a ClientBase object generated by a service reference to call a third party WCF SOAP service. Every once in awhile, the service call returns a fault exception instead with an exception message of "An error occurred while processing the request" which is completely generic.

即使在服务出错期间,也应该在响应中返回一个跟踪ID值,以便该服务的开发人员可以调试/解决任何问题.因此,理想情况下,我要记录的是发生异常时返回到ClientBase对象的原始响应.如果可能的话,我不想记录每条消息,那将是对IMO的过大杀伤力.

Even during service errors, there is supposed to be a trace ID value that comes back with the response so the developers of that service can debug/troubleshoot any issues. So, what I ideally want is to log the raw response that comes back to my ClientBase object when there is an exception. I don't want to log every single message if possible, that would be overkill IMO.

有没有一种方法可以使用ClientBase捕获它?也许有一些包含原始响应内容的上下文对象?如果可能的话,这需要内置到我的应用程序中.我知道有一些工具可以充当客户端和服务之间的代理,可以记录http请求/响应,但这不是我想要的.

Is there a way to capture that using the ClientBase? Maybe there's some context object that contains the raw response content? This needs to be built into my application if possible. I know there are tools out there that act as a proxy between the client and service that can log http requests/responses but that is not what I'm after.

推荐答案

ClientBase本身,没有地方可以获取该信息.但是,您可以在客户端(IClientMessageInspector)上添加自定义消息检查器,在这里您可以查看正在接收的所有消息.对于这些消息,您可以检查IsFault属性,如果为true,则根据需要记录该消息.

At the ClientBase itself there's no place where you can get that information. But you can add a custom message inspector at the client (IClientMessageInspector) where you can see all the messages that are being received; for those messages you can check the IsFault property, and if true log the message as you want.

更新:添加示例代码

using System.ServiceModel.Channels;
using System.ServiceModel.Dispatcher;

public class StackOverflow_12842014
{
    [ServiceContract]
    public interface ITest
    {
        [OperationContract]
        string Echo(string text);
    }
    public class Service : ITest
    {
        public string Echo(string text)
        {
            if (text == "throw") throw new ArgumentException("Throwing as requested");
            return text;
        }
    }
    class MyClient : ClientBase<ITest>, ITest
    {
        public MyClient(Binding binding, EndpointAddress address)
            : base(binding, address)
        {
            this.Endpoint.Behaviors.Add(new MyFaultLogger());
        }

        public string Echo(string text)
        {
            return this.Channel.Echo(text);
        }

        class MyFaultLogger : IEndpointBehavior, IClientMessageInspector
        {
            public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
            {
            }

            public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
            {
                clientRuntime.MessageInspectors.Add(this);
            }

            public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
            {
            }

            public void Validate(ServiceEndpoint endpoint)
            {
            }

            public void AfterReceiveReply(ref Message reply, object correlationState)
            {
                if (reply.IsFault)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Fault received!: {0}", reply);
                    Console.ResetColor();
                }
            }

            public object BeforeSendRequest(ref Message request, IClientChannel channel)
            {
                return null;
            }
        }
    }
    public static void Test()
    {
        string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
        ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));
        host.Description.Behaviors.Find<ServiceDebugBehavior>().IncludeExceptionDetailInFaults = true;
        host.AddServiceEndpoint(typeof(ITest), new BasicHttpBinding(), "");
        host.Open();
        Console.WriteLine("Host opened");

        MyClient client = new MyClient(new BasicHttpBinding(), new EndpointAddress(baseAddress));

        Console.WriteLine(client.Echo("Hello"));
        try
        {
            Console.WriteLine(client.Echo("throw"));
        }
        catch (Exception)
        {
            Console.WriteLine("The fault should have been traced");
        }

        client.Close();

        Console.Write("Press ENTER to close the host");
        Console.ReadLine();
        host.Close();
    }
}

这篇关于记录由ClientBase对象接收的SOAP原始响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆