articles

Home / DeveloperSection / Articles / Simple "Hello World” in WCF

Simple "Hello World” in WCF

Sumit Kesarwani 6564 22-Aug-2013

Introduction:

 Here I am creating a simple WCF program that contains single service method “Message ()” which return simple message of good morning to consumer of this service.

The basic objective of this sample is to illustrate, how WCF operate and communicate with other application.

So let’s start from very first step.

Step 1:Start Visual studio 2010 and click->File->new->Project

Simple "Hello World” in WCF

Step 2: This will open a new dialog box select->WCF->WCF service Application and give appropriate (MyCalculator) name to your service and Click on OK button.

Simple "Hello World” in WCF

Step3: Now you have opened the solution explorer click on IService1.cs and delete all the code, what it contain by default. And write your own code as give below:

Note: you can delete or rename IService class and add your own class.

 

Simple "Hello World” in WCF

Step 4: Now open Service1.svc file and write down the following code.

Simple "Hello World” in WCF

Step5: Open Web.config file and configure the “endpoint”. As we have already discussed the end point is the basics for accessing WCF services.

Simple "Hello World” in WCF

Add the following line of code in web.config and save it.

<services>

      <service name="WcfService1.Service1" >
       <endpoint address=""contract="WcfService1.IService1"binding="basicHttpBinding"/>
        <endpoint address="mex" contract="IMetadataExchange" binding="mexHttpBinding"/>
      </service>
    </services> 

Step6: Build the solution and run it by using f5. This will display that service is successfully creates as in the browser.

Simple "Hello World” in WCF

If you able to see the above window after following the entire step mentioned above. Then you did well.

Congratulations! You have created the service successfully, now we use this WCF service by using the other application (as client).

So now we will take a simple console application to consume the service offered by the WCF(what we have created)

TO Consuming the WCF service in Console Application

Step 1: Follow the step 1 of above and open the console application.

Simple "Hello World” in WCF

Step2: Add the service references in your project what was created by you above.

Simple "Hello World” in WCF

Step3: Now in face of you have opened a new dialog box, enter the service address and click on ok button.

Simple "Hello World” in WCF

Step 4: Write down the following code as

using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WcfServiceConsumer
{
    class Program
    {
        static void Main(string[] args)
        {
            //ServiceReferenc1 is your service references
            // and Service1Client is the service what was contracted in above.
            // we have create a object(some time callesd proxy)
            // and we call the method what was contacted under the mehod contract "Message()" in this case.
            ServiceReference1.Service1Client objService = new ServiceReference1.Service1Client();
            Console.WriteLine(objService.Message());
            Console.ReadKey();
        }
    }
}

 

Step5: Build and run you program the output might be as

Simple "Hello World” in WCF

 I hope you learn the basics sample of WCF creation and its uses.

Thank you.

Updated 07-Sep-2019

Leave Comment

Comments

Liked By