Saturday, December 13, 2008

Surfacing a Custom Data Source in ADO.NET Data Services.

 

What is ADO.NET Data Services?

In .NET 3.5 with SP1  Microsoft introduced the new ADO.NET Data Service Framework.  ADO.NET Data Service expose the Data as  REST Style.  This provides to access the  data resource in a URI  Addressable  fashion.

ADO.NET Data Service uses the  ATOM and JSON format to represent the data with HTTP/HTTPS protocols.

Application Interact with  ADO.NET Data Services using HTTP verbs  GET,POST, PUT, DELETE.

 

The following Steps are needed to expose Custom Data Source as a ADO.NET Data Service.

 

1) First Create a ASP.NET Web Application.

2) Define a Custom Data Source as CLR Type ( Class).

3) Add  ADO.NET Data Service Template to the ASP.NET Web application and configure with our custom Data source.

4) Expose the public property  IQueryable<T> interface to objects that implement IEnumerable<T>. This new interface makes it easy to deploy ADO.NET Data Services based on lists, arrays, and collections in the .NET Framework.

 

STEP 1: 

image

Select ASP.NET Web application project  this will add default.aspx by default  now select this file and Delete it.  Now right click on newly created project click Add and select  ADO.NET Data Service from the list of available Templates and name it CustomDataService

image

Now press F7 and go to code view you will see the CustomdataService  class derived from DataService

namespace SreeniAddressBook
{
    public class CustomdataService : DataService< /* TODO: put your data source class name here */ >
    {
        // 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("MyEntityset", EntitySetRights.AllRead);
            // config.SetServiceOperationAccessRule("MyServiceOperation", ServiceOperationRights.All);
        }
    }
}

 

Now Add another class to the project  and Name the class as  AddressBook

image

Next we need to add one more class to the project and name it  DataSource.

image

 

Now open CustomDataService.Svc  Edit the file as shown blow.

I configured all the Entity defined in Custom data source is accessible and only has read only access  the Code line

config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);

image

 

Here is the List of Access rule which you can configure [ It is an Enum EntitySetRights ]

image

Now browse the CustomDataService.svc

image

 

  if you point your browser to   http://localhost:54907/CustomDataService.svc/Addressbooks

you will see the content of the Addressbook.

image

 

thanks

Sreenivasaragavan.

1 comment:

georryan said...

Thank you so much. This solved a week of headaches and frustration from going down a path that wouldn't work.