gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 10:09:17 25 4
gpt4 key购买 nike

我有一个应用程序,它使用由服务引用生成的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com