c# - Catch base exception class but not its derived generic variants -


i don't catch generic faultexception in client.

my service contract:

[servicecontract] public interface iempresawebservice {     [operationcontract]     [faultcontract(typeof(validationexceptiondto))]     empleadoempresadto login(string username, string password); } 

my service

[servicebehavior(instancecontextmode = instancecontextmode.persession)]  public class empresawebservice : iempresawebservice {     private readonly iunitofwork _unitofwork;      public empresawebservice(iunitofwork unitofwork) {         this._unitofwork = unitofwork;         //seedingbootsrapper.registerseedings();         //var initializer = new customdatainitializer<psicotecnicoscontext>(new string[] { "*" });         //database.setinitializer<psicotecnicoscontext>(initializer);     }      public empleadoempresadto login(string username, string password) {         var empleadoempresaservice = this._unitofwork.getservice<empleadoempresaservice>();         empleadoempresadto empleadoempresadto = new empleadoempresadto();         try {             var empleado = empleadoempresaservice.validarlogin(username, password);             mapper.createmap<empleadoempresa, empleadoempresadto>();             empleadoempresadto = mapper.map<empleadoempresa, empleadoempresadto>(empleado);         }         catch (validationexception ex) {             throw new faultexception<validationexceptiondto>(new validationexceptiondto(ex.errors));         }         return empleadoempresadto;     } } 

the class try serialize

[datacontract] public class validationexceptiondto {     [datamember]     private dictionary<string, string> _errors = new dictionary<string, string>();      public validationexceptiondto(ilist<validationfailure> errors) {         foreach (var validation in errors) {             this._errors.add(validation.errormessage, validation.propertyname);         }     }     [datamember]     public dictionary<string,string> errors {         {             if (this._errors != null) {                 return this._errors;             }             else {                 return new dictionary<string,string>();             }         }     } } 

and client:

public class cuentascontroller : controller {      // get: /candidatos/cuentas/     public actionresult index()     {         return view();     }      public actionresult login() {         return view();     }      [httppost]     public actionresult login(string username, string password, bool recordarme) {         try {             empresaserviceproxy.empresawebserviceclient client = new empresaserviceproxy.empresawebserviceclient();             empleadoempresadto empleadoempresa = client.login(username, password);              var autenticationprovider = new authenticationprovider(this.httpcontext);             autenticationprovider.loginas(username, recordarme);         }         catch (faultexception<validationexceptiondto> ex) {          }         catch(faultexception ex){             //only cath here         }         if (modelstate.isvalid)  {             return redirecttoaction("prueba");         }         return view();     }      [authorize]     public string prueba() {         return "esta logeado";     }  } 

}

standard c# exception handling code won't allow it, have catch faultexceptions , filter them using gettype method , strict type comparison:

   catch(faultexception ex)    {         if (ex.gettype().equals(typeof(faultexception))) // strict comparison of type         {             // handle faultexception exceptions, not descendants         }         else             throw;     } 

just not forget throw(or not throw depending on requirements) exception (thanks @dstanley)


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

django - CSRF verification failed. Request aborted. CSRF cookie not set -