1
我有一個暴露Azure表的WCF數據服務。 此工作適用於CRUD操作和過濾,但我無法使用OData功能,如$top=1
。通過WCF數據服務暴露Azure表存儲缺少OData函數
我試圖在DataServiceQuery
的WcfDataService1.svc.cs
使用IncludeTotalCount()
時也出現錯誤:
[System.ServiceModel.ServiceBehavior(IncludeExceptionDetailInFaults = true)]
public class WcfDataService1 : DataService<MenuDataServiceContext>
{
// This method is called only once to initialize service-wide policies.
public static void InitializeService(DataServiceConfiguration config)
{
// TODO: set rules to indicate which entity sets and service operations are visible, updatable, etc.
config.SetEntitySetAccessRule("*", EntitySetRights.All);
config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
config.UseVerboseErrors = true;
}
}
的MenuDataServiceContext
:
public class MenuDataServiceContext : IUpdatable
{
private readonly AzureTableContext tableContext = new AzureTableContext();
public IQueryable<MenuItemRow> Menu
{
get
{
var retval = this.tableContext.CreateQuery<MenuItemRow>("Menu").AsTableServiceQuery();
return retval;
}
}
public void AddReferenceToCollection(object targetResource, string propertyName, object resourceToBeAdded)
{
throw new NotImplementedException();
}
public void ClearChanges()
{
return;
}
public object CreateResource(string containerName, string fullTypeName)
{
var entity = new MenuItemRow();
// Add the entity to table context.
this.tableContext.AddObject(containerName, entity);
return entity;
}
public void DeleteResource(object targetResource)
{
var person = targetResource as MenuItemRow;
if (person == null)
{
throw new DataServiceException(400, "Invalid object. Object must be a Person");
}
this.tableContext.DeleteObject(person);
this.tableContext.SaveChanges();
}
public object GetResource(IQueryable query, string fullTypeName)
{
var tableQuery = query as IQueryable<MenuItemRow>;
if (tableQuery == null)
{
throw new DataServiceException(400, "Invalid query.");
}
return tableQuery.First();
}
public object GetValue(object targetResource, string propertyName)
{
var person = (MenuItemRow)targetResource;
return typeof(MenuItemRow).GetProperty(propertyName).GetValue(person);
}
public void RemoveReferenceFromCollection(object targetResource, string propertyName, object resourceToBeRemoved)
{
throw new NotImplementedException();
}
public object ResetResource(object resource)
{
throw new NotImplementedException();
}
public object ResolveResource(object resource)
{
return resource;
}
public void SaveChanges()
{
this.tableContext.SaveChanges();
}
public void SetReference(object targetResource, string propertyName, object propertyValue)
{
throw new NotImplementedException();
}
public void SetValue(object targetResource, string propertyName, object propertyValue)
{
// The Partition/RowKey should not be modified.
if (propertyValue != null && propertyName != "PartitionKey" && propertyName != "RowKey")
{
var person = (MenuItemRow)targetResource;
typeof(MenuItemRow).GetProperty(propertyName).SetValue(person, propertyValue, null);
this.tableContext.UpdateObject(person);
}
}
}
我覺得像MenuDataServiceContext
應該實現其他的東西比IUpdatable
獲得額外的功能。
在調試時沒有任何例外情況引發服務,但在瀏覽器中使用$top=1
命令時,會出現NotImplemented
例外。