google analytics

Saturday, April 11, 2009

Consume WCF Service

Hello all

Today I will show how to consume a WCF service. To go through this before, I have created a WCF service [ Creating WCF Service ] , which I will consume for my project in this article. Consuming WCF Service is as simple as consuming a simple webservice through Visual Studio 2008.

First I am taking an empty Web project named ConsumedWCF.

Now I will add a service reference which I have created before [ Creating WCF Service ] in this ConsumedWCF project as bellow:

In my previously created WCF service I have got a URL for my service "http://localhost:49530/DataService.svc?wsdl", I have pasted the URL in the Address box. One thing we should have to remember that,to consume the service we have to run that service during development.

Now I will consume the service from my Default.aspx  . My Default.aspx.cs content is as bellow:

using System;
using ConsumeWCF.ServiceReference1;

namespace ConsumeWCF
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
           DataServiceClient cl = new DataServiceClient();
           Response.Write( cl.GetUserDataFromService().UserName);
           Response.Write("<br />");
           Response.Write(cl.GetUserDataFromService().UserLocation);
           Response.Write("<br />");
           Response.Write(  cl.GetUserRestrictionFromService("1"));
           Response.Write("<br />");
           Response.Write(cl.GetUserRestrictionFromService("23"));
           
        }
    }
}

The above code will print the values which we have consumed form the WCF service.

Thanks

That's all for the day.
BYE

User ScrumPad for your Agile based projects.