2

If I have both IParameterInspector and IErrorHandler attached to a service can I be sure IErrorHandler.HandleError() will be called on the same thread where IParameterInspector.BeforeCall() is called?

I need this because in case of a fault thrown IParameterInspector.AfterCall() is never called and there is no way (as far as I know) I can get correlation state object created in BeforeCall(). So I hope to overcome this by having a ThreadStatic field in my implementation of the interfaces :(

UserControl
  • 14,766
  • 20
  • 100
  • 187

2 Answers2

5

You may want to utilize Instance Context Extensions.

InstanceContextExtension: IExtension<InstanceContext>   
OperationContext.Current.InstanceContext.Extensions.Find<InstanceContextExtension>()

The instance context extension can be added in BeforeCall method. The instance context extension can then be retrieved in AfterCall method and used. Any operation specific data can be put in this extension object instead of introducing thread affinity.

jags
  • 2,022
  • 26
  • 34
  • I'm not sure OperationContext is accessible in IErrorHandler.HandleError(). Tried similar approach few days ago and got ObjectDisposedException :( – UserControl Oct 15 '12 at 10:14
  • Just a thought, would creating correlation state in `AfterReceiveRequest` method of `IDispatchMessageInspector` interface would be good idea? – jags Oct 15 '12 at 11:03
  • I've realized I can use ProvideFault() instead of HandleError() so your approach looks the best! +1 – UserControl Oct 15 '12 at 12:15
2

You could have your class that implements IErrorHandler also implement IParameterInspector. You could then store references to your correlation state and input parameters as class variables. Then they would be accessable from within the HandleError method. I'm currently using this for logging input parameters and the raw message whenever an unhandled exception occurs.

Jason Turan
  • 1,302
  • 12
  • 20
  • That's exactly what i'm doing. However, I'd like to be sure this will be thread safe. – UserControl Oct 15 '12 at 10:11
  • Is your service InstanceContextMode PerCall? If so I believe you would get a new instance of the service and the ErrorHandler for that service for each call. – Jason Turan Oct 17 '12 at 17:00