locked
WCF and exception shielding RRS feed

  • Question

  • Hi Guys,

    I want to shield exception that is gererated by my WCF service, so I have enabled exception sheilding, but I am doing this a little bit differently, I want your opinion on if this approach is sound or I may get into problems in the future.

    so here we go...


    First Approach....


    My confiuration is like this for WCF Exception Shielding has 2 exceptions types mapped
    1)Fault Exception - in this case I rethrow the exception, I need this setting so that any custom fault exception that i throw like an EntityNotFoundFault will not be shielded bu sent to the client

    2) Exception class : this type appears after the FaultException in the configuration , and is configred to be shielded, what i want here is any exception that gets genrented would be shielded and I have also configured logging for this type.
    My ultimate goal would be to shield any exception other then the faults that I throw, so I would not wont to configure the block for every exception type to be shielded but to add the Excption class type to shield



    My method look like this..


    public string GetData()
    {

        //code goes here
        //......
        //....
        if(notValid)
        {

           throw new FaultException<NotValidFault>(new NotValidFault(....));
        }

    }



    Now in this apporach, i dont wrap my methods in a try-catch block
    , which looks kind of not a good practice, but I am expecting this to be handled by the exception shielding


    Second approach.....

    I configure my exception block to Shield an exception called ServiceException and then this what I do in my code
    is this.

    public string GetData()
    {

      try
     {
        //...do some thing
        //....

        if(notValid)
        {

           throw new FaultException<NotValidFault>(new NotValidFault(....));
        }

     }
    catch(Exception)
    {

       if(!(Exception is FaultException)) //then i dont want the exception block to process this
       { 
          throw new ServiceException(...);//shield this exception

       }

    }

    }
     

    OK now this code looks like I am doing something unneccessary, like wrapping the exception and rethrowing, when I know the exception block would take care of this.

    PLease let me know your opinion or if there is a better way to do this, please let me know.


    Regards,
    Nairooz NIlafdeen
    Saturday, January 9, 2010 3:47 PM

Answers

  • Actually WCF exception shielding block should throw Faults, not your code. Define in your code custom exceptions, like NotValidException, and in WCF exception shielding config, map these custom exceptions to appropriate Faults: NotValidException will be transformed into NotValidFault. This will make your design more flexible - you will be able to throw exceptions in any layer of your service without referencing WCF staff and FaultException in particular. In other words, in your business logic layer you need to define custom business logic exceptions, then map this exceptions to faults in WCF EntLib config. In this case your BL layer will not convey any references to WCF libraries and will be much more orthogonal.
    Vitaliy Liptchinsky http://dotnetframeworkplanet.blogspot.com/
    Monday, January 11, 2010 9:26 AM