Create and Consume a WCF (Windows Communication Foundation) service using C#

Creating a simple ‘Hello World’-like WCF service is very simple with Visual Studio 2010. Open Visual Studio 2010 and select New => Project => WCF Service. Name it for example, HelloWorldWCF. It will initially create 2 files for you (IService.cs and IService.svc). Delete them.

Right-click on your project and add a new WCF Service named HW.svc. Your project should look something like this.

image

Open the IHW.cs file and modify it so it looks like this.

[sourcecode language="csharp" padlinenumbers="true" autolinks="false" gutter="false" toolbar="false" wraplines="true"]
namespace HelloWorldWCF
{
    [ServiceContract]
    public interface IHW
    {
        [OperationContract]
        float AccountBalance(int AccountNumber);
    }
}
[/sourcecode]

Then open the HW.svc.cs file which is the code-behind for the HW.svc file. Modify it so it looks like this.

[sourcecode language="csharp" autolinks="false" gutter="false" toolbar="false" wraplines="true"]
namespace HelloWorldWCF
{
    public class HW : IHW
    {
        public float AccountBalance(int AccountNumber)
        {
            //access a secured database for this.
            if (AccountNumber == 554)
            {
                return 10859.38F;
            }
            else
            {
                return 0.00F;
            }            
        }
    }
}
[/sourcecode]

That’s it. You have just created your first WCF service. To run it, right-click on the HW.svc and select ‘Set as Start Page’ and then push F5. The WCF Testclient opens up. Double click on the method you just created, enter in a value for the parameter and select the call button.

image

Consuming the WCF service from a Windows Form application is just as easy as creating it. In the WCF Testclient, note the http address and port that Visual Studio is using to run you WCF webservice. It may look like this (http://localhost:440928/HW.svc). You will need this url from within the Windows Form to add a reference to the WCF service.

Open another instance of Visual Studio 2010. Leave the instance you used to created the service open and the personal web service running. If you close it down, you will not be able to access the url above that is running the service.

In the new instance of Visual Studio 2010, select New => Project => Windows Form Application. Add a button and a label to the form. In the Solution explorer, right-click references and select Add Web Resource and enter the url we noted from the WCF Testclient, then Go. Give the service a logical namespace and select ok. In this example I named it AccountBalance.

image

Your solution explorer should look something like this.

image

Lastly, add the code to the button clicked event to call the WCF service.

[sourcecode language="csharp" autolinks="false" gutter="false" toolbar="false"]
private void button1_Click(object sender, EventArgs e)
{
  AccountBalance.HWClient client = 
                       new AccountBalance.HWClient();
  double message = client.AccountBalance(554);
  label1.Text = message.ToString();
}
[/sourcecode]

Before running the ConsumeWCF windows form application we just made, make sure the other instance of Visual Studio is still running. Click the button and the WCF service will be called.




Leave a Comment

Your email address will not be published.