简体   繁体   English

WCF IParameterInspector + IErrorHandler

[英]WCF IParameterInspector + IErrorHandler

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? 如果我同时将IParameterInspectorIErrorHandler附加到服务,是否可以确保在调用IErrorHandler.HandleError()的同一线程上IParameterInspector.BeforeCall()

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() . 我之所以需IParameterInspector.AfterCall() ,是因为在发生错误的情况下,永远不会调用IParameterInspector.AfterCall()而且也无法(据我所知)我可以在BeforeCall()创建关联状态对象。 So I hope to overcome this by having a ThreadStatic field in my implementation of the interfaces :( 因此,我希望通过在我的接口实现中使用ThreadStatic字段来克服这一问题:(

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. 实例上下文扩展可以添加在BeforeCall方法中。 The instance context extension can then be retrieved in AfterCall method and used. 然后可以在AfterCall方法中检索实例上下文扩展并使用。 Any operation specific data can be put in this extension object instead of introducing thread affinity. 可以将任何特定于操作的数据放在此扩展对象中,而不用引入线程亲缘关系。

You could have your class that implements IErrorHandler also implement IParameterInspector. 您可以让实现IErrorHandler的类也实现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. 然后可以从HandleError方法中访问它们。 I'm currently using this for logging input parameters and the raw message whenever an unhandled exception occurs. 每当发生未处理的异常时,我目前都在使用它来记录输入参数和原始消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM