Skip to main content

A Beginner's Tutorial for Understanding Windows Communication Foundation (WCF)

In this article, we will create a WCF service. Firstly, we will discuss some basics understanding of WCF services.

Basics of WCF services

WCF Services can be used to communicate with different type of applications using different protocols. if we want to use the WCF services we will have the basic understanding of its main components which is called ABC. Let's discuss these main components of WCF services one by one.

Address

A WCF provides a URI which can be used to locate the WCF services. This URI called the address of the WCF service.

Binding

Once we are able to locate the WCF service, The next point is how to communicate with WCF service like which protocol will be used to communicate. the binding which defines how the WCF service handle this communication, it also defines other communication parameters like message encoding

Contract

The contract defines what public data and interfaces provide by WCF service to the client. In other words what functionality WCF service will provide to the client. 

Implementation   

I am going to implement a WCF service in my MVCProject project which I create in my article .NET MVC5 .

Right click on the solution in solution explorer and click on add a new project. Select WCFService and name it ArithmeticService and press ok


Change the name of Service and interface.




Right click on the App_Code folder and add a new class with name ArithmeticOperation. Copy paste following code into that class. this class is actually a contract.

[DataContract]

public class ArithmeticOperation
{
    int m_first;
    int m_second;

    public ArithmeticOperation()

    {
        m_first = 0;
        m_second = 0;
    }

    public ArithmeticOperation(int first, int second)

    {
        m_first = first;
        m_second = second;
    }

    [DataMember]

    public int First
    {
        get { return m_first; }
        set { m_first = value; }
    }

    [DataMember]

    public int Second
    {
        get { return m_second; }
        set { m_second = value; }
    }

}


We will expose this class from our service so this class will have to be tagged with the attribute DataContract This attribute specifies that this data type can be used by consumers of this WCF service. Also, the public properties of this class will have to be tagged with the DataMember attribute to specify that clients can use these properties and to indicate that they will be needing serialization and deserialization.


in the next step Add following lines of code in the interface IArithmetic which was automatically created by the VS.



[ServiceContract]
public interface IArihmetic
{
    [OperationContract]
    ArithmeticOperation Add(ArithmeticOperation p1, ArithmeticOperation p2);

}

This interface will list the major functionalities provided by this service. We will have to tag this interface with the attribute  ServiceContract to specify that this interface is being exposed by this service and can be used by the clients.

We will then have to write methods for all major operations provided by this service and tag them with the attribute OperationContract to specify that these operations can be used by the clients.

Now open the ArithmeticService class and add following lines of code into it.in this service, we will implement the interface which we created earlier.

public class ArihmeticService : IArithmetic
{
    ArithmeticOperation IArithmetic.Add(ArithmeticOperation  p1, ArithmeticOperation  p2)
    {
        ArithmeticOperation result = new ArithmeticOperation ();

        result.First = p1.First + p2.First;
        result.Second = p1.Second + p2.Second;

        return result;
    }

  
}

In the Next step, we have to make this service visible to the client. To do this we need to specify the service behavior in the web.config.

<system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
        <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>

In th above line o code httpGetEnabled="true" which enables this service to provide its metadata when a client requests for it.

Set the  .SVC file as the startup page and run the website to see what happens.

congratulation! we have successfully created a WCF service. 

Happy Programming :)

Comments

Popular posts from this blog

ASP.NET AJAX PasswordStrength Extender

  AJAX Password Strength Extender shows the strength of user chosen passwords.the strength can be show in a  text form, a bar indicator or combination of both.Now i am going to show you how to add ajax password extender control to a asp.net page, In case of Visual studio 2005 install AjaxControlToolkit and in case of Visual studio 2010 add AjaxControlToolkit.dll in the project. I have added a text box and a label control in the page and page will be look like this:                                               After that Add password strength code in the aspx file of the web page. <body>     <form id="form1" runat="server">         <asp:ScriptManager ID="ScriptManager1" runat="ser...

How to send Email through ASP.NET in C#

Sending Email through ASP.NET is very easy.the .NET framework comes with a namespace which is uses for handling the Email.The namespace is:                                                            System.Net.Mail namespace Here i am using the two classes of the above mentioned namespace.The first class is MailMessage  class which is used for actual email and second is SmtpClient class which is for sending Email. Write the following code into the page load event: try { MailMessage mailMessage = new MailMessage(); mailMessage.To.Add( " test@domain.com " ); mailMessage.From = new MailAddress( " test2 @ domain.com " ); mailMessage.Subject = " Test Email " ; mailMessage.Body = " This is an ASP.NET test E-mail! " ; SmtpClient smtpClient = new SmtpClient( " smtp .te...

Linked List Data Structure

A linked list is a linear data structure just like arrays but its elements do not store in the contagious location. Elements in a linked list are connected through pointers as shown in the below image. Link list consists of nodes. Each node contains a data field and a pointer reference to the next node in the list. Important Points about the linked list A linked list can be used to store linear data of diffent type. Dynamic size. Ease of insertion and deletion of an element. Random access of an element is not allowed. if we want to search an element we have to go sequentially starting from the first node.  Extra memory space would be required for the pointer with each element of a linked list. A linked list is represented by a pointer to the first node o the list. The first node called head.if the linked list is empty then the head is NULL. Let's start implementation of a simple linked list in C# I have created a project of the consol...