Friday 15 March 2013

WCF Lesson 2:- Data Contract in WCF



for any query you can mail me at Malhotra.isha3388@gmail.com.
you can also mail me your wcf problem which you face in your project

Data Contract in WCF

In the previous article I described basic Idea of WCF and Service Contract. Now I am going to describe your data contract.
As we know that WCF exchange information in XML format. Data contract defines which data type is serialized (convert into Xml). Primitive data types like int, string etc. serialized already as they defined in XSD (Xml Schema Definition). But custom data types like classes are not.
Wcf uses serialization engine i.e. Data Contract Serializer by default to serialize and DE serialize the data.
We can use DataContract attribute to serialize the class and DataMember attribute to Serialize its member.

For Example:-

In following example I simply create a class EmpData and declare two members in it. In service I create a simple method which returns the object of EmpData with some information.



Interface  

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ServiceModel;

[ServiceContract]
public interface ITechAltum
{
    [OperationContract]
    EmpData EmpRec();

}


Data Member Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ServiceModel;
using System.Runtime.Serialization;

[DataContract]
public class EmpData
{
   [DataMember]
    public int id;

  [DataMember]
    public string name;

  public int age;

}

As you can see here that I didn’t assign data member attribute on age. So this will not serialize and will not be accessed the outside.

I implement this interface in the following class:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

public class TechAltum: ITechAltum

{
   
    public EmpData EmpRec()
    {

        EmpData ev = new EmpData();
        ev.id = 101;
        ev.name = "isha";
        ev.age = 25;
        return ev;
   
    }
}

Changes in Sevice.svc

<%@ ServiceHost Language="C#" Debug="true" Service="TechAltum" CodeBehind="~/App_Code/TechAltum.cs" %>


Now implement it in the client side. As we know that our service will return the object of EmpData class. So we also have to create the object of that class.

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

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ServiceReference1.TechAltumClient ta = new ServiceReference1.TechAltumClient();
        ServiceReference1.EmpData ed = new ServiceReference1.EmpData();
        ed = ta.EmpRec();
        Response.Write(ed.id + " " + ed.name);

    }
}

As we didn’t serialize the age so we will not able to access it on our client side as shown in the following image of code.



As we can see that it shows only id and name not age.


14 comments:

  1. Webtrackker Technology is a leading mobile application development company offering integrated, dynamic and streamlined e-commerce solutions to businesses worldwide. Webtrackker takes pride in developing outstanding mobile applications using Android and iOS based platforms. We have created variety of core Android, core iOS and hybrid applications.

    Mobile Apps Development Company in Delhi NCR

    ReplyDelete
  2. Croma campus is creative constrictive lecture for SAS training in Noida and implements your knowledge through programming training. Croma campus provide best lecture on practical lab. We have a best mentor. croma campus have provided of online work.

    ReplyDelete
  3. Croma Campus is very famoused for AWS Training in Noida. Croma is best training center in Noida.Croma Provides 100% placement gurantee.and best lab facility and expirience based mentor , giving best fully practally project based training.Croma is giving AWS Training facility according other institute.

    ReplyDelete
  4. Croma campus is providing “Selenium Training in Noida” and best training in noida. Selenium is very important for every programmer and these programs have must for feature growth. croma campus is give placement 100% guarantee. Croma campus is creative constrictive lecture for Selenium Training.

    ReplyDelete
  5. Croma campus is very famous in noida or delhi-ncr. We provided developing or programming training. We have best training mentor. croma campus is give to AngularJS training in noida .we have complete facility and placement guarantee. croma have done already many students place in best .croma is better according other institute.

    ReplyDelete

  6. Croma campus is the finest top rank training center is provided MongoDB training in Noida. croma campus is give placement 100% guarantee. Croma provides quality fundamental driven job training for the student. Croma campus is best training in noida.

    ReplyDelete

  7. Croma campus is very famous in noida or Delhi-ncr. We provided developing or programming training. We have best training mentor. croma campus is give Ethical Hacking training in noida. We have complete facility and placement guarantee. croma have done already many students place in best company. croma is better according other institute.

    ReplyDelete
  8. Croma Campus is specialized different types of Trainings according to need of the students. Croma campus is providing is best Graphics Designing Training in Noida. We are providing totally practical based & project based training through more experience tutor. We are giving 100% placement with training certificate.

    ReplyDelete
  9. This is my first time visit to your blog and I am very interested in the articles that you serve. Provide enough knowledge for me. Thank you for sharing useful and don't forget, keep sharing useful info:
    AWS cloud training
    Selenium training
    Manal testing training
    Cucumber training

    ReplyDelete
  10. After seeing your article I want to say that also a well-written article with some very good information which is very useful for the readers....thanks for sharing it and do share more posts like this.

    Docker Kubernetes training in Gurgaon
    AWS cloud training in Gurgaon
    FullStack Development Training in Gurgaon

    ReplyDelete
  11. This comment has been removed by the author.

    ReplyDelete
  12. I am completed training last week. It was a very good 3 month session which included excellent training content, live examples, mock exams etc. Thanks to the trainer and Simplilearn.
    Machine Learning Training in Gurgaon
    Devops Training in Gurgaon
    Python Training in Gurgaon

    ReplyDelete
  13. Hello, I am very much interested in doing this course but I some confusions regarding this. Recently IIT Madras has launched an online degree program on Data Science and programming,

    Power Bi Training in Gurgaon
    Deeplearning Training in Gurgaon

    ReplyDelete