Different types of WCF Contracts

By this article, we will get to know all contracts available in WCF
The contracts will let the client know regarding the functionality, the service provide. In WCF we need one interface and we can add all the Contracts inside the interface.

There are 4 types of contracts:

  1. ServiceContract
  2. OperationContract
  3. DataContract
  4. DataMember

You can get the ServiceContract and OperationContract WCF attribute classes in the System.ServiceModel namespace.
You can get the DataContract and DataMember classes are in the System.Runtime.Serialization namespace.
These classes are used to define the details of the contract that your service will have with calling clients.

ServiceContract:By adding the ServiceContract attribute class in the interface, we are telling the that you are a WCF service

OperationContract: If we need to expose a method to the client. Then We need to add this contrat to that particular method.

DataContract:The DataContract attribute class is added to mark that type (classes,
enumerations, or structures) serializable.
DataMember:The DataMember attribute class is used to mark individual fields
and properties that you want to serialize. You use this class with the DataContract class.

How to create a simple WCF service and consume it

Bellow i have explained how to create a simple WCF service in Visual Studio 2013
1. Click on new Project
New Project
2. From the left hand side of the templates please select WCF then you will get a list of templates associated with WCF.
select Template
3. Please select WCF service Application from the list. By default our visual studio will add one Interface and one svc file.
The interface will be have bellow contracts:

  1. ServiceContract
  2. OperationContract
  3. DataContract
  4. DataMember

it looks like bellow:

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

namespace MyService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract]
    public interface IService1
    {

        [OperationContract]
        string GetData(int value);

        [OperationContract]
        CompositeType GetDataUsingDataContract(CompositeType composite);

        // TODO: Add your service operations here
    }


    // Use a data contract as illustrated in the sample below to add composite types to service operations.
    [DataContract]
    public class CompositeType
    {
        bool boolValue = true;
        string stringValue = "Hello ";

        [DataMember]
        public bool BoolValue
        {
            get { return boolValue; }
            set { boolValue = value; }
        }

        [DataMember]
        public string StringValue
        {
            get { return stringValue; }
            set { stringValue = value; }
        }
    }
}


The Service1.svc.cs file looks like bellow:

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

namespace MyService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
    public class Service1 : IService1
    {
        public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }

        public CompositeType GetDataUsingDataContract(CompositeType composite)
        {
            if (composite == null)
            {
                throw new ArgumentNullException("composite");
            }
            if (composite.BoolValue)
            {
                composite.StringValue += "Suffix";
            }
            return composite;
        }
    }
}

4. The service will look like bellow in browser.

Service in browser
Now please create one website to consume the Service. Now we have the website.
The next work is to add service reference for the service, which we have created above.

1. Right click on the Website and the add service reference. Bellow i have provided the screenshots, please have a look.

Add Service reference

3. A pop up will open there you can add your service reference URL and you can add your service there.

Service reference window

You can get the service reference url by viewing the service in browser. From the browser you can get the service url.

Service Reference link
You can change your reference name like bellow:

Reference Name

Now you are done with reference adding in website. Now you can call the service for the website. Bellow here is some code

using MyServiceReference;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class User : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Service1Client client = new Service1Client();
        lblUserName.Text = client.GetData(4);
    }
}

This was small demo to Create and consume WCF service. Please put your valuable comments.