locked
WCF FaultException Hadling with MVC3 RRS feed

  • Question

  • Hello

    I am working on an application which consists of 3 projects

    1) MVC project 2) WCF project 3) Data Access project

    Communication between projects as below

    MVC<-> WCF<->DataAcces.

    I am little bit confused in handling the exception from WCF.

    I am planing to use Fault Exceptions to through the message from WCF to client.

    Will it be a good idea to keep try{} catch{} block in the Data Accesss methods and throw the exception from there?

    OR will it be fine if I am having try{} catch{} block only within the WCF methods (operation contracts) only ?

    Can you someone direct me in right way.

    Thanks Vipin


    Vipin Unnikrishnan

    Wednesday, June 27, 2012 6:19 PM

Answers

  • Hi Vipin,

    If you are following tier based application development then you should handle fault and propagate fault into each layer.

    What i mean by that,

    when you are in DAC you will mainly get data specific exception. The you can trap this exception and send it to your business layer.

    Then you business layer transform this and send business specific fault send it back to your client. If your business layer or service layer is WCF, WCF can get DataAccess fault and converts it to generic FaultException<T> type and send it back to your MVC client. In this way your solution will be more complete. Say in future you want to replace your WCF layer with something else or directly use the DAC, then you already have the exception handling there. You dont need to worry about that. Secondly, you are isolating your exception message. Say a SQL Exception doesnt mean anything to your NON-IT clients, what they just want to do to know whether operation failed or success. But in your business layer if any SQL Exception happened you still want to let your administrators know about this.

    So if i were you i would handle fault in each layer and propage it up to WCF layer then to MVC layer and then to client.

    Hope that helps


    Tanvir Huda Application Architect/Consultant http://thetechnocrate.wordpress.com/

    Thursday, June 28, 2012 1:44 AM

All replies

  • On 6/27/2012 2:19 PM, Vipinunnikrishnan wrote:
    > Hello
    >
    > I am working on an application which consists of 3 projects
    >
    > 1) MVC project 2) WCF project 3) Data Access project
    >
    > Communication between projects as below
    >
    > MVC<-> WCF<->DataAcces.
    >
    > I am little bit confused in handling the exception from WCF.
    >
    > I am planing to use Fault Exceptions to through the message from WCF to
    > client.
    >
    > Will it be a good idea to keep try{} catch{} block in the Data Accesss
    > methods and throw the exception from there?
    >
    > OR will it be fine if I am having try{} catch{} block only within the
    > WCF methods (operation contracts) only ?
    >
    > Can you someone direct me in right way.
    >
     MVC -- Service Layer -- WCF -- DAL
     
    The Service Layer should be based on this concept of using a try/catch.
     
     
    You'll want to try/catches in the DAL.
     
    Wednesday, June 27, 2012 7:13 PM
  • Hi Vipin,

    If you are following tier based application development then you should handle fault and propagate fault into each layer.

    What i mean by that,

    when you are in DAC you will mainly get data specific exception. The you can trap this exception and send it to your business layer.

    Then you business layer transform this and send business specific fault send it back to your client. If your business layer or service layer is WCF, WCF can get DataAccess fault and converts it to generic FaultException<T> type and send it back to your MVC client. In this way your solution will be more complete. Say in future you want to replace your WCF layer with something else or directly use the DAC, then you already have the exception handling there. You dont need to worry about that. Secondly, you are isolating your exception message. Say a SQL Exception doesnt mean anything to your NON-IT clients, what they just want to do to know whether operation failed or success. But in your business layer if any SQL Exception happened you still want to let your administrators know about this.

    So if i were you i would handle fault in each layer and propage it up to WCF layer then to MVC layer and then to client.

    Hope that helps


    Tanvir Huda Application Architect/Consultant http://thetechnocrate.wordpress.com/

    Thursday, June 28, 2012 1:44 AM