为什么我的WCF数据服务集合没有列出?

时间:2011-07-31 21:54:53

标签: .net wcf-data-services

我正在尝试制作一个简单的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>

看到该集合未列出。我做错了什么?

1 个答案:

答案 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;
    }
}