wcf - 记录 ClientBase 对象接收的 SOAP 原始响应

标签 wcf logging soap

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

即使在服务错误期间,也应该有一个跟踪 ID 值与响应一起返回,以便该服务的开发人员可以调试/排除任何问题。因此,我理想中想要的是记录出现异常时返回到我的 ClientBase 对象的原始响应。如果可能的话,我不想记录每条消息,这对 IMO 来说太过分了。

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

最佳答案

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

更新:添加示例代码

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

关于wcf - 记录 ClientBase 对象接收的 SOAP 原始响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12842014/

相关文章:

WCF - 为什么要为服务创建多个端点?

.net - 通过 WCF 进行身份验证的最佳方式是什么?

c# - 使用 WCF 的 Windows 服务

WCF - 不带 DataMemberAttribute 的 EmitDefaultValue

python - 如何使用守护进程 uwsgi 从 stderr 收集错误消息?

javascript - Node.js winston 日志记录使用邮件传输仅将 uncaughtException 作为电子邮件发送

java - 如何通过在 Java 中每隔几秒记录一次来避免日志垃圾邮件

java - 是否可以将 soap 消息写入 xml 文件?

java - 使用 javax.xml.namespace.Qname 和 javax.xml.ws.handler.PortInfo 的类转换异常

php - 从使用 php 构建的 Web 服务中获取空响应