我使用:
1)AppDomain.CurrentDomain.UnhandledException事件
2)TaskScheduler.UnobservedTaskException事件
3)IErrorHandler:
public class ErrorHandler : IErrorHandler
{
public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
{
var faultException = new FaultException<string>("Server error: " + error.Format());
var messageFault = faultException.CreateMessageFault();
fault = Message.CreateMessage(version, messageFault, null);
}
public bool HandleError(Exception error)
{
return false;
//return true; //if handled
}
}
[AttributeUsage(AttributeTargets.Class)]
public class ErrorHandlerBehavior : Attribute, IEndpointBehavior, IServiceBehavior
{
public void Validate(ServiceEndpoint endpoint)
{
}
public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
{
}
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{
endpointDispatcher.ChannelDispatcher.ErrorHandlers.Add(new ErrorHandler());
}
public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
}
public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
}
public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
{
}
public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
foreach (ChannelDispatcher channelDispatcher in serviceHostBase.ChannelDispatchers)
{
channelDispatcher.ErrorHandlers.Add(new ErrorHandler());
}
}
}
這可以應用到整個服務impl。類:
[ErrorHandlerBehavior]
public class SubscriberInfoTaskService : {}
或端點:
var endpoint = Host.Description.Endpoints.FirstOrDefault();
//foreach (ChannelDispatcher channelDispatcher in Host.ChannelDispatchers) //ChannelDispatcherBase
//{
// channelDispatcher.ErrorHandlers.Add(new ErrorHandler());
//}
endpoint.Behaviors.Add(new ErrorHandlerBehavior());
這裏關於使用配置:http://www.steverb.com/post/2008/11/24/Useful-WCF-Behaviors-IErrorHandler.aspx
我怎樣才能讓 「op.ParameterInspectors.Add(新ValidatingParameterInspector());」在線編譯?我是否需要繼承其他課程? – user1886415 2013-02-28 15:54:37
對不起,你不需要這個。我刪除了這些行。 – SalientBrain 2013-02-28 16:20:09
感謝上帝!這終於奏效了。我一直在使用Google搜索整個上午,沒有一篇文章顯示100%清晰的解決方案。你是我的英雄人,剛剛救了我的一天:) – WtFudgE 2016-04-11 04:35:24