Visual Studio 2008 SP1 Beta is now available!  You can download it here.  

 

ADO.net Data Service is included in the SP1 beta, and there is a bit difference from CTP time.  Let’s follow the following procedure to create a C# website, which provides ADO.net data service on a class with IQueryable interface.

 

1.       Create a  new C# website

2.       Add a class file, e.g. class.cs under App_Code directory

3.       Add reference to System.Data.Services.Client

4.       Use the following code for the class1.cs , note, we need a namespace added here (e.g. CellPhoneNS)

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Data.Services.Common;

 

namespace CellPhoneNS

{

    /// <summary>

    /// Summary description for Class1

    /// </summary>

    [DataServiceKey("ModelName")]

    public class CellPhone

    {

        public string ModelName { get; set; }

 

        public float Price { get; set; }

 

        public bool Bluetooth { get; set; }

 

        public CellPhone(string modelName, float price, bool bluetooth)

        {

            ModelName = modelName;

            Price = price;

            Bluetooth = bluetooth;

        }

    }

 

    public class CellPhoneContainer

    {

        private List<CellPhone> _cellPhones;

 

        public CellPhoneContainer()

        {

            _cellPhones = new List<CellPhone>

                {   new CellPhone("Razor", 234, true),

                    new CellPhone("Treo 700wx", 550, true),

                    new CellPhone("Iphone", 145, true)

                };

        }

 

        public IQueryable<CellPhone> CellPhones

        {

            get

            {

                return _cellPhones.AsQueryable();

            }

        }

    }

}

 

5.       Add new item ADO.NET Data Service, e.g. WebDataService.svc

6.       Add namespace to the new WebDataService.cs file

7.       Change the svc file to include the namespace, i.e. Service=”CellPhoneNS.WebDataService”

8.       Change WebDataService.cs file as following:

 

using System;

using System.Data.Services;

using System.Collections.Generic;

using System.Linq;

using System.ServiceModel.Web;

 

namespace CellPhoneNS

{

    public class WebDataService : DataService<CellPhoneContainer>

    {

        // 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.

            // For testing purposes use "*" to indicate all entity sets/service operations.

            // "*" should NOT be used in production systems.

 

            // Example for entity sets (this example uses "AllRead" which allows reads but not writes)

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

 

            // Example for service operations

            config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);

        }

 

        //// Query interceptors, change interceptors and service operations go here

        //[WebGet]

        //public int someMethod() {return 1;}

    }

}

 

9.       View svc file in the browser to see the ADO.net service for your class

 

The  obvious breaking changes from the original CTP is the following:

1.       Use     [DataServiceKey("ModelName")] above the class, instead of the [DataWebKey] on method name.

2.       The reference for the above attribute is System.Data.Services.Common from assembly System.Data.Services.Client.dll, instead of the System.Data.Services

 

Note, you need namespace in the website to help the ADO.net service consumers in the future, so they won’t get an empty namespace…

 

Hope this helps.

 

Reference:

http://lostintangent.com/training-series/adonet-data-services-training-series/

 

======

中文               

 

Visual Studio 2008 SP1 Beta中提供了ADO.net 数据服务,和CTP(社区技术预览版)时的数据服务稍有不同。下面的例子新建一个C#网页,并通过一个带有IQueryable 接口的类来提供ADO.net 数据服务。 

1.       新建一个C#网页

2.       加入一个类文件,如 在App_Code目录中加入class.cs文件

3.       加入对System.Data.Services.Client的引用

4.       U在class1.cs中加入下面的程序(参照上面英文版本第4步)。注意,我们加入了一个命名空间(CellPhoneNS)

5.       加入新的ADO.NET 数据服务项目, 如 WebDataService.svc

6.       在WebDataService.cs 文件中加入命名空间

7.       更改svc 文件,加入命名空间,如:Service=”CellPhoneNS.WebDataService”

8.       更改WebDataService.cs (参照上面英文版本第8步)。

9.       V在浏览器中查看svc文件,来看你生成的ADO.NET 数据服务

 

和CTP不一样的地方在:

1.       在类的上面定义[DataServiceKey("ModelName")] ,而不是在函数上方定义[DataWebKey] 。

2.       上面的属性是在 System.Data.Services.Common 中定义,要引用System.Data.Services.Client.dll 文件,而不是System.Data.Services

 

注意,在网页中,你需要定义命名空间,来帮助将来的ADO.net数据服务的客户端,这样他们不会拿到一个空的命名空间。 

 

引用: 

http://lostintangent.com/training-series/adonet-data-services-training-series/