WCF Fault Exception not being propagated to client -
i have wcf service defines faultcontract:
[operationcontract] [faultcontract(typeof(invalidoperationexception))] [faultcontract(typeof(notsupportedexception))] [faultcontract(typeof(exception))] getsysidsresponse getsysids(string id);
the wcf service catches exceptional case (null pointer exception) , throws faultexception:
try { .... } } catch (invalidoperationexception ex) { // specified directoryentry not container throw new faultexception<invalidoperationexception>(ex); } catch (notsupportedexception ex) { // search not supported provider being used throw new faultexception<notsupportedexception>(ex); } catch (exception ex) { throw new faultexception<exception>(ex); }
it throws last one. thing never gets client. first o9f all, "fault contracts published part of service metadata." not see in client metadata after add service reference. here client code. never hits catch block catch (faultexception e) says faultexception uncaught user. catch communicationexception. don't know doing wrong?
try { response = client.getsysids("sgentil"); } catch (faultexception<exception> e) { console.writeline("faultexception<exception>: " + e.detail); client.abort(); return; } catch (faultexception e) { console.writeline("faultexception: " + e.message); client.abort(); return; } catch (communicationexception ex) { console.writeline("communicationsexception"); client.abort(); return; }
** tried approach of first answer , defined 2 exceptions:
[datacontract] public class systemfault { [datamember] public string systemoperation { get; set; } [datamember] public string systemreason { get; set; } [datamember] public string systemmessage { get; set; } } [datacontract] public class databasefault { [datamember] public string dboperation { get; set; } [datamember] public string dbreason { get; set; } [datamember] public string dbmessage { get; set; } }
i applied it:
[faultcontract(typeof(systemfault))] getsysidsresponse getsysids(string id);
then threw it:
catch (exception ex) { systemfault sf = new systemfault { systemoperation = "getsystemids", systemmessage = "exception while obtaining ad user properties", systemreason = ex.message }; throw new faultexception<systemfault>(sf); }
the client see systemfault type , has metadata:
catch(faultexception<systemfault> sf) { console.writeline("systemfault {0}: {1}\n{2}", sf.detail.systemoperation, sf.detail.systemmessage, sf.detail.systemreason); }
yet still execution stops in server @ line:
throw new faultexception<systemfault>(sf);
it says: faultexception'1 not handled user code
now what?
the problem specifying faultcontract
of type xxxexception
. not work think, must create custom faultcontract
of own, example:
[datacontract] public class initializationfault { public initializationfault(exception exc, string msg) { exception = exc; message = msg; } [datamember] public exception exception { get; set; } [datamember] public string message { get; set; } }
then servicecontract
becomes:
[operationcontract] [faultcontract(typeof(initializationfault))] //..more getsysidsresponse getsysids(string id);
and client side code becomes:
try { response = client.getsysids("sgentil"); } catch (faultexception<initializationfault> e) { console.writeline("faultexception<initializationfault>: " + e.detail); //more }
Comments
Post a Comment