2016-04-07 2 views
0

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.

Répondre

0

Vous devez ajouter le FaultContract attribut à vos OperationContract méthodes afin que le client SOAP sait attendre le type d'exception

[OperationContract] 
[FaultContract(typeof(MathFault))] 
int Divide(int n1, int n2); 

Votre bloc catch doit attraper FaultException<T>

catch (FaultException<MathFault> e) 
{ 
    Console.WriteLine("FaultException<MathFault>: Math fault while doing " + e.Detail.operation + ". Problem: " + e.Detail.problemType); 
    client.Abort(); 
} 

Son mieux avoir un DataContract pour chaque type d'exception, plutôt que d'essayer d'envelopper tous dans un DataContract

[DataContract] 
public class MathFault 
{  
    private string operation; 
    private string problemType; 

    [DataMember] 
    public string Operation 
    { 
     get { return operation; } 
     set { operation = value; } 
    } 

    [DataMember]   
    public string ProblemType 
    { 
     get { return problemType; } 
     set { problemType = value; } 
    } 
} 

Si vous souhaitez inclure des implémentations de UserExceptions dans votre DataContract alors vous devrez peut-être utiliser l'attribut KnownType afin que le client SOAP est conscient des types:

[DataContract] 
[KnownType(typeof(RecordNotFoundException))] 
[KnownType(typeof(StoreProcNotFoundException))] 
public class ExceptionFault 
{ 
    [DataMember] 
    public UserExceptions Exception { get; set; } 

    public ExceptionFault(UserExceptions ex) 
    { 
     this.Exception = ex; 
    } 
} 
+0

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

+0

En bref, je voudrais sérialiser l'exception réelle et l'envoyer au client par fil. – Sourabh

+0

@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. –