Visual Studio 2008 SP1: A Simple ADO.Net Data Service Walkthrough

12 במאי 2008

no comments

Visual Studio 2008 SP1: A Simple ADO.Net Data Service Walkthrough


Back in December, when the last preview of ADO.Net Data Services was released I wrote several blog posts about the topic. Now that Beta 1 of Visual Studio 2008 and .Net Framework 3.5 Service Pack 1 is out, it is a good time to refresh those posts. Additionally, this post is a part of a series of posts Exploring Beta 1 of Visual Studio 2008 and .Net Framework 3.5.


This post is a step by step guide for building a Simple ADO.Net Data Service for the Blog database I have been using though my recent samples.


1. Create the Initial Web Project


Create a new standard ASP.Net Web Application. Notice that there is no special project template for a Web Data Service, and later we will see the new item template. In this guide, I called my web application BlogWebApp.


ADO.Net Data Services VS2008 SP1 Beta1 


Visual Studio 2008 created an initial Default.aspx page as part of the web application – it can be removed since we are not exposing any presentation layer as part of this data service.


2. Create an Entity Data Model


Add a new item of ADO.Net Entity Data Model to the application. This is the new item template that is installed as part of Visual Studio 2008 and .Net Framework 3.5 Service Pack In this guide, I called my model Blog.edmx.


ADO.Net Data Services VS2008 SP1 Beta1 


In the Entity Data Model Wizard, choose Generate From database option, select the database connection to use, and then select the database objects you want to import to the model. In this sample, I imported all the database objects.


ADO.Net Data Services VS2008 SP1 Beta1


Click Finish. The new model is now created and we can see that by default, all the entities are mapped 1:1 to the database table, besides the PostCategories table that does not appear in the model since it only represents a link table for implementing a n:m releation.


ADO.Net Data Services VS2008 SP1 Beta1


Click Save to save the model and let Visual Studio 2008 generate the entity classes.


3. Create the ADO.Net Data Service


Add a new ADO.Net Data Service item to the project. This is the new item template that is installed as part of Visual Studio 2008 and .Net Framework 3.5 Service Pack. in this guide I called this service BlogDataService.svc.


ADO.Net Data Services VS2008 SP1 Beta1 


Creating this data service adds a new item to the project, and opens the service for editing. Additionally, this also adds a reference to System.Data.Services.dll which is a new assembly that ships as part of this service pack (in previous previews of ADO.Net Data Services this assembly was called Microsoft.Data.Web.dll).



public class BlogDataService : 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)


    {


       
    }


}


Note: In previous previews of ADO.Net Data Services, the data service base class was called WebDataService<T> and the InitializeService method took an instance of IWebDataServiceConfiguration.


Edit the ADO.Net Data Service. First, replace the template comment in the class definition



/* TODO: put your data source class name here */


with the name of the data provider. In this guide, since we are using ADO.Net Entity Framework, we will use the BlogDataContext class as a provider. This class is one of the classes that Visual Studio has generated when we created the LINQ to SQL model.



public class BlogDataService : DataService< BlogModel.BlogEntities >


{


    public static void InitializeService(IDataServiceConfiguration config)


    {


       
    }


}


Run the project, and enable debugging if the dialog appears. The ASP.Net Web Server starts and the Internet Explorer with it, navigating to the data service: http://localhost:1039/BlogDataService.svc/. Notice that the output returns no results, and the output looks like:



<service xml:base="http://localhost:1039/BlogDataService.svc/"


        xmlns:atom="http://www.w3.org/2005/Atom"


        xmlns:app="http://www.w3.org/2007/app"


        xmlns="http://www.w3.org/2007/app">


  <workspace>


    <atom:title>Default</atom:title>


  </workspace>


</service>


The reason is that as of the December CTP of ADO.Net Data Services, Access Control was integrated into the Data Services, not allowing anyone to view the metadata or the data itself of any resource, without explicitly allowing it. I will not dive into this access control features in this post.


Allow all the resources to be readable. Edit the Data Service, and in the InitializeService method, use the configuration parameter to do this.



public static void InitializeService(IDataServiceConfiguration config)


{



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


}


Note: In previous versions the method that set the access rule for entity sets was called SetResourceContainerAccessRule and the Access Rules enumeration was called ResourceContainerRights.


This call gives all the entity sets the ability to be seen via the service.


4. Query the Data Service via Browser


If you now run the service (http://localhost:1039/BlogDataService.svc/) again, you can see the following output:



<service xml:base="http://localhost:1039/BlogDataService.svc/"


        xmlns:atom="http://www.w3.org/2005/Atom"


        xmlns:app="http://www.w3.org/2007/app"


        xmlns="http://www.w3.org/2007/app">


  <workspace>


    <atom:title>Default</atom:title>


    <collection href="Blogs">


      <atom:title>Blogs</atom:title>


    </collection>


    <collection href="Categories">


      <atom:title>Categories</atom:title>


    </collection>


    <collection href="Posts">


      <atom:title>Posts</atom:title>


    </collection>


  </workspace>


</service>


If you cannot see the output in the above way, go to Tools->Internet Options-> Content Tab. Click the Settings button in the Feeds section, an uncheck the Turn on Feed Reading View option.


Notice that some elements contain the href attribute with an additional URL. We can use it to navigate through the data:




<feed xml:base="http://localhost:1039/BlogDataService.svc/"
      xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices"
      xmlns:m
="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"
      xmlns="http://www.w3.org/2005/Atom">


  <title type="text">Blogs</title>


  <id>http://localhost:1039/BlogDataService.svc/Blogs</id>


  <updated>2008-05-07T10:26:34Z</updated>


  <link rel="self" title="Blogs" href="Blogs" />


  <entry m:type="BlogModel.Blogs">


    <id>http://localhost:1039/BlogDataService.svc/Blogs('bursteg')</id>


    <title type="text" />


    <updated>2008-05-07T10:26:34Z</updated>


    <author>


      <name />


    </author>


    <link rel="edit" title="Blogs" href="Blogs('bursteg')" />


    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Posts"
          type="application/atom+xml;type=feed" title="Posts" href="Blogs('bursteg')/Posts" />


    <content type="application/xml">


      <m:properties>


        <d:BlogID>bursteg</d:BlogID>


        <d:BlogName>Guy Burstein Blog</d:BlogName>


        <d:Owner>Guy Burstein</d:Owner>


      </m:properties>


    </content>


  </entry>


</feed>




<entry xml:base="http://localhost:1039/BlogDataService.svc/"
       xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices"
       xmlns:m
="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"
       m:type
="BlogModel.Blogs" xmlns="http://www.w3.org/2005/Atom">


  <id>http://localhost:1039/BlogDataService.svc/Blogs('bursteg')</id>


  <title type="text" />


  <updated>2008-05-07T10:39:20Z</updated>


  <author>


    <name />


  </author>


  <link rel="edit" title="Blogs" href="Blogs('bursteg')" />


  <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Posts"
        type="application/atom+xml;type=feed" title="Posts" href="Blogs('bursteg')/Posts" />


  <content type="application/xml">


    <m:properties>


      <d:BlogID>bursteg</d:BlogID>


      <d:BlogName>Guy Burstein Blog</d:BlogName>


      <d:Owner>Guy Burstein</d:Owner>


    </m:properties>


  </content>


</entry>




<entry xml:base="http://localhost:1039/BlogDataService.svc/"
       xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices"
       xmlns:m
="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"
       m:type="BlogModel.Posts" xmlns="http://www.w3.org/2005/Atom">


  <id>http://localhost:1039/BlogDataService.svc/Posts(3)</id>


  <title type="text" />


  <updated>2008-05-07T10:41:12Z</updated>


  <author>


    <name />


  </author>


  <link rel="edit" title="Posts" href="Posts(3)" />


  <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Blogs"
        type="application/atom+xml;type=entry" title="Blogs" href="Posts(3)/Blogs" />


  <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Categories"
        type="application/atom+xml;type=feed" title="Categories" href="Posts(3)/Categories" />


  <content type="application/xml">


    <m:properties>


      <d:PostID m:type="Edm.Int32">3</d:PostID>


      <d:Title>Post 3</d:Title>


      <d:Body>This is the body of post 3</d:Body>


      <d:PublishDate m:type="Edm.DateTime">2008-05-07T13:21:41.537</d:PublishDate>


    </m:properties>


  </content>


</entry>


And so on…


You can read more about URL syntax for December CTP of ADO.NET Data Services. Although there are several updates from the December CTP, you can get a very good idea of it.


Enjoy!

Add comment
facebook linkedin twitter email