Thursday, October 13, 2011

WCF are used in n-tier Architecture.

Step For Implementing WCF

1. Create a Project select WCF in this select WCFServiceLibrary Save As EvalServiceLibrary

2. Add a class File like "Eval.cs"
in this file write a class and the data member.
above the class definition Write the [DataContract]
attribute and on Datamember Write [DataMember] Attribute to enable this attribute you have To Add
System.Runtime.Serialization.

************************************Code (Eval.cs)**********
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;

namespace EvalServiceLibrary
{
[DataContract]
public class Eval
{
[DataMember]
public string Id;

[DataMember]
public double BasicSalary;

[DataMember]
public double IncomeTax;

[DataMember]
public double TotalIncome;

[DataMember]
public DateTime TimeofJoining;
}

}
***********************************************************
3. Add another class file like "IEvalService.cs"

You have to make a Interface that will have your Methods Definition. this Interface known as service Contract means it tells that what service we are going to provide and What Operation You can Perform.
in this File You have to write on the interface [serviceContract] and on the Method(Function) Definition write[Operation Contract] and To enable this attribute you have to Add name space System.ServiceModel.

*************************Code IEvalService.cs**************

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

namespace EvalServiceLibrary
{
[ServiceContract] // it tells We are providing IEvalservice as a service
public interface IEvalService
{
[OperationContract] // it tells You can do these Things if You take my Service
DateTime GetId(long Id);

[OperationContract]
double GetTotalIncome(double BasicSalary, double IncomeTax);

[OperationContract]
double TotalSalaryIs(double Total);
}
}

*******************************************************************
4. You Have to Add one more class file "ImplIEvalservice.cs"

this file has the implimentation of "IEvalService" Interface. write your own Way to impelment
them. this must be inherited by the IEvalService interface

**********************Code ImplIEvalService***********************************

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

namespace EvalServiceLibrary
{
[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
class ImplIEvalService : IEvalService
{

public DateTime GetId(long Id)
{
DateTime Time = DateTime.Now;
return Time;
}

public double GetTotalIncome(double BasicSalary, double IncomeTax)
{
if (BasicSalary > 20000)
return BasicSalary - IncomeTax;
return BasicSalary;
}

public double TotalSalaryIs(double Total)
{
return Total > 20000 ? Total + 1000 : Total + 1500;
}

}
}