Wednesday, February 15, 2012

how to Implement wcf service

Vs 2010 provides different templates for develop WCF services .One it provides in file--> new website-->WCF service using which we can design develop WCF service in simplified manner by default this template will create One .SVC file fallowed by two .cs files.Most importantly End points for this new service are also created in web.config file.We can add more services using add new item WCF service template for every service that we create automatically.End points along with other configuration settings are written system.Service model is the tag where all these End points are defined.we can directly add more end points otherwise use wcf configuration editor utility to modify or add more points.

Here i will shown how to create a simple web service .For this we have to fallow the below steps.

start wcf service and add two more services called Products and category.These two are services we will use to provide categories and Products information.

The back end layer is performed using LINQ SQL and entity framework objects.so add entity framework classes using add new item from App code and choose Ado.net entity data model.EDM is very complex to create for our application but with EDM designer provided with VS to maximum extend we can simplify the creation of entities.

One EDM class are lINQ to SQL classes are prepared.Then we can provide operation contracts fallowed by implementation in our service and it's class go to products and add the following operation contract

[operation contract]
void Dowork()
[operation contract]
list GetProducts();
[operation contract]
products GetProducts(int product id);
[operation contract]
list GetProductsIn category(int cat id);

Now go to implementation class of this contract i:e product class.Implementation of each method like this

public listGetProducts()
{//linq to entities
Eshopmodel.Eshopentities obj=new Eshopmodel.EshopEntities();

var x=from n in obj.Products select n;
return x.Tolist();
}
public Eshopmodel.Products GetProducts(int productid)
{
Eshopmodel.EshopEntities obj=new Eshopmodel.EshopEntities();
var x=(from n in obj.products where n.productid==productid select n).First or default();
return x;
}
public Eshopmodel.Products GetProductsInCategory(int catid)
{
Eshopmodel.EshopEntities obj=new Eshopmodel.EshopEntities();
var x=(from n in obj.products where n.productid==catid select n).First or default();
return x.Tilist();
}
Post a Comment

Bel