Menu ▼


Consuming WCF service in asp.net application

In this article we will see how to consume WCF service in Asp.net application step by step.

1. Right click on the Asp.net application References and select Add Service Reference

add service reference

2. In the Add Service Reference dialogue box, Click on Discover and Select Services in Solution.

discover service

3. Give Namespace as WCFServiceReference and click on OK.

service reference

4. WCF service reference will be add to solution under Service References.

service reference in solution

5. Bindings and endpoint will be added to web.config as shown below.

  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_IService1" />
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://localhost:52221/Service1.svc" binding="basicHttpBinding"
        bindingConfiguration="BasicHttpBinding_IService1" contract="WCFServiceReference.IService1"
        name="BasicHttpBinding_IService1" />
    </client>
  </system.serviceModel>

6. Add a button & lable in ConsumeWCF.aspx

    <div>
        <asp:Button ID="btnGetTime" runat="server" Text="Get Time" OnClick="btnGetTime_Click" />
        <br /><br />
        <asp:Label ID="lblTime" runat="server" Style="color:Blue; font-size:18px; font-weight:bold;"></asp:Label>
    </div>

7. Implement btnGetTime_Click method in ConsumeWCF.aspx.cs

protected void btnGetTime_Click(object sender, EventArgs e)
{
    WCFServiceReference.Service1Client objWCF=new WCFServiceReference.Service1Client();
    lblTime.Text = objWCF.GetDateTime();
}

Where WCFServiceReference is service reference and created service client object objWCF. Now we can call the GetDateTime() method of WCF Service like objWCF.GetDateTime();


Output:

consumed service output

Download sample project : Download






comments powered by Disqus


Search dotnetlearners.com