Exception is a .NET mechanism used to communicate problems encountered during the execution of a program. Various .NET languages allows us to throw, catch/handle or possibly ignore exceptions so that it can be further propagated up to the call stack.
int result = 0;
try
{
result = 100 / x; //this line will generate divide by zero exception
//Some code here…
}
catch (DivideByZeroException dbzex)
{
Console.WriteLine(dbzex.ToString());
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
While WCF Faults refer to SOAP fault mechanism that transfers errors or fault condition from service to consumer according to SOAP Specifications. If some exception occurs at WCF Service level (which is basically a CLR exception), throwing it as is to client is illogical because client of this service is not necessarily a .NET application. It might be a Java client or something else that might not be able to understand CLR exception. Another important reason for not throwing CLR exceptions to client is, it contains internal details of our service code and off course we don’t want to expose those internal details as is to client.
In Windows Communication Foundation, we handle CLR exceptions and return error details to client using Fault Contract. A fault contract in WCF contains details of possible exceptions that might occur in a service code. Another WCF Tutorial with code sample, explains in detail about WCF Fault Contract here.
Following code samples will definitely help to understand WCF Faults.
public interface IMyService1
{
[OperationContract]
[FaultContract(typeof(MyWCFFault))]
int MyOperation1();
}
[DataContract]
public class MyWCFFault
{
[DataMember]
public string ErrorDetails { get; set; }
}
Implementation code for WCF Service is:
{
Try{
//Do something……
}
catch()
{
MyWCFFault ex = new MyWCFFault();
ex.ErrorDetails = “Specific error details here.“;
throw new FaultException(ex,“Reason: Testing…..“);
}
}
Other WCF and related Articles:
Top 10 Interview Questions and Answers Series:
- Top 10 WCF Interview Questions
- Comprehensive Series of WCF Interview Questions
- Top 10 HTML5 Interview Questions
- Top 10 ASP.NET Interview Questions
- Comprehensive Series of ASP.NET Interview Questions
- Top 10 ASP.NET MVC Interview Questions
- Top 10 ASP.NET Web API Interview Questions
- Top 10 ASP.NET AJAX Interview Questions
It is a stateless, system individual, XML based typical compact technique that uses HTTP as its transport technique and can be used for developing assigned complex handling environment.
Thanks for all the information,it was very helpful and i really like that you are providing information on .net training ,being enrolled in .net freshers training with projects live training http://www.wiziq.com/course/57-fresher-training-projects i was looking for such .net fresher training to assist me and your information helped me a lot.Really like that you are providing such information . Thanks.