Using Enterpise Library 6 Exception Handling Application Block with WCF? -
i have these 3 packages loaded wcf service project:
enterpriselibrary.common version 6.0.1304.0
enterpriselibrary.exceptionhandling 6.0.1304.0
enterpriselibrary.exceptionhandling.wcf version 6.0.1304.0
here service interface , mytestfault datacontract:
[servicecontract] public interface irepairservice { [operationcontract] [faultcontract(typeof(mytestfault))] string saverepaircode(string failurecode, string description); } [datacontract] public class mytestfault { #region member fields private string _message = "an unexpected error occured while executing service method."; #endregion #region properties [datamember] public string message { { return _message; } set { _message = value; } } #endregion #region constructor(s) public mytestfault() { } #endregion }
here implementation of service:
[exceptionshielding("testpolicy")] public class repairservice : irepairservice { #region private members private wimdal wimdal; exceptionmanager exmanager; #endregion #region constructor(s) public repairservice() { wimdal = new wimdal(); var testpolicy = new list<exceptionpolicyentry> { { new exceptionpolicyentry( typeof(sqlexception), posthandlingaction.thrownewexception, new iexceptionhandler[] { new faultcontractexceptionhandler( typeof(mytestfault), "sqlexception occurred.", new namevaluecollection(){ { "message", "message" }}) }) }, { new exceptionpolicyentry( typeof(exception), posthandlingaction.thrownewexception, new iexceptionhandler[] { new faultcontractexceptionhandler( typeof(mytestfault), "exception occurred.", new namevaluecollection(){ { "message", "message" }}) }) } }; var policies = new list<exceptionpolicydefinition>(); policies.add(new exceptionpolicydefinition( "testpolicy", testpolicy)); exmanager = new exceptionmanager(policies); } #endregion /// <summary> /// insert new fail code description rpcode. /// duplicate primary key throw sqlexception should processed ehab /// </summary> public string saverepaircode(string failurecode, string description) { using (transactionscope txscope = new transactionscope()) { wimsqlcommand sc = new wimsqlcommand() { storedprocedure = "repair.rpcode_insert" }; sc.parameters.add(new sqlparameter("@failurecode", failurecode)); sc.parameters.add(new sqlparameter("@desc", description)); exmanager.process(() => wimdal.execute_nonquerynoreturn(sc), "testpolicy"); txscope.complete(); return "<save_repair_code></save_repair_code>"; } } }
now, have testclient console application part of same project has reference project , service reference. there call saverepaircode() method , try catch specific fault exception so:
servicereference1.repairserviceclient r = new servicereference1.repairserviceclient(); console.writeline("enter new repair code:"); string repaircode = console.readline(); console.writeline("enter description:"); string description = console.readline(); try { r.saverepaircode(repaircode, description); } catch (faultexception<mytestfault> ex) { //do throw; } catch (faultexception fex) { //do throw; }
finally, run console app , try save duplicate repair code. know through debugging causes sqlexception occur. after step on sqlexception, see "faultcontractwrapperexception unhandled user code" exception , has message specified in policy of "sqlexception occurred.". when step on client , see error:
communicationexception unhandled
server did not provide meaningful reply; might caused contract mismatch, premature session shutdown or internal server error.
ps - enterprise library 6 wcf , made no manual changes web.config... , yes, includeexceptiondetailinfaults set false.
what missing here? in advance.
update
looks missing 1 line of code after instantiating new exceptionmanager.
exceptionpolicy.setexceptionmanager(exmanager);
nice see 1 line of code not in enterprise library 6 - april 2013.chm in "developer's guide microsoft enterprise library-preview.pdf" on page 90 of 269. after including 1 line proper faultexception catch on client.
with being said, still can't other mytestfault properties on client. example, if add public string storedprocedurename mytestfault , map sqlexception's "procedure" property, see null on client. change in policy add mapping so:
new namevaluecollection(){ { "message", "{message}" }, { "storedprocedurename", "{procedure}" } }
it turns out line culprit.
exmanager.process(() => wimdal.execute_nonquerynoreturn(sc), "testpolicy");
insead of using exceptionmanager's process method, execute command expect potential exception so.
wimdal.execute_nonquerynoreturn(sc);
this not follow "developer's guide microsoft enterprise library-preview.pdf" says guess documentation still work in progress. hope helps else.
Comments
Post a Comment