- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个应用程序,它使用由服务引用生成的 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/
我尝试在我的 MonoTouch 应用程序中调用 wcf 网络服务。在模拟器上一切正常。在设备上我收到错误:EXC_CRASH (SIGABRT) 我使用以下工具生成我的代理类:SlSvcUtil.e
我有以下代码: public partial class MyServiceClient : System.ServiceModel.ClientBase 派生,请使用此选项 m_MyClient.S
我正在开发使用网络服务的 xamarin 应用程序。 Xamarin 是否支持 System.ServiceModel.ClientBase 中的 X509Certificate? 在使用 .Net
如果 Dispose() 方法声明不可见/未声明,System.ServiceModel.ClientBase 抽象类如何实现 IDisposable 接口(interface)? 如果我尝试做同样的
我有一个应用程序,它使用由服务引用生成的 ClientBase 对象来调用第三方 WCF SOAP 服务。每隔一段时间,服务调用就会返回一个错误异常,而不是带有“处理请求时发生错误”的异常消息,这是完
我正在使用一个扩展 ClientBase<> 的类: interface IService {} class MyServiceClient : ClientBase {} 我遇到的问题是 FxCop
ClientBase 的 C# 定义是: public abstract class ClientBase : ICommunicationObject, IDisposable where
System.ServiceModel.Clientbase.Open() 有什么作用?我从未使用过它,只是在一些代码中遇到过它。它能抛出异常吗?如果不调用 Close() 是否有问题? 最佳答案 如
我有一个 WCF 客户端/服务器,客户端代理正在使用 ClientBase 。当访问客户端代理时,对任何服务的第一次调用大约需要 8 秒。后续调用(在 15 秒超时内)需要约 0.5 秒。我找到了以下
我正在研究 ClientBase 中采用的使用模式和 ChannelFactory有一点我无法理解。 在大多数例子中,我们都有这种用法 public class MyClient : ClientBa
我正在将 .NET WCF 应用程序移至 Mono 以在 iOS 中使用。因为iOS中的Mono不支持动态代码生成,所以我需要重写ClientBase.CreateChannel ,如本文所述: Mo
我正在开发一个包含 WCF 服务和使用该服务的客户端的解决方案。有时我正在调试服务,有时是客户端,有时两者都是。 在调试期间,我得到一个带有附加信息的 TimeoutException 附加信息:请求
我们目前正在使用 Web 服务 (IBM Message Broker)。由于该服务仍在开发中,在许多情况下它会返回无效的 XML(是的,我保证会修复这个问题)。 从 .NET 调用此服务时出现问题,
我有一个第三方 API 的服务引用。我使用自定义 ChannelFactory 类来构建此 API 的 channel (类型为 System.ServiceModel.ClientBase 的 [W
前一段时间我看到一篇有趣的文章,解释了将 HttpClient在 using block 中将在代码执行但不关闭 TCP 套接字时处理该对象,并且 TCP 状态最终将进入 TIME_WAIT 并在该状
在早期版本的 C#.Net 中,处理 WCF 代理(服务引用生成的客户端)时存在问题 - 这在 .Net 4.5+ 中是否仍然存在? 可以在此处找到此问题的完整详细信息... https://codi
我是一名优秀的程序员,十分优秀!