我試圖使用WCF的OData服務5.2發佈服務方法,應返回派生類型的集合(POCO對象):WCF OData服務法不變類型集合
public class WcfDataService1 : DataService<MyProvider>
{
public static void InitializeService(DataServiceConfiguration config)
{
config.SetEntitySetAccessRule("*", EntitySetRights.All);
config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V3;
config.UseVerboseErrors = true;
config.SetServiceActionAccessRule("*", ServiceActionRights.Invoke);
}
[WebGet]
public List<Vehicle> GetStuff()
{
var c = new List<Vehicle>()
{
new Motorbike() {Id = 1},
new Car() {Id = 2}
};
return c;
}
}
public class MyProvider
{
public IQueryable<Car> Cars { get; set; }
public IQueryable<Motorbike> Motorbikes { get; set; }
}
[DataServiceEntity]
[DataServiceKey("Id")]
public abstract class Vehicle
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Motorbike : Vehicle
{
public int MaxSpeed { get; set; }
}
public class Car: Vehicle
{
public int NumberOfTyres { get; set; }
}
我收到以下例外: 服務操作'System.Collections.Generic.List`1 [ODataV3.Vehicle] GetStuff()'生成類型爲'ODataV3.Vehicle'的實例,但需要爲該類型設置單個實體集。
如果我添加設置爲我的上下文中的實體,它會導致不同的異常: 住宅「車」和「車」是種「ODataV3.Vehicle」和「ODataV3.Car」並輸入「ODataV3的IQueryable的。車輛'是類型'ODataV3.Car'的祖先。請確保每個類型層次結構只有一個IQueryable屬性。
沒有服務方法,我得到正確的元數據與預期的基本類型屬性。
是否有任何方法來實現我的GetStuff()方法並擁有所有必要的OData元數據?我試圖結合base/inherited類型的屬性,但沒有成功。 我不會在WebAPI項目中手動編寫元數據(使用其他OData模塊進行擴展)。
我錯過了,我看到WebAPI OData 0.2.0 alpha中的WebAPI Model binder API(http://blogs.msdn.com/b/alexj/archive/2012/11/02/odata-in-webapi -microsoft-asp-net-web-api-odata-0-2-0-alpha-release.aspx),我將使用WCF數據服務 – Robesz