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.

<system.serviceModel>

  <bindings>

    <wsHttpBinding>

      <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">

        <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384"/>

        <reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="false"/>

        <security mode="Message">

          <transport clientCredentialType="Windows" proxyCredentialType="None" realm=""/>

          <message clientCredentialType="Windows" negotiateServiceCredential="true" algorithmSuite="Default"/>

        </security>

      </binding>

    </wsHttpBinding>

  </bindings>

  <client>

    <endpoint address="http://localhost:4864/MyFIrst_WCFApplication/Service.svc" binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IService" contract="IService" name="WSHttpBinding_IService">

      <identity>

        <dns value="localhost"/>

      </identity>

    </endpoint>

  </client>

</system.serviceModel>

Step 4: Add a Login.Aspx page as given below..

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Login.aspx.cs" Inherits="Login" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head id="Head1" runat="server">

    <title></title>

    <style type="text/css">

        .style1

        {

            width: 100%;

        }

    </style>

</head>

<body>

    <form id="form1" runat="server">

    <div>

        <div style="height: 150px">

        </div>

        <div>

            <table class="style1">

                <tr>

                    <td>

                        &nbsp;

                    </td>

                    <td>

                        &nbsp;

                    </td>

                    <td>

                        &nbsp;

                    </td>

                    <td>

                        &nbsp;

                    </td>

                </tr>

                <tr>

                    <td>

                        &nbsp;

                    </td>

                    <td>

                        User name

                    </td>

                    <td>

                        <asp:TextBox ID="txt_Username" runat="server" Width="200px"></asp:TextBox>

                    </td>

                    <td>

                        &nbsp;

                    </td>

                </tr>

                <tr>

                    <td>

                        &nbsp;

                    </td>

                    <td>

                        Password

                    </td>

                    <td>

                        <asp:TextBox ID="txt_Password" runat="server" TextMode="Password" Width="200px"></asp:TextBox>

                    </td>

                    <td>

                        &nbsp;

                    </td>

                </tr>

                <tr>

                    <td>

                        &nbsp;

                    </td>

                    <td>

                        &nbsp;

                    </td>

                    <td>

                        <asp:Button ID="btn_Login" runat="server" OnClick="btn_Login_Click" Text="login" />

                        &nbsp;

                    </td>

                    <td>

                        &nbsp;

                    </td>

                </tr>

                <tr>

                    <td>

                        &nbsp;

                    </td>

                    <td>

                        &nbsp;

                    </td>

                    <td>

                        <asp:Label ID="lbl_error" runat="server"></asp:Label>

                    </td>

                    <td>

                        &nbsp;

                    </td>

                </tr>

            </table>

        </div>

    </div>

    </form>

</body>

</html>

Step 5: Go to Login.aspx.cs page and write the below code.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.ServiceModel;

public partial class Login : System.Web.UI.Page

{

    ServiceClient wcfservice = new ServiceClient();

    protected void Page_Load(object sender, EventArgs e)

    {

    }

    protected void btn_Login_Click(object sender, EventArgs e)

    {

        bool result = wcfservice.Check_Login(txt_Username.Text, txt_Password.Text);

        lbl_error.Text = result.ToString();

    }

}

Step 6: In same way, create for register page.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Register.aspx.cs" Inherits="Register" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<head id="Head1" runat="server">

    <title></title>

    <style type="text/css">

        .style1

        {

            width: 100%;

        }

    </style>

</head>

<body>

    <form id="form1" runat="server">

    <div>

        <div style="height: 150px">

        </div>

        <div>

            <table class="style1">

                <tr>

                    <td>

                        &nbsp;

                    </td>

                    <td>

                        &nbsp;

                    </td>

                    <td>

                        &nbsp;

                    </td>

                    <td>

                        &nbsp;

                    </td>

                </tr>

                <tr>

                    <td>

                        &nbsp;

                    </td>

                    <td>

                        User name

                    </td>

                    <td>

                        <asp:TextBox ID="txt_Username" runat="server" Width="200px"></asp:TextBox>

                        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txt_Username"

                            ErrorMessage="Can't Be Blank" Font-Bold="True" ForeColor="#000066"></asp:RequiredFieldValidator>

                    </td>

                    <td>

                        &nbsp;

                    </td>

                </tr>

                <tr>

                    <td>

                        &nbsp;

                    </td>

                    <td>

                        Password

                    </td>

                    <td>

                        <asp:TextBox ID="txt_Password" runat="server" TextMode="Password" Width="200px"></asp:TextBox>

                        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txt_Password"

                            ErrorMessage="Can't Be Blank" Font-Bold="True" ForeColor="#000066"></asp:RequiredFieldValidator>

                    </td>

                    <td>

                        &nbsp;

                    </td>

                </tr>

                <tr>

                    <td>

                        &nbsp;

                    </td>

                    <td>

                        Confirm Password

                    </td>

                    <td>

                        <asp:TextBox ID="txt_CNF_Password" runat="server" TextMode="Password" Width="200px"></asp:TextBox>

                        <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="txt_CNF_Password"

                            ErrorMessage="Can't Be Blank" Font-Bold="True" ForeColor="#000066"></asp:RequiredFieldValidator>

                        <asp:CompareValidator ID="CompareValidator1" runat="server" ControlToCompare="txt_Password"

                            ControlToValidate="txt_CNF_Password" ErrorMessage=" Password Not matched" Font-Bold="True"

                            ForeColor="#000066"></asp:CompareValidator>

                    </td>

                    <td>

                        &nbsp;

                    </td>

                </tr>

                <tr>

                    <td>

                        &nbsp;

                    </td>

                    <td>

                        First Name

                    </td>

                    <td>

                        <asp:TextBox ID="txt_FName" runat="server" Width="200px"></asp:TextBox>

                        <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="txt_FName"

                            ErrorMessage="Can't Be Blank" Font-Bold="True" ForeColor="#000066"></asp:RequiredFieldValidator>

                    </td>

                    <td>

                        &nbsp;

                    </td>

                </tr>

                <tr>

                    <td>

                        &nbsp;

                    </td>

                    <td>

                        Last Name

                    </td>

                    <td>

                        <asp:TextBox ID="txt_LName" runat="server" Width="200px"></asp:TextBox>

                        <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ControlToValidate="txt_LName"

                            ErrorMessage="Can't Be Blank" Font-Bold="True" ForeColor="#000066"></asp:RequiredFieldValidator>

                    </td>

                    <td>

                        &nbsp;

                    </td>

                </tr>

                <tr>

                    <td>

                        &nbsp;

                    </td>

                    <td>

                        Address

                    </td>

                    <td>

                        <asp:TextBox ID="txt_Address" runat="server" Width="200px"></asp:TextBox>

                        <asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" ControlToValidate="txt_Address"

                            ErrorMessage="Can't Be Blank" Font-Bold="True" ForeColor="#000066"></asp:RequiredFieldValidator>

                    </td>

                    <td>

                        &nbsp;

                    </td>

                </tr>

                <tr>

                    <td>

                        &nbsp;

                    </td>

                    <td>

                        City

                    </td>

                    <td>

                        <asp:TextBox ID="txt_City" runat="server" Width="200px"></asp:TextBox>

                        <asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server" ControlToValidate="txt_City"

                            ErrorMessage="Can't Be Blank" Font-Bold="True" ForeColor="#000066"></asp:RequiredFieldValidator>

                    </td>

                    <td>

                        &nbsp;

                    </td>

                </tr>

                <tr>

                    <td>

                        &nbsp;

                    </td>

                    <td>

                        Mobile No

                    </td>

                    <td>

                        <asp:TextBox ID="txt_Mobile" runat="server" Width="200px"></asp:TextBox>

                        <asp:RequiredFieldValidator ID="RequiredFieldValidator8" runat="server" ControlToValidate="txt_Mobile"

                            ErrorMessage="Can't Be Blank" Font-Bold="True" ForeColor="#000066"></asp:RequiredFieldValidator>

                    </td>

                    <td>

                        &nbsp;

                    </td>

                </tr>

                <tr>

                    <td>

                        &nbsp;

                    </td>

                    <td>

                        &nbsp;

                    </td>

                    <td>

                        <asp:Button ID="btn_Register" runat="server" OnClick="btn_Register_Click" Text="Register" />

                    </td>

                    <td>

                        &nbsp;

                    </td>

                </tr>

                <tr>

                    <td>

                        &nbsp;

                    </td>

                    <td>

                        &nbsp;

                    </td>

                    <td>

                        <asp:Label ID="lbl_error" runat="server"></asp:Label>

                    </td>

                    <td>

                        &nbsp;

                    </td>

                </tr>

                <tr>

                    <td>

                        &nbsp;

                    </td>

                    <td>

                        &nbsp;

                    </td>

                    <td>

                        &nbsp;

                    </td>

                    <td>

                        &nbsp;

                    </td>

                </tr>

            </table>

        </div>

    </div>

    </form>

</body>

And

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.ServiceModel;

using System.ServiceModel.Description;

public partial class Register : System.Web.UI.Page

{

    // ServiceClient wcf = new ServiceClient();

    protected void Page_Load(object sender, EventArgs e)

    {

    }

    protected void btn_Register_Click(object sender, EventArgs e)

    {

        ServiceClient wcf = new ServiceClient();

        try

        {

            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);

            lbl_error.Text = result.ToString();

        }

        catch (FaultException<string> ex)

        {

            lbl_error.Text = ex.Message.ToString();

        }

    }

    public class CustomException

    {

        public string Title;

 

        public string ExceptionMessage;

 

        public string InnerException;

 

        public string StackTrace;

    }

}

Run the application, if you get any error from server side, it will show a error that is given on webservice program. For more find the code.
 

Ebook Download
View all
Learn
View all