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
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.
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.
Step 4: Now open Service1.svc file and write down the following code.
Step5: Open Web.config file and configure the “endpoint”. As we have already discussed the end point is the basics for accessing WCF services.
Add the following line of code in web.config and save it.
<service name="WcfService1.Service1" >
<endpoint address="mex" contract="IMetadataExchange" binding="mexHttpBinding"/>
Step6: Build the solution and run it by using f5. This will display that service is successfully creates as in the browser.
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.
Step2: Add the service references in your project what was created by you above.
Step3: Now in face of you have opened a new dialog box, enter the service address and click on ok button.
Step 4: Write down the following code as
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();
Step5: Build and run you program the output might be as
I hope you learn the basics sample of WCF creation and its uses.