一直在IErrorHandler上阅读,并想要去配置路线。 因此,我已阅读以下内容以尝试实施它。IErrorHandler似乎没有处理我在WCF中的错误..任何想法?
Keyvan Nayyeri blog about the type defintion
这基本上只是在MSDN例如包裹在继承IErrorHandler和IServiceBehaviour一类...然后这个被包裹在从BehaviourExtensionElement继承Extension元素据称允许我将该元素添加到web.config中。我错过了什么?
我得到它来编译和从我已经修复的各种错误,它似乎像WCF实际上加载错误处理程序。我的问题是,我正在抛出的异常处理错误处理程序并没有得到传递给它的异常。
我的服务实现只是调用另一个引发ArgumentOutOfRangeException的类的方法 - 但是这个异常永远不会被处理程序处理。
我的web.config
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="basic">
<security mode="None" />
</binding>
</basicHttpBinding>
</bindings>
<extensions>
<behaviorExtensions>
<add name="customHttpBehavior"
type="ErrorHandlerTest.ErrorHandlerElement, ErrorHandlerTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</behaviorExtensions>
</extensions>
<behaviors>
<serviceBehaviors>
<behavior name="exceptionHandlerBehaviour">
<serviceMetadata httpGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="true"/>
<customHttpBehavior />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="exceptionHandlerBehaviour" name="ErrorHandlerTest.Service1">
<endpoint binding="basicHttpBinding" bindingConfiguration="basic" contract="ErrorHandlerTest.IService1" />
</service>
</services>
服务合同
[ServiceContract]
public interface IService1
{
[OperationContract]
[FaultContract(typeof(GeneralInternalFault))]
string GetData(int value);
}
的的ErrorHandler类
public class ErrorHandler : IErrorHandler , IServiceBehavior
{
public bool HandleError(Exception error)
{
Console.WriteLine("caught exception {0}:",error.Message);
return true;
}
public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
{
if (fault!=null)
{
if (error is ArgumentOutOfRangeException)
{
var fe = new FaultException<GeneralInternalFault>(new GeneralInternalFault("general internal fault."));
MessageFault mf = fe.CreateMessageFault();
fault = Message.CreateMessage(version, mf, fe.Action);
}
else
{
var fe = new FaultException<GeneralInternalFault>(new GeneralInternalFault(" the other general internal fault."));
MessageFault mf = fe.CreateMessageFault();
fault = Message.CreateMessage(version, mf, fe.Action);
}
}
}
public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, System.Collections.ObjectModel.Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
{
}
public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
IErrorHandler errorHandler = new ErrorHandler();
foreach (ChannelDispatcherBase channelDispatcherBase in serviceHostBase.ChannelDispatchers)
{
ChannelDispatcher channelDispatcher = channelDispatcherBase as ChannelDispatcher;
if (channelDispatcher != null)
{
channelDispatcher.ErrorHandlers.Add(errorHandler);
}
}
}
public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
}
}
并且行为扩展元素
public class ErrorHandlerElement : BehaviorExtensionElement
{
protected override object CreateBehavior()
{
return new ErrorHandler();
}
public override Type BehaviorType
{
get { return typeof(ErrorHandler); }
}
}
非常感谢你,对不起,花了这么长时间来回答我在工作中的设置是非常困难的。虽然这在我家里的设置很好。 – 2010-07-09 17:49:29
感谢您的完整示例。这工作就像一个魅力! – Stewie 2012-03-11 04:33:52
为我工作,即使Visual Studio抱怨“元素行为有无效的子元素错误处理程序” - 我只是忽略,并在运行时它的工作。 – BornToCode 2015-08-16 13:23:45