我正在尝试制作一个简单的WCF数据服务。我能够成功访问scv
文件,但我的Cars
集合未列出。我scv
的内容是:
[DataServiceKey("VIN")]
public class Car
{
public String VIN { get; set; }
public String Make { get; set; }
public String Model { get; set; }
public int Year { get; set; }
}
public class CarService : DataService<Car>
{
public IQueryable<Car> Cars
{
get
{
return (new List<Car> {
new Car { VIN = "ABC123", Make = "Ford", Model = "F-250", Year = 2000 },
new Car { VIN = "ABC124", Make = "BMW", Model = "Z-3", Year = 2005 },
new Car { VIN = "ABC125", Make = "Audi", Model = "TT", Year = 2008 }
}).AsQueryable();
}
}
public static void InitializeService(DataServiceConfiguration config)
{
config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
}
}
在浏览器上访问此服务时,我得到:
<?xml version="1.0" encoding="iso-8859-1" standalone="yes"?>
<service xml:base="http://localhost:60730/CarService.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>
看到该集合未列出。我做错了什么?
答案 0 :(得分:4)
请改为尝试:
[DataServiceKey("VIN")]
public class Car
{
public String VIN { get; set; }
public String Make { get; set; }
public String Model { get; set; }
public int Year { get; set; }
}
public class MyContainer
{
public IQueryable<Car> Cars
{
get
{
return (new List<Car> {
new Car { VIN = "ABC123", Make = "Ford", Model = "F-250", Year = 2000 },
new Car { VIN = "ABC124", Make = "BMW", Model = "Z-3", Year = 2005 },
new Car { VIN = "ABC125", Make = "Audi", Model = "TT", Year = 2008 }
}).AsQueryable();
}
}
}
public class CarService : DataService<MyContainer>
{
public static void InitializeService(DataServiceConfiguration config)
{
config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
}
}