Create a WCF Application with Fault Contract

Step 1: Create a new WCF Service
 
Step 2: Add A Commom.Cs file in App_Code folder.
 
And write these code.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Configuration;  
  6. using System.Data;  
  7. using System.Data.SqlClient;  
  8. /// <summary>  
  9. /// Summary description for Common  
  10. /// </summary>  
  11. public class Common  
  12. {  
  13.     public static string str = ConfigurationManager.ConnectionStrings["conn"].ToString();  
  14.     SqlConnection conn = new SqlConnection(str);  
  15.     public Common()  
  16.     {  
  17.     }  
  18.     #region Check Login  
  19.     public bool Check_Login(string User_Name, string Password)  
  20.     {  
  21.         bool result = false;  
  22.         SqlCommand cmd = new SqlCommand("usp_check_login", conn);  
  23.         cmd.CommandTimeout = 0;  
  24.         cmd.CommandType = CommandType.StoredProcedure;  
  25.         cmd.Parameters.AddWithValue("@username", User_Name);  
  26.         cmd.Parameters.AddWithValue("@pass", Password);  
  27.         SqlDataReader dr;  
  28.         conn.Open();  
  29.         dr = cmd.ExecuteReader();  
  30.         dr.Read();  
  31.         if (dr[0].ToString() == "True")  
  32.         {  
  33.             result = true;  
  34.         }  
  35.         else  
  36.         {  
  37.             result = false;  
  38.         }  
  39.         conn.Close();  
  40.         return result;  
  41.     }  
  42.     #endregion  
  43.     # region Register  
  44.     public string Register_Login(string Username, string User_Pass, string F_Name, string L_Name, string Address1, string mobile, string City)  
  45.     {  
  46.         string result = "Error";  
  47.         SqlCommand cmd = new SqlCommand("Usp_Register_User", conn);  
  48.         cmd.CommandType = CommandType.StoredProcedure;  
  49.         cmd.CommandTimeout = 0;  
  50.         cmd.Parameters.AddWithValue("@Username1", Username);  
  51.         cmd.Parameters.AddWithValue("@User_Pass", User_Pass);  
  52.         cmd.Parameters.AddWithValue("@F_Name", F_Name);  
  53.         cmd.Parameters.AddWithValue("@L_Name", L_Name);  
  54.         cmd.Parameters.AddWithValue("@Address1", Address1);  
  55.         cmd.Parameters.AddWithValue("@mobile", mobile);  
  56.         cmd.Parameters.AddWithValue("@City", City);  
  57.         conn.Open();  
  58.         SqlDataReader dr;  
  59.         dr = cmd.ExecuteReader();  
  60.         dr.Read();  
  61.         if (dr[0].ToString().Contains("Error"))  
  62.         {  
  63.             result = "Error !!!";  
  64.         }  
  65.         else  
  66.         {  
  67.             result = "Succsess";  
  68.         }  
  69.         return result;  
  70.     }  
  71.     #endregion  
  72. }  
Step 3: On IService.cs page write this code
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Runtime.Serialization;  
  5. using System.ServiceModel;  
  6. using System.ServiceModel.Web;  
  7. using System.Text;  
  8. // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService" in both code and config file together.  
  9. [ServiceContract]  
  10. public interface IService  
  11. {  
  12.     [OperationContract]  
  13.     string GetData(int value);  
  14.     [OperationContract]  
  15.     bool Check_Login(String U_Id, String U_Pass);  
  16.     [OperationContract]  
  17.     [FaultContract(typeof(string))]  
  18.     string Register_Login(string Username, string User_Pass, string F_Name, string L_Name, string Address1, string mobile, string City);  
  19. }  
  20. // Use a data contract as illustrated in the sample below to add composite types to service operations.  
  21. [DataContract()]  
  22. public class ServiceData  
  23. {  
  24.     [DataMember]  
  25.     public bool Result { getset; }  
  26.     [DataMember]  
  27.     public string ErrorMessage { getset; }  
  28.     [DataMember]  
  29.     public string ErrorDetails { getset; }  
  30. }  
Step 4: On Service.cs page wrote the below code.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Runtime.Serialization;  
  5. using System.ServiceModel;  
  6. using System.ServiceModel.Web;  
  7. using System.Text;  
  8. // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service" in code, svc and config file together.  
  9. public class Service : IService  
  10. {  
  11.     Common cm = new Common();  
  12.     public string GetData(int value)  
  13.     {  
  14.         return string.Format("You entered: {0}", value);  
  15.     }  
  16.     public bool Check_Login(String U_Id, String U_Pass)  
  17.     {  
  18.         bool result = false;  
  19.         result = cm.Check_Login(U_Id, U_Pass);  
  20.         return result;  
  21.     }  
  22.     public string Register_Login(string Username, string User_Pass, string F_Name, string L_Name, string Address1, string mobile, string City)  
  23.     {  
  24.         string result = "Error";  
  25.         ServiceData myServiceData = new ServiceData();  
  26.         try  
  27.         {  
  28.             result = cm.Register_Login(Username, User_Pass, F_Name, L_Name, Address1, mobile, City);  
  29.         }  
  30.         catch (Exception sqlEx)  
  31.         {  
  32.             //myServiceData.Result = true;  
  33.             //myServiceData.ErrorMessage = "Connection can not open this time either connection string is wrong or Sever is down. Try later";  
  34.             //myServiceData.ErrorDetails = sqlEx.ToString();  
  35.             string str = "Error in Service";  
  36.             throw new FaultException<string>(str, "Error In SQL Stored Procedure!!!");  
  37.         }  
  38.         return result;  
  39.     }  
  40. }  
Step 5: In web.config file..white the below code
  1. <system.serviceModel>  
  2.   <services>  
  3.     <service name="Service" behaviorConfiguration="ServiceBehavior">  
  4.       <!-- Service Endpoints -->  
  5.       <endpoint address="" binding="wsHttpBinding" contract="IService">  
  6.         <!--  
  7. Upon deployment, the following identity element should be removed or replaced to reflect the  
  8. identity under which the deployed service runs. If removed, WCF will infer an appropriate identity  
  9. automatically.  
  10. -->  
  11.         <identity>  
  12.           <dns value="localhost"/>  
  13.         </identity>  
  14.       </endpoint>  
  15.       <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>  
  16.     </service>  
  17.   </services>  
  18.   <behaviors>  
  19.     <serviceBehaviors>  
  20.       <behavior name="ServiceBehavior">  
  21.         <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->  
  22.         <serviceMetadata httpGetEnabled="true"/>  
  23.         <!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->  
  24.         <serviceDebug includeExceptionDetailInFaults="false"/>  
  25.       </behavior>  
  26.     </serviceBehaviors>  
  27.   </behaviors>  
  28. </system.serviceModel>  
Step 6: Add Connection string in config file.
  1. <connectionStrings>  
  2.   <add name="conn" connectionString="server=.\SQL;User Id=sa;Password=abc@123;Initial Catalog=test"/>  
  3. </connectionStrings>  
Step 7:  Run and save file for Client application that you can see in my next blog. For more please find the attachment.