Register SOAP Raw Response Received by ClientBase

I have an application that uses a ClientBase object generated by a service reference to invoke a third-party WCF SOAP service. Every time after some time, the service call returns a failure exception instead of the error message "Request processing error", which is completely general.

Even during service errors, it is assumed that the value of the trace identifier is returned with a response so that the developers of this service can debug / fix any problems. So, ideally, I want to record the original response, which returns to my ClientBase object when there is an exception. I do not want to log every message, if possible, which will be excessive IMO.

Is there a way to do this using ClientBase? Maybe there is some kind of context object that contains the contents of the raw response? This must be integrated into my application, if possible. I know that there are tools that act as a proxy between the client and the service, which can register HTTP requests / responses, but this is not what I want.

+4
source share
1 answer

There is no place on ClientBase where you can get this information. But you can add a personalized message inspector ( IClientMessageInspector ) to the client, where you can see all the messages that are received; for these messages, you can check the IsFault property, and if true, register the message as you want.

Update: adding sample code

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

Source: https://habr.com/ru/post/1439183/


All Articles