Rather than throwing general .NET exceptions, you throw SOAP fault messages in a WCF service. You can create your own class that represents the fault and use properties of this class to store detailed information about the fault. You then add this class
to the service’s data contract. Finally, you use the FaultContractAttribute to identify which operations can throw which SOAP faults.
You can also use the FaultException class with a string or any other built-in type as type argument:
throw new FaultException<string>("Error message...");
Please 'Mark as Answer' if this post helped you.
Marked as answer by gopikrsna on Apr 07, 2012 10:31 AM
the basic rule is try handling exceptions in your service code, then encapsulate the exception/error with a custom FaultException(defined through FaultContract on your service/operation). So the client-side will receive your customized FaultException instead
of raw exception/errors.
gopikrsna
Member
190 Points
234 Posts
Help, How to handle Exception in WCF
Apr 07, 2012 05:07 AM|LINK
We are exposing our wcf service to xxx client.
We are sending huge data when they request our service.
public string GetData(int RequestID) { try { ... .....some code retuns string; } catch { throw; } }Suppose while sending string from our side is failed due to some reason.
I want to send client , what is the error occured.
What is the best way to handle.
mm10
Contributor
6395 Points
1182 Posts
Re: Help, How to handle Exception in WCF
Apr 07, 2012 09:25 AM|LINK
Rather than throwing general .NET exceptions, you throw SOAP fault messages in a WCF service. You can create your own class that represents the fault and use properties of this class to store detailed information about the fault. You then add this class to the service’s data contract. Finally, you use the FaultContractAttribute to identify which operations can throw which SOAP faults.
Example:
[ServiceContract()]
public interface IService
{
[OperationContract]
[FaultContract(typeof(MyFaultException))]
string GetData(int RequestID)
}
[DataContract]
public class MyFaultException
{
private string _reason;
[DataMember]
public string Reason
{
get { return _reason; }
set { _reason = value; }
}
}
public string GetData(int RequestID)
{
try
{
...
.....some code
retuns string;
}
catch(Exception ex)
{
MyFaultException theFault = new MyFaultException();
theFault.Reason = "Some Error " + ex.Message.ToString();
throw new FaultException<MyFaultException>(theFault);
}
}
You can also use the FaultException class with a string or any other built-in type as type argument:
throw new FaultException<string>("Error message...");
gopikrsna
Member
190 Points
234 Posts
Re: Help, How to handle Exception in WCF
Apr 07, 2012 10:32 AM|LINK
Thanks
Steven Cheng...
Contributor
4187 Points
547 Posts
Microsoft
Moderator
Re: Help, How to handle Exception in WCF
Apr 09, 2012 02:55 AM|LINK
Hi gopikrsna,
here are some further references on WCF Exception handling and FaultContract usage:
#Exception Handling
http://msdn.microsoft.com/en-us/library/ff650899.aspx
#Exception Handling in WCF
http://www.codeproject.com/Articles/115006/Exception-Handling-in-WCF
#WCF Error Handling and Fault Conversion
http://www.codeproject.com/Articles/26320/WCF-Error-Handling-and-Fault-Conversion
the basic rule is try handling exceptions in your service code, then encapsulate the exception/error with a custom FaultException(defined through FaultContract on your service/operation). So the client-side will receive your customized FaultException instead of raw exception/errors.
Feedback to us
Microsoft One Code Framework