c# - Exception Handling in WCF's Completed Event -
i wondering how catch exception in wcf's
completed event method , show user in messagebox
.
i call getproducttypeasync();
method fetches records database. want catch exception occurs here , send service_getproducttypecompleted
event should show exception message
user.
public list<producttype> getproducttype() { list<producttype> producttype = new list<producttype>(); try { using (sqlconnection con = new sqlconnection(_connectionstring)) { sqlcommand cmd = new sqlcommand("usp_get_producttype", con); cmd.commandtype = commandtype.storedprocedure; con.open(); using (sqldatareader reader = cmd.executereader()) { while (reader.read()) { producttype ptype = new producttype(convert.toint32(reader["pkproducttypeid"]), reader["name"].tostring()); producttype.add(ptype); } } } } catch(exception ex) { //catch exception , send service_getproducttypecompleted event method } return producttype; }
here service_getproducttypecompleted
event
void service_getproducttypecompleted(object sender, getproducttypecompletedeventargs e) { if (e.result.count != 0) { producttypes = e.result.tolist(); cboproducttype.datacontext = producttypes; } }
when send exception
service, client receive generic error message:
the server unable process request due internal error. more information error, either turn on includeexceptiondetailinfaults (either servicebehaviorattribute or configuration behavior) on server in order send exception information client, or turn on tracing per microsoft .net framework sdk documentation , inspect server trace logs."
if want catch specific exception on client side, should throw called faultexception
service. these soap faults , can recognized consumers. please note consumers service not .net consumer, throwing clr exception wouldn't recognized. that's reason why need throw
soap exception
.
e.g. how catch , throw fault exception
catch (faultexception<anytypethatcanbeserialized> ex) { throw; }
read this article more details on wcf faults
Comments
Post a Comment