Pages

Thursday 27 February 2014

WCF Tutorial for Beginners


Definition of WCF 

Windows Communication Foundation (WCF) is a framework for building service-oriented applications. Using WCF we can build secure, reliable, transacted solutions that integrate across platforms.


Description


  • WCF inter-operate between WCF-based applications and any other processes that communicate via SOAP (Simple Object Access Protocol) messages.
  • So how can a WCF service be used to communicate using different protocols and from different kinds of applications, for this we want to use a WCF service from an application.
  • If we want to use a WCF service, we have to get answer for the three questions given below
             1. From where can the client access the service 
             2. How to access the service i.e protocols and message formats
             3. What functionality does the service provides to the clients 

  • The answers for the above questions are known as the ABC of a WCF service.
             Address : Like a web service, WCF service also provides a URI which the client
                            needs to access the WCF service. This URI is called the Address of
                            WCF service. So we got answer to the first question.

             Binding : We got the address of WCF service, so we know how to connect
                            to it from a client. Now we should think about how to communicate
                            with the service (Protocol wise). So Binding is what defines how the
                            WCF service handles the communication.It could also define other
                            communication parameters like message encoding etc. So this clears
                            the second question.

             Contract : It is what defines the public data and interfaces that WCF 
                             service provides to the clients. So we have got answers to all the 
                             3 questions asked above.

Now lets go for your First WCF Project

Steps

1. Launch Visual Studio 2008 or above.
2. Create New Project -> WCF Service Application





3. Now your project MyFirstWCF will have Iservice1 and Service1. Replace
    the codes of both with the one given below

Iservice1

Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace MyfirstWCF
{
    
    [ServiceContract]
    public interface IService1
    {

        [OperationContract]
        string GetData(string Name);

    }


}

See the Screenshot



Service1

Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace MyfirstWCF
{
       public class Service1 : IService1
    {
        public string GetData(string Name)
        {
            return string.Format("Hello: {0}", Name);
        }
        
    }
}

See the Screenshot




   4. Now Rebuild MyFirstWCF and Debug. You may get an output page in your browser 
       as shown below.



    If you are not getting the above output page  and getting the below one, 
    don't worry just click on Service1.svc, you will get the required output.


 The url shown above i.e localhost:22186/Service1.svc is the address of your service.
 Note down this address , it is required for further steps.

5. So we have created a Service and now we need to create a New Project from 
    which we can access the service. So Right click on Main Project file on 
    Solution Explorer and Add -> New Project as shown in the below screenshot.


6. Now you have to add a Windows Form Application  as shown below.


7. Design the Form as shown in the below screenshot


8. Here you will have to Add a reference to the service you have created. So just Right
   click on the project MyForm from Solution Explorer and select Add Service Reference.


   a)  Here you have to give the Address of the Service you got from Step 4.
       (Here its localhost:22186/Service1.svc ). Now ClicGo.
   b) Now under Services, the service you have created will be listed.
   c) Click OK

9. This is the last step and your first WCF Project will be ready with this step.
     Double click on Say Hello button and copy the code given below.

    Form 1
    Code

using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Myform.ServiceReference1;

namespace Myform
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Service1Client client = new Service1Client();
            MessageBox.Show(client.GetData(textBox1.Text));
            client.Close();
        }
    }
}

10. Now Rebuild and Debug your MyForm Project. Enter your name and click on
     Say Hello, you will get a Messagebox with Hello added as a prefix to your name.
     Hello is added by the service. See the screenshot of the output.

Conclusion

You have done your First WCF Project. If you have any issues or doubts in getting the output for this project, please feel free to drop a comment.

   

    

   

8 comments:

  1. Thank you Tijo...It helped me a lot :-)

    ReplyDelete
  2. Amazing article.loved reading every bit.

    ReplyDelete
  3. Thanks a lot

    ReplyDelete
  4. Nice Description

    ReplyDelete
  5. Thanks good one.

    ReplyDelete
  6. Awesome description....Thanks a lot

    ReplyDelete
  7. you're my hero,been struggling with this for 3 days now!

    ReplyDelete

 

About Me

I am Tijo Tom, a Computer Engineer from India.Right from my schooling, I had a passion for Programming, PC Tricks, Virus Creation and Hacking.

I started this blog on Jan 1, 2014 to share my views and ideas with the world.

You can know more about me on