Je voudrais inclure une exception de définition d'utilisateur dans FaultContract de WCF. Dans mon application WCF, j'aimerais encapsuler une instance d'exception/UserDefine Exception Instance dans FaultContract. Veuillez trouver ci-dessous UserDefine Exception.Comment inclure une exception réelle ou une exception personnalisée dans FaultContract de WCF
public class UserExceptions : Exception
{
public string customMessage { get; set; }
public string Result { get; set; }
public UserExceptions(Exception ex):base(ex.Message,ex.InnerException)
{
}
}
public class RecordNotFoundException : UserExceptions
{
public RecordNotFoundException(Exception ex): base(ex)
{
}
}
public class StoreProcNotFoundException : UserExceptions
{
public string innerExp { get; set; }
public StoreProcNotFoundException(Exception ex,string innerExp)
: base(ex)
{
this.innerExp = innerExp;
}
}
[DataContract]
public class ExceptionFault
{
[DataMember]
public UserExceptions Exception { get; set; }
public ExceptionFault(UserExceptions ex)
{
this.Exception = ex;
}
}
Et je suis en service Exception jet comme ci-dessous
try
{
//Some Code
//Coding Section
throw new RecordNotFoundException(new Exception("Record Not Found"));
//Coding Section
}
catch (RecordNotFoundException rex)
{
ExceptionFault ef = new ExceptionFault(rex);
throw new FaultException<ExceptionFault>(ef,new FaultReason(rex.Message));
}
catch (Exception ex)
{
throw new FaultException<ExceptionFault>(new ExceptionFault((UserExceptions)ex),new FaultReason(ex.Message));
}
capture bloc try CustomException (RecordNotFoundException), mais il n'est pas en mesure d'envoyer cette exception au client.
Merci Glen pour votre réponse rapide, mais Je voudrais inclure l'exception originale dans mon FaultContract (MathFault dans votre exemple), afin que le client puisse obtenir une exception réelle et d'autres données personnalisées comme Operation and Problem. – Sourabh
En bref, je voudrais sérialiser l'exception réelle et l'envoyer au client par fil. – Sourabh
@Sourabh Ah je vois. Avec un service SOAP, vous pouvez uniquement transmettre des types personnalisés dont le client a connaissance via un contrat DataContract. L'ajout de l'attribut KnownType pourrait aider à cela. –