Friday, October 3, 2008

Tutorial: Creating a simple ADO.NET Data Service application.

ADO.NET Data Services enable applications to expose data as a data service that can be consumed by web clients within corporate networks and across the internet. A data service is reachable via regular HTTP requests, using standard HTTP verbs such as GET, POST, PUT and DELETE to perform CRUD operations against the service. The payload format used by the service is controllable by the application, but all options are simple, open formats such as JSON and Atom/APP.

This article will help you get quick Hands on experience on ADO.NET entity framework using VS 2008 and LINQ to SQL classes.

  1. Create a new ASP.NET web site / ASP.NET web application and name it AdventureWorksServices. (I am going to use the AdventureWorks sample database provided by MS for creating the ADO.NET Data service.
  2. Add a new LINQ to SQL class and name it AdventureWorksORM.dbml to the project. Drag and drop the respective tables from the AdventureWorks database for the entities.

  3. Add a new ADO.NET Data Service class and name it AdventureWorkService.svc. Change the class definition for the AdventureWorkService class to add the AdventureWorksORMDataContext class as given below

    public class AdventureWorksService : WebDataService<AdventureWorksORMDataContext>

    {

    public static void InitializeService(IWebDataServiceConfiguration config)

    {

    config.SetResourceContainerAccessRule("*", ResourceContainerRights.AllRead);

  4. Run the web site/ web application and you can start querying the database from the URI as given below.

  5. For getting the row 1 value for Product.

1 comment:

Anonymous said...

I've got data coming back from a northwind ADO Service using entity classes to my client but CRUD does not work when i call savechanges

XML comes back with Forbidden in the message.

All the code is generated apart from the change shown below

public class WebDataService : DataService NorthwindModel.NorthwindEntities
{
// This method is called only once to initialize service-wide policies.
public static void InitializeService(IDataServiceConfiguration config)
{
// TODO: set rules to indicate which entity sets and service operations are visible, updatable, etc.
// Examples:
config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
// config.SetServiceOperationAccessRule("MyServiceOperation", ServiceOperationRights.All);
}
}

CRUD does not work out of the box for me so any tips