c# - WCF Client Extensibility: IParemeterInspector.AfterCall and exception handling -
for wcf client, i'd able pre , post work every operation of given endpoint.
- the pre work make use of operation name , input arguments.
- the post work make use of (again) operation name , original input arguments outputs/return value or exception occurred.
given this, iparameterinspector
(with beforecall
, aftercall
) gives me almost need. problem that
- in case of exception,
aftercall
not called (see here).
to problem, can add iclientmessageinspector
since afterreceivereply
called in face of exception. gives me ability to
- determine if exception (fault) occurred or not.
- do post work in face of exception
question:
- is there way
iclientmessageinspector.afterreceivereply
(or create equivalent) exception represents thrown callers. , if so, there way mark exception handled such callers not exception? - or, there other extensibility mechanism or other approach should using meet goals?
if understand correctly want mute exceptions. it's possible , can done via replacing message in iclientmessageinspector.afterreceivereply
, "deserializing" new message in iclientmessageformatter.deserializereply
.
the code listed below returns default value result value when exception thrown.
message:
public sealed class fakemessage : message { #region fields private messageproperties properties; private messageheaders headers; #endregion #region constructors public fakemessage(messageversion version, string action) { this.headers = new messageheaders(version); this.headers.action = action; } #endregion #region message members public override messageheaders headers { { return headers; } } protected override void onwritebodycontents(xmldictionarywriter writer) { throw new notsupportedexception(); } public override messageproperties properties { { if (this.properties == null) { properties = new messageproperties(); } return properties; } } public override messageversion version { { return headers.messageversion; } } #endregion }
message formatter:
public sealed class fakemessageformatter : iclientmessageformatter { #region fields private iclientmessageformatter baseformatter; private object defaultreturnvalue; #endregion #region construcotrs public fakemessageformatter(iclientmessageformatter baseformatter, type returntype) { this.baseformatter = baseformatter; if (returntype.isvaluetype && returntype != typeof(void)) { this.defaultreturnvalue = activator.createinstance(returntype); } } #endregion #region iclientmessageformatter members public object deserializereply(message message, object[] parameters) { if (message fakemessage) { return defaultreturnvalue; } return baseformatter.deserializereply(message, parameters); } public message serializerequest(messageversion messageversion, object[] parameters) { return baseformatter.serializerequest(messageversion, parameters); } #endregion }
and message inspector:
public sealed class fakemessageinspector : iclientmessageinspector { #region iclientmessageinspector members public void afterreceivereply(ref message reply, object correlationstate) { if (reply.isfault) { reply = new fakemessage(reply.version, (string)correlationstate); } } public object beforesendrequest(ref message request, iclientchannel channel) { return request.headers.action + "response"; } #endregion }
Comments
Post a Comment