WCF application for displaying and inserting records into database using Data Contract and Data Member Attributes – Part 2

Iservice.cs file is where we are going to define our interface and methods which they contain and Service.cs is where we are going to implement our methods which are in our interface.

We will also have to create image directory at the service application directory so that we can save the images to this location, in my case I have named the directory as images and in my system its location is:

F:\wcf programs\Leaders Info Insert Update DELETE\WcfServiceLeaders\WcfServiceLeaders\images

Service side code.

IService1.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Runtime.Serialization;

using System.ServiceModel;

using System.Text;

using System.Data;

namespace WcfServiceLeaders

{

// NOTE: If you change the interface name "IService1" here, you must also update the reference to "IService1" in Web.config.

[ServiceContract]

public interface IService1

{

 

[OperationContract]

string GetData(int value);

 

[OperationContract]

CompositeType GetDataUsingDataContract(CompositeType composite);

 

// TODO: Add your service operations here

[OperationContract]

DataTable GetLeadersInfo(Leaders ldr);

 

[OperationContract]

void AddLeader(Leaders ldr);

}

 

 

// 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; }

}

}

 

[DataContract]

public class Leaders

{

string name1;

[DataMember]

public string p_name

{

get{return name1;}

set{name1=value;}

}

string Country1;

[DataMember]

public string p_country

{

get{return Country1;}

set{Country1=value;}

}

string pic1;

[DataMember]

public string p_pic

{

get { return pic1; }

set { pic1 = value; }

}

string address;

[DataMember]

public string p_address

{

get { return address; }

set { address = value; }

}

}

}

 

Now in the above code we can see our interface IService1 is decorated with ServiceContract attribute. Service contract specifies what an endpoint communicates to the outside world (client).

Without Service Contract attribute, the interface is not visible to WCF clients. In simple words it is necessary to write service contract on an interface only then we would be able to invoke its methods on client side i.e. Expose it to exterior world.

 

We can also see that our methods are decorated with operation contract attribute. Operation contract defines which operations are to be exposed to the client (Exterior world). If we don't mention operation contract over method it will not get exposed to client side or in other words we won't be able to use it at client side.

 

We have also used class "Leaders" and decorated it with DataContract attribute which will expose it to the client and we will be using its member variables for inserting and displaying records. We have also decorated properties with Data Member attributes defined in class Leaders. Decorating member variables or properties with data member will expose them to the exterior world or client, without data member attribute the properties will not get exposed to the client side and therefore we won't be able to use them. When data member is applied to the member of a type it specifies that the member is part of a data contract and is serializable by the DataContractSerializer.

 

In the above example we have used explicit data contract (class, structure etc) as we are explicitly mentioning them. In my previous article we used implicit data contract (int, string etc…)

 

Continue ...

Jump to: Part 1, Part 3Part 4Part 5

Tags:

ASP.NET | WCF