WCF Service errors/Exceptions can be passed to the Client(WCF Service Consumer) by usingFaultContract.
In ASP.NET It’s very simple just by adding the simple Try & Catch blocks. But when you come to WCF Service if any unexpected error occurred ) in service then error/Exception details can be passed to Client by using Fault Contract
Create a WCF Service application project & Implement the service with the following code.
The TestConnection()
method added with FaultContract
attribute in IService1
interface. It means that service errors should be passed to the client with the type of ServiceData
class.
ServiceData
class is DataContract
class. The Error & Success message details to be added to this data members.
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.ServiceModel.Web; using System.Text; namespace FaultContractSampleWCF { // NOTE: You can use the "Rename" command on the // "Refactor" menu to change the interface name // "IService" in both code and config file together. [ServiceContract] public interface IService1 { [OperationContract] [FaultContract(typeof(ServiceData))] ServiceData TestConnection(string strConnectionString); } // Use a data contract as illustrated in the sample // below to add composite types to service operations. [DataContract] public class ServiceData { [DataMember] public bool Result { get; set; } [DataMember] public string ErrorMessage { get; set; } [DataMember] public string ErrorDetails { get; set; } } }
Here I am implementing the IService1
interface. This interface
contain only one method TestConnection()
with one input parameter StrConnectionString
. For this input parameter SQL Server connection details should be passed from the client side
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.ServiceModel.Web; using System.Text; using System.Data.SqlClient; namespace FaultContractSampleWCF { // NOTE: You can use the "Rename" command on the "Refactor" // menu to change the class name "Service1" in code, svc and config file together. public class Service1 : IService1 { /// <summary> /// Implement the TestConnection method. /// </summary> /// <returns></returns> public ServiceData TestConnection(string StrConnectionString) { ServiceData myServiceData = new ServiceData(); try { SqlConnection con = new SqlConnection(StrConnectionString); con.Open(); myServiceData.Result = true; con.Close(); return myServiceData; } catch (SqlException sqlEx) { myServiceData.Result = true; myServiceData.ErrorMessage = "Connection can not open this " + "time either connection string is wrong or Sever is down. Try later"; myServiceData.ErrorDetails = sqlEx.ToString(); throw new FaultException<ServiceData>(myServiceData, sqlEx.ToString()); } catch (Exception ex) { myServiceData.Result = false; myServiceData.ErrorMessage = "unforeseen error occured. Please try later."; myServiceData.ErrorDetails = ex.ToString(); throw new FaultException<ServiceData>(myServiceData, ex.ToString()); } } } }
Create a new Console project and give the above service reference. Here we are creating a object to theService1Client
class and calling the TestConnection()
method by passing the connection string. If connection succeeds then it shows “Connection Succeeded” message. If unable to open connection then it moves to the catch
block and displays the appropriate error.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Client_FaultContractSampleWCF.MyServiceRef; using System.ServiceModel; namespace Client_FaultContractSampleWCF { class Program { static void Main(string[] args) { try { Service1Client objServiceClient = new Service1Client(); //Pass the connection string to the TestConnection Method. ServiceData objSeviceData = objServiceClient.TestConnection( @"integrated security=true;data source=localhost;initial catalog=master"); if (objSeviceData.Result == true) Console.WriteLine("Connection Succeeded"); Console.ReadLine(); } catch (FaultException<ServiceData> Fex) { Console.WriteLine("ErrorMessage::" + Fex.Detail.ErrorMessage + Environment.NewLine); Console.WriteLine("ErrorDetails::" + Environment.NewLine + Fex.Detail.ErrorDetails); Console.ReadLine(); } } } }