Rethrowing strongly-typed WCF FaultException in a middle-layer service -
i have simple 3-tier project. there dal (data access layer) wcf service interacting data store; bll (business logic layer) wcf service intermediary between dal service , client ui, , windows forms client layer.
i declare datacontract in dal service act fault contract:
[datacontract] public class validationfault { [datamember] public string message { get; set; } [datamember] public string propertyname { get; set; } ... }
my dal service has operation decorated faultcontract attribute:
[operationcontract] [faultcontract(typeof(validationfault))] patient createpatient(patient patient);
the implementation of createpatient throws strongly-typed faultexception this:
throw new faultexception<validationfault>(new validationfault("patient last name cannot empty.", "lastname"));
my bll service acts client dal service, , service ui layer. in bll service call createpatient method of dal service, , if catch faultexception fault, want re-throw handled client. relevant bll code looks this:
... catch (faultexception<validationfault>) { throw; }
i can inspect exception in bll , can confirm strongly-typed faultexception , detail section passed dal intact. bll method attempting rethrow exception decorated same [faultcontract] attribute dal method above.
the ui client, client of bll service, tries handle exception , log/display appropriate information. trouble is, when fault reaches client, no longer strongly-typed faultexception, instead general faultexception null detail section.
i did find if instead of rethrowing fault in bll method, instead recreate same parameters this:
catch (faultexception<validationfault> valex) { throw new faultexception<validationfault>(new validationfault(valex.detail.message, valex.detail.propertyname)); }
then reaches client strongly-typed faultexception expected.
my question is: why need recreate exception in bll service? why can't pass through strongly-typed faultexception thrown dal service? got code work understand going on. imagine it's that's staring me in face can't life of me figure out.
indeed answer staring me in face along, in case helps down road: problem action property of faultcontract. when decorate operation faultcontract attribute, unless specify action explicitly, wcf automatically generates you. action includes faultexception namespace, service , method names of operation generated fault. in case, service , method name in bll layer different service , method name of dal method generated exception, although both dal , bll layer operations decorated identical attribute:
[faultcontract(typeof(validationfault))]
... action of faultcontract different. when trying rethrow dal faultexception in bll service, faultcontract did not match 1 specified on bll operation, , wcf repacking exception general faultexception instead of strongly-typed one.
my options in case either re-create faultexception in bll service, creates proper action, when reaches client, appears coming bll service; fact originates in dal lost unless specify explicitly, or else add action property faultcontract attribute on bll operation, , specify action of fault generated dal. better idea , better practice, whole other topic, have understanding of going on, after.
Comments
Post a Comment