【发布时间】: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<ValidationExceptionDTO>异常将被吞没(它不会被下一个catch块“再次”捕获)。 -
我想捕获 FaultException
但总是捕获 FaultException。 -
@MartinSanchez 不要发布您在问题中的所有内容 - 代码墙很难阅读,仅发布 relevant compact code pieces。此外,如果难以使用复杂的措辞来描述所需的行为,则使用案例场景来描述它:比如如果它抛出
FaultException,它在try(FaultException)中处理,如果它抛出FaultException<ValidationExceptionDTO>,那么它在两者中都被处理try(FaultException<ValidationExceptionDTO>)和try(FaultException),或者任何你想要的。
标签: c# asp.net .net asp.net-mvc wcf