How to Call a Fault Contract WCF Service in ASP.NET

Step 1: Download and run the last application and save URL
 
Step 2: Create a new web application
 
Step 3: Open visual studio command prompt and type
 
svcutil.exe http://localhost:4864/MyFIrst_WCFApplication/Service.svc?wsdl
 
And press Enter.
 
This will give you two files, add this cs file to your Client application and add config file information to web.config file as given below.
  1. <system.serviceModel>  
  2.   <bindings>  
  3.     <wsHttpBinding>  
  4.       <binding name="WSHttpBinding_IService" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false">  
  5.         <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384"/>  
  6.         <reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="false"/>  
  7.         <security mode="Message">  
  8.           <transport clientCredentialType="Windows" proxyCredentialType="None" realm=""/>  
  9.           <message clientCredentialType="Windows" negotiateServiceCredential="true" algorithmSuite="Default"/>  
  10.         </security>  
  11.       </binding>  
  12.     </wsHttpBinding>  
  13.   </bindings>  
  14.   <client>  
  15.     <endpoint address="http://localhost:4864/MyFIrst_WCFApplication/Service.svc" binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IService" contract="IService" name="WSHttpBinding_IService">  
  16.       <identity>  
  17.         <dns value="localhost"/>  
  18.       </identity>  
  19.     </endpoint>  
  20.   </client>  
  21. </system.serviceModel>  
Step 4: Add a Login.Aspx page as given below..
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Login.aspx.cs" Inherits="Login" %>  
  2.    
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4. <html xmlns="http://www.w3.org/1999/xhtml">  
  5. <head id="Head1" runat="server">  
  6.     <title></title>  
  7.     <style type="text/css">  
  8.         .style1  
  9.         {  
  10.             width100%;  
  11.         }  
  12.     </style>  
  13. </head>  
  14. <body>  
  15.     <form id="form1" runat="server">  
  16.     <div>  
  17.         <div style="height: 150px">  
  18.         </div>  
  19.         <div>  
  20.             <table class="style1">  
  21.                 <tr>  
  22.                     <td>&nbsp;</td>  
  23.                     <td>&nbsp;</td>  
  24.                     <td>&nbsp;</td>  
  25.                     <td>&nbsp;</td>  
  26.                 </tr>  
  27.                 <tr>  
  28.                     <td>&nbsp;</td>  
  29.                     <td>User name</td>  
  30.                     <td>  
  31.                         <asp:TextBox ID="txt_Username" runat="server" Width="200px"></asp:TextBox>  
  32.                     </td>  
  33.                     <td>&nbsp;</td>  
  34.                 </tr>  
  35.                 <tr>  
  36.                     <td>&nbsp;</td>  
  37.                     <td>Password</td>  
  38.                     <td>  
  39.                         <asp:TextBox ID="txt_Password" runat="server" TextMode="Password" Width="200px"></asp:TextBox>  
  40.                     </td>  
  41.                     <td>&nbsp;</td>  
  42.                 </tr>  
  43.                 <tr>  
  44.                     <td>&nbsp;</td>  
  45.                     <td>&nbsp;</td>  
  46.                     <td>  
  47.                         <asp:Button ID="btn_Login" runat="server" OnClick="btn_Login_Click" Text="login" />  
  48.                     </td>  
  49.                     <td>&nbsp;</td>  
  50.                 </tr>  
  51.                 <tr>  
  52.                     <td>&nbsp;</td>  
  53.                     <td>&nbsp;</td>  
  54.                     <td>  
  55.                         <asp:Label ID="lbl_error" runat="server"></asp:Label>  
  56.                     </td>  
  57.                     <td>&nbsp;</td>  
  58.                 </tr>  
  59.             </table>  
  60.         </div>  
  61.     </div>  
  62.     </form>  
  63. </body>  
  64. </html>  
Step 5: Go to Login.aspx.cs page and write the below code.
  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.Linq;    
  4. using System.Web;    
  5. using System.Web.UI;    
  6. using System.Web.UI.WebControls;    
  7. using System.ServiceModel;    
  8. public partial class Login : System.Web.UI.Page    
  9. {    
  10.     ServiceClient wcfservice = new ServiceClient();    
  11.     protected void Page_Load(object sender, EventArgs e)    
  12.     {    
  13.     }    
  14.     protected void btn_Login_Click(object sender, EventArgs e)    
  15.     {    
  16.         bool result = wcfservice.Check_Login(txt_Username.Text, txt_Password.Text);    
  17.         lbl_error.Text = result.ToString();    
  18.     }    
  19. }  
Step 6: In same way, create for register page.
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Register.aspx.cs" Inherits="Register" %>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3. <head id="Head1" runat="server">  
  4.     <title></title>  
  5.     <style type="text/css">  
  6.         .style1  
  7.         {  
  8.             width100%;  
  9.         }  
  10.     </style>  
  11. </head>  
  12. <body>  
  13.     <form id="form1" runat="server">  
  14.     <div>  
  15.         <div style="height: 150px">  
  16.         </div>  
  17.         <div>  
  18.             <table class="style1">  
  19.                 <tr>  
  20.                     <td>&nbsp;</td>  
  21.                     <td>&nbsp;</td>  
  22.                     <td>&nbsp;</td>  
  23.                     <td>&nbsp;</td>  
  24.                 </tr>  
  25.                 <tr>  
  26.                     <td>&nbsp;</td>  
  27.                     <td>User name</td>  
  28.                     <td>  
  29.                         <asp:TextBox ID="txt_Username" runat="server" Width="200px"></asp:TextBox>  
  30.                         <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txt_Username"  
  31.                             ErrorMessage="Can't Be Blank" Font-Bold="True" ForeColor="#000066"></asp:RequiredFieldValidator>  
  32.                     </td>  
  33.                     <td>&nbsp;</td>  
  34.                 </tr>  
  35.                 <tr>  
  36.                     <td>&nbsp;</td>  
  37.                     <td>Password</td>  
  38.                     <td>  
  39.                         <asp:TextBox ID="txt_Password" runat="server" TextMode="Password" Width="200px"></asp:TextBox>  
  40.                         <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txt_Password"  
  41.                             ErrorMessage="Can't Be Blank" Font-Bold="True" ForeColor="#000066"></asp:RequiredFieldValidator>  
  42.                     </td>  
  43.                     <td>&nbsp;</td>  
  44.                 </tr>  
  45.                 <tr>  
  46.                     <td>&nbsp;</td>  
  47.                     <td>Confirm Password</td>  
  48.                     <td>  
  49.                         <asp:TextBox ID="txt_CNF_Password" runat="server" TextMode="Password" Width="200px"></asp:TextBox>  
  50.                         <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="txt_CNF_Password"  
  51.                             ErrorMessage="Can't Be Blank" Font-Bold="True" ForeColor="#000066"></asp:RequiredFieldValidator>  
  52.                         <asp:CompareValidator ID="CompareValidator1" runat="server" ControlToCompare="txt_Password"  
  53.                             ControlToValidate="txt_CNF_Password" ErrorMessage=" Password Not matched" Font-Bold="True"  
  54.                             ForeColor="#000066"></asp:CompareValidator>  
  55.                     </td>  
  56.                     <td>&nbsp;</td>  
  57.                 </tr>  
  58.                 <tr>  
  59.                     <td>&nbsp;</td>  
  60.                     <td>First Name</td>  
  61.                     <td>  
  62.                         <asp:TextBox ID="txt_FName" runat="server" Width="200px"></asp:TextBox>  
  63.                         <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="txt_FName"  
  64.                             ErrorMessage="Can't Be Blank" Font-Bold="True" ForeColor="#000066"></asp:RequiredFieldValidator>  
  65.                     </td>  
  66.                     <td>&nbsp;</td>  
  67.                 </tr>  
  68.                 <tr>  
  69.                     <td>&nbsp;</td>  
  70.                     <td>Last Name</td>  
  71.                     <td>  
  72.                         <asp:TextBox ID="txt_LName" runat="server" Width="200px"></asp:TextBox>  
  73.                         <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ControlToValidate="txt_LName"  
  74.                             ErrorMessage="Can't Be Blank" Font-Bold="True" ForeColor="#000066"></asp:RequiredFieldValidator>  
  75.                     </td>  
  76.                     <td>&nbsp;</td>  
  77.                 </tr>  
  78.                 <tr>  
  79.                     <td>&nbsp;</td>  
  80.                     <td>Address</td>  
  81.                     <td>  
  82.                         <asp:TextBox ID="txt_Address" runat="server" Width="200px"></asp:TextBox>  
  83.                         <asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" ControlToValidate="txt_Address"  
  84.                             ErrorMessage="Can't Be Blank" Font-Bold="True" ForeColor="#000066"></asp:RequiredFieldValidator>  
  85.                     </td>  
  86.                     <td>&nbsp;</td>  
  87.                 </tr>  
  88.                 <tr>  
  89.                     <td>&nbsp;</td>  
  90.                     <td>City</td>  
  91.                     <td>  
  92.                         <asp:TextBox ID="txt_City" runat="server" Width="200px"></asp:TextBox>  
  93.                         <asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server" ControlToValidate="txt_City"  
  94.                             ErrorMessage="Can't Be Blank" Font-Bold="True" ForeColor="#000066"></asp:RequiredFieldValidator>  
  95.                     </td>  
  96.                     <td>&nbsp;</td>  
  97.                 </tr>  
  98.                 <tr>  
  99.                     <td>&nbsp;</td>  
  100.                     <td>Mobile No</td>  
  101.                     <td>  
  102.                         <asp:TextBox ID="txt_Mobile" runat="server" Width="200px"></asp:TextBox>  
  103.                         <asp:RequiredFieldValidator ID="RequiredFieldValidator8" runat="server" ControlToValidate="txt_Mobile"  
  104.                             ErrorMessage="Can't Be Blank" Font-Bold="True" ForeColor="#000066"></asp:RequiredFieldValidator>  
  105.                     </td>  
  106.                     <td>&nbsp;</td>  
  107.                 </tr>  
  108.                 <tr>  
  109.                     <td>&nbsp;</td>  
  110.                     <td>&nbsp;</td>  
  111.                     <td>  
  112.                         <asp:Button ID="btn_Register" runat="server" OnClick="btn_Register_Click" Text="Register" />  
  113.                     </td>  
  114.                     <td>&nbsp;</td>  
  115.                 </tr>  
  116.                 <tr>  
  117.                     <td>&nbsp;</td>  
  118.                     <td>&nbsp;</td>  
  119.                     <td>  
  120.                         <asp:Label ID="lbl_error" runat="server"></asp:Label>  
  121.                     </td>  
  122.                     <td>&nbsp;</td>  
  123.                 </tr>  
  124.                 <tr>  
  125.                     <td>&nbsp;</td>  
  126.                     <td>&nbsp;</td>  
  127.                     <td>&nbsp;</td>  
  128.                     <td>&nbsp;</td>  
  129.                 </tr>  
  130.             </table>  
  131.         </div>  
  132.     </div>  
  133.     </form>  
  134. </body>  
And
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.ServiceModel;  
  8. using System.ServiceModel.Description;  
  9. public partial class Register : System.Web.UI.Page  
  10. {  
  11.     // ServiceClient wcf = new ServiceClient();  
  12.     protected void Page_Load(object sender, EventArgs e)  
  13.     {  
  14.     }  
  15.     protected void btn_Register_Click(object sender, EventArgs e)  
  16.     {  
  17.         ServiceClient wcf = new ServiceClient();  
  18.         try  
  19.         {  
  20.             string result = wcf.Register_Login(txt_Username.Text, txt_Password.Text, txt_FName.Text, txt_LName.Text, txt_Address.Text, txt_Mobile.Text, txt_City.Text);  
  21.             lbl_error.Text = result.ToString();  
  22.         }  
  23.         catch (FaultException<string> ex)  
  24.         {  
  25.             lbl_error.Text = ex.Message.ToString();  
  26.         }  
  27.     }  
  28.     public class CustomException  
  29.     {  
  30.         public string Title;  
  31.         public string ExceptionMessage;  
  32.         public string InnerException;  
  33.         public string StackTrace;  
  34.     }  
  35. }  
Run the application, if you get any error from server-side, it will show an error that is given on webservice program. For more find the code.