當實現IErrorHandler接口並將其添加到WCF服務中的調度程序時應用程序我遇到了一些奇怪的行爲。 只有HandleError
方法被觸發,而不是ProvideFault
方法。在WCF服務應用程序或WCF服務庫中實現IErrorHandler
當在WCF服務中使用相同的代碼和配置時庫這兩種方法都會觸發代碼中的異常。
實施例:
public class ErrorHandler : IErrorHandler, IServiceBehavior
{
public void ProvideFault(Exception error, System.ServiceModel.Channels.MessageVersion version, ref System.ServiceModel.Channels.Message fault)
{
// Provide the fault
}
public bool HandleError(Exception error)
{
// If handled return true, otherwise false
}
// Validate
// AddBindingParameters
// ApplyDispatchBehavior
}
public class ErrorHandlerBehavior : BehaviorExtensionElement
{
public override Type BehaviorType
{
get { return typeof(ErrorHandler); }
}
protected override object CreateBehavior()
{
return new ErrorHandler();
}
}
而且在Web.config:
<extensions>
<behaviorExtensions>
<add name="ErrorLogging" type="Service.ErrorHandlerBehavior, Service, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</behaviorExtensions>
</extensions>
...
<serviceBehaviors>
<behavior>
<ErrorLogging />
</behavior>
</serviceBehaviors>
的代碼和配置是相同的(雖然配置在Web.config中在服務應用程序,我應用。服務庫中的.config)。
通過插入斷點在ProvideFault
和HandleError
我可以看到ProvideFault
只是援引了服務圖書館 - 怎麼能這樣呢?我錯過了什麼嗎?
編輯
看來,當服務操作的調用簽名ProvideFault
不調用具有其他然後返回類型的基本類型,如:
public IEnumerable<MyType> GetMyType(string s1)
{
throw new Exception("Testing...");
}
...將不會觸發ProvideFault
。
但是:
public bool DoStuff(string s1, string s2)
{
throw new Exception("Testing...");
}
...沒有觸發ProvideFault
。