, IErrorHandler. , , , . -? :
public class ServiceErrorHandler : IErrorHandler
{
public ServiceErrorHandler()
{
}
public ServiceErrorHandler(string strUrl, bool bHandled)
{
this.strUrl_m = strUrl;
this.bHandled_m = bHandled;
}
public virtual bool HandleError(Exception exError)
{
System.Diagnostics.EventLog evt = new System.Diagnostics.EventLog("Application", ".", "My Application");
evt.WriteEntry("Error at " + this.strUrl_m + ":\n" + exError.Message, System.Diagnostics.EventLogEntryType.Error);
return this.bHandled_m;
}
public virtual void ProvideFault(Exception exError,
System.ServiceModel.Channels.MessageVersion version,
ref System.ServiceModel.Channels.Message msg)
{
}
private string strUrl_m;
public string Url
{
get
{
return this.strUrl_m;
}
}
private bool bHandled_m;
public bool Handled
{
get
{
return this.bHandled_m;
}
}
}
public class ErrorBehaviorAttribute : Attribute, IServiceBehavior
{
private Type typeErrorHandler_m;
public ErrorBehaviorAttribute(Type typeErrorHandler)
{
this.typeErrorHandler_m = typeErrorHandler;
}
public ErrorBehaviorAttribute(Type typeErrorHandler, string strUrl, bool bHandled)
: this(typeErrorHandler)
{
this.strUrl_m = strUrl;
this.bHandled_m = bHandled;
}
public virtual void Validate(ServiceDescription description, ServiceHostBase serviceHostBase)
{
return;
}
public virtual void AddBindingParameters(ServiceDescription description, ServiceHostBase serviceHostBase, System.Collections.ObjectModel.Collection<ServiceEndpoint> endpoints, BindingParameterCollection parameters)
{
return;
}
protected virtual IErrorHandler CreateTypeHandler()
{
IErrorHandler typeErrorHandler;
try
{
typeErrorHandler = (IErrorHandler)Activator.CreateInstance(this.typeErrorHandler_m, this.strUrl_m, bHandled_m);
}
catch (MissingMethodException e)
{
throw new ArgumentException("The ErrorHandler type specified in the ErrorBehaviorAttribute constructor must have a public constructor with string parameter and bool parameter.", e);
}
catch (InvalidCastException e)
{
throw new ArgumentException("The ErrorHandler type specified in the ErrorBehaviorAttribute constructor must implement System.ServiceModel.Dispatcher.IErrorHandler.", e);
}
return typeErrorHandler;
}
public virtual void ApplyDispatchBehavior(ServiceDescription description, ServiceHostBase serviceHostBase)
{
IErrorHandler typeErrorHandler = this.CreateTypeHandler();
foreach (ChannelDispatcherBase channelDispatcherBase in serviceHostBase.ChannelDispatchers)
{
ChannelDispatcher channelDispatcher = channelDispatcherBase as ChannelDispatcher;
channelDispatcher.ErrorHandlers.Add(typeErrorHandler);
}
}
private string strUrl_m;
public string Url
{
get
{
return this.strUrl_m;
}
}
private bool bHandled_m;
public bool Handled
{
get
{
return this.bHandled_m;
}
}
}
:
[ServiceContract(Namespace = "http://example.come/test")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ErrorBehavior(typeof(ServiceErrorHandler),"ExceptonTest.svc",false)]
public class ExceptonTest
{
[OperationContract]
public void TestException()
{
throw new Exception("this is a test!");
}
}