if(client.InnerChannel.State != System.ServiceModel.CommunicationState.Faulted)
{
// call service - everything's fine
}
else
{
// channel faulted - re-create your client and then try again
}
try
{
... some code here
}
catch (Exception ex)
{
throw new FaultException(ex.Message)
}
Tell WCF to handle all Exceptions using an Errorhandler. This can be done in several ways, I chose a simple one using an Attribute:
All we have to do more, is to use the attribute [SvcErrorHandlerBehaviour] on the wanted Service Implementation
using System;
using System.Collections.ObjectModel;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;
namespace MainService.Services
{
/// <summary>
/// Provides FaultExceptions for all Methods Calls of a Service that fails with an Exception
/// </summary>
public class SvcErrorHandlerBehaviourAttribute : Attribute, IServiceBehavior
{
public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{ } //implementation not needed
public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints,
BindingParameterCollection bindingParameters)
{ } //implementation not needed
public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
foreach (ChannelDispatcherBase chanDispBase in serviceHostBase.ChannelDispatchers)
{
ChannelDispatcher channelDispatcher = chanDispBase as ChannelDispatcher;
if (channelDispatcher == null)
continue;
channelDispatcher.ErrorHandlers.Add(new SvcErrorHandler());
}
}
}
public class SvcErrorHandler: IErrorHandler
{
public bool HandleError(Exception error)
{
//You can log th message if you want.
return true;
}
public void ProvideFault(Exception error, MessageVersion version, ref Message msg)
{
if (error is FaultException)
return;
FaultException faultException = new FaultException(error.Message);
MessageFault messageFault = faultException.CreateMessageFault();
msg = Message.CreateMessage(version, messageFault, faultException.Action);
}
}
}
This is an easy example, you can dive deeper into IErrorhandler by not using the naked FaultException, but a FaultException<> with a type that provides additional info
see IErrorHandler for an detailed example.
此错误也可由您自己的计算机触发,而不仅仅是未处理的异常。如果您的服务器/计算机的时钟时间关闭太多分钟,许多。NET Web 服务将拒绝您的请求,并出现未处理的错误。这是从他们的角度来处理的,但是从你的角度来看没有处理过。检查以确保接收服务器的时钟时间是正确的。如果需要修复,您必须在通道重新打开之前重置您的服务或重新启动。
我在一台服务器上遇到了这个问题,防火墙阻止了互联网时间的更新,服务器由于某种原因停机了。所有的第三方。NET Web 服务出现故障,因为它们拒绝了任何 Web 服务请求。深入研究事件查看器有助于识别问题,但调整时钟解决了问题。即使我们收到了用于未来 Web 服务调用的错误状态错误消息,错误仍在我们这一端。