<%@ ServiceHost Language="C#" Factory="System.Data.Services.DataServiceHostFactory, System.Data.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" Service="WcfCustomerDataService.WcfDataService1" %>
如果在开发非IIS Host应用的时候,则使用 WebServiceHost 来寄宿服务。详细参考:Hosting the Data Service (ADO.NET Data Services)
1.定义实体:实体里必须使用 DataServiceKeyAttribute 标注Key。否则DataService运行报错。[DataServiceKeyAttribute("OrderId")] public class Order { public int OrderId { get; set; } public string Customer { get; set; } public IList<Item> Items { get; set; } } [DataServiceKeyAttribute("Product")] public class Item { public string Product { get; set; } public int Quantity { get; set; } }2. 定义数据模型:
public class DataModel { #region Populate Service Data static IList<Order> _orders; static IList<Item> _items; static DataModel() { _orders = new List<Order> { new Order(){ OrderId=1, Customer = "Wendy Wu", Items = new List<Item>()}, new Order(){ OrderId=2, Customer = "John Gu", Items = new List<Item>()}, new Order(){ OrderId=3, Customer = "Balance Yao", Items = new List<Item>()} }; _items = new List<Item> { new Item(){ Product="Chang", Quantity = 4 }, new Item(){ Product="Aniseed Syrup", Quantity=5 }, new Item(){ Product="Toy", Quantity=7 }, new Item(){ Product="Car", Quantity=1 }, new Item(){ Product="Ball", Quantity=6} }; _orders[0].Items.Add(_items[0]); _orders[1].Items.Add(_items[1]); _orders[1].Items.Add(_items[2]); _orders[2].Items.Add(_items[3]); _orders[2].Items.Add(_items[4]); } #endregion public IQueryable<Order> Orders { get { return _orders.AsQueryable<Order>(); } } public IQueryable<Item> Items { get { return _items.AsQueryable<Item>(); } } }3. 实现Service
public class WcfDataService1 : DataService<DataModel> { public static void InitializeService(DataServiceConfiguration config) { config.SetEntitySetAccessRule("*", EntitySetRights.All); config.SetServiceOperationAccessRule("*", ServiceOperationRights.All); config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2; } }
运行,在浏览器里输入 http://localhost:50480/WcfDataService1.svc/Orders 就能看到结果:返回Atom协议的XML数据。
PS:Data Service 通过客户端Request的Accept 来决定返回格式。
比如我用 Fiddler 来查询,使用Accept: application/json 那么返回的就是Json格式的数据:
[WebGet] [SingleResult] public Order TopOrder() { return this.CurrentDataSource.Orders .OrderByDescending(o => o.Items.Sum(i => i.Quantity)).First(); }Data Service 对服务操作有一些具体的限制
static void Main(string[] args) { var svcUri = new Uri("http://localhost:50480/WcfDataService1.svc"); //Query var ctx = new DataSvc.DataModel(svcUri); var order = ctx.Orders.Where(o => o.OrderId == 3).First(); Console.WriteLine(order.Customer); //Query by customer method 'TopOrder' var topOrder = ctx.Execute<DataSvc.Order>(new Uri("/TopOrder", UriKind.Relative)).First(); Console.WriteLine(topOrder.Customer); Console.Read(); }
参考:http://msdn.microsoft.com/zh-cn/library/dd723653.aspx
(2011/7/30 更新)
通过 QueryInterceptorAttribute 和 ChangeInterceptorAttribute 可以对请求进行拦截。
参考:http://msdn.microsoft.com/zh-cn/library/dd744837.aspx