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

Introduction of Arrays

An array is a linear data structure which stores collection of data in a contagious memory location. The idea is to store the collection of the same type of data. this makes it easier to calculate the position of each item by simply adding an offset to a base value. In the above image of an array, we can identify each element by its index. we can declare an array by specifying the types of its element. type [] arrayname; Types of Arrays single dimensional arrays Multidimensional arrays Single Dimensional Arrays A single dimensional array can be declared in the following way. int [] array = new int [ 5 ]; this array will contain the element from array[0] to array[4].the new operator will initialize  each  element of this array with zero. An array which contains string value can be declared the same way. string[] array = new string[6]; Array Initialization we can also initialize an array while declaration. For example int

How to upload a file on the server through upload control in asp.net

With ASP.NET upload a file on the server is very easy.With FileUpload control we can easily upload a file on the server.Following markup is required.   < form id ="form1" runat ="server" > < asp:FileUpload id ="FileUploadControl" runat ="server" /> < asp:Button runat ="server" id ="btnupload" text ="Upload" onclick =" btnupload _Click" /> < asp:Label runat ="server" id ="lblstatus" /> </ form > Here is the CodeBehind code required to handle upload a file on the server. protected void btnupload _Click( object sender, EventArgs e) { if (FileUploadControl.HasFile) { try { string filename = Path.GetFileName(FileUploadControl.FileName); FileUploadControl.SaveAs(Server.MapPath( " ~/ " ) + filename); lblstatus .Text = " Upload status: Fi

ASP.NET MVC 5 (Part 1)

This tutorial teaches you the basics of building an ASP.NET MVC 5 web app using  Visual Studio 2017 . Get started Start by installing Visual Studio 2017. You can  Install Visual Studio 2017 community version which is free. Open Visual Studio,  Instead of selecting  New Project  on the  Start page , you can use the menu bar and select  File  >  New Project . Your first app On the  Start page , select  New Project . In the  New Project  dialog box, select the  Visual C#  category on the left, then  Web , and then select the  ASP.NET Web Application (.NET Framework)  project template. Name your project "MVCProject" and then choose  OK . In the  New ASP.NET Web Application  dialog, choose  MVC  and then choose  OK . The visual studio used the default template for the project you just created.you have a running application which is doing nothing right now. Press  F5  to start debugging. When you press  F5 , Visual Studio starts  IIS Ex