【问题标题】:Catch base exception class but not its derived generic variants [closed]捕获基本异常类,但不捕获其派生的通用变体 [关闭]
【发布时间】:2014-09-09 17:25:26
【问题描述】:

我没有在我的客户端中捕获通用的 FaultException。

我的服务合同:

[ServiceContract]
public interface IEmpresaWebService
{
    [OperationContract]
    [FaultContract(typeof(ValidationExceptionDTO))]
    EmpleadoEmpresaDTO Login(string username, string password);
}

我的服务

[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;
    }
}

尝试序列化的类

[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 {
        get {
            if (this._errors != null) {
                return this._errors;
            }
            else {
                return new Dictionary<string,string>();
            }
        }
    }
}

还有我的客户:

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";
    }

}

}

【问题讨论】:

  • 我没有明白你的问题/问题是什么。您能否详细说明您正在尝试做的事情以及它在哪里出现问题?
  • 我尝试在客户端捕获异常,但只捕获 FaultException 而不是 FaultException
  • 不清楚正在发生什么以及你想要发生什么。使用代码,您拥有FaultException&lt;ValidationExceptionDTO&gt; 异常将被吞没(它不会被下一个catch 块“再次”捕获)。
  • 我想捕获 FaultException 但总是捕获 FaultException。
  • @MartinSanchez 不要发布您在问题中的所有内容 - 代码墙很难阅读,仅发布 relevant compact code pieces。此外,如果难以使用复杂的措辞来描述所需的行为,则使用案例场景来描述它:比如如果它抛出FaultException,它在try(FaultException) 中处理,如果它抛出FaultException&lt;ValidationExceptionDTO&gt;,那么它在两者中都被处理try(FaultException&lt;ValidationExceptionDTO&gt;)try(FaultException),或者任何你想要的。

标签: c# asp.net .net asp.net-mvc wcf


【解决方案1】:

标准 C# 异常处理代码不允许您这样做,因此您必须捕获所有 FaultExceptions 并使用 GetType 方法和严格的 Type comparison 过滤它们:

   catch(FaultException ex)
   {
        if (ex.GetType().Equals(typeof(FaultException))) // Strict comparison of type
        {
            // Handle only FaultException exceptions, not its descendants
        }
        else
            throw;
    }

只是不要忘记抛出(或根据您的要求不抛出)异常(感谢@DStanley)

【讨论】:

  • 不要忘记重新抛出未处理的异常,否则它们会被吞没。
  • @Stanley 谢谢,我的错。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-10-06
  • 2020-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-12
相关资源
最近更新 更多