通过WCF数据服务公开Azure表存储缺少OData功能

时间:2013-06-28 15:49:13

标签: c# azure wcf-data-services odata azure-table-storage

我有一个WCF数据服务公开Azure表。 这适用于CRUD操作和过滤,但我无法使用像$top=1这样的OData函数。

尝试使用IncludeTotalCount()

上的DataServiceQuery时,我也会遇到错误

WcfDataService1.svc.cs

[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以外的其他功能来获得额外的功能。

在debuging中没有异常抛出服务,但是当使用浏览器中的$top=1命令时,会出现NotImplemented异常。

1 个答案:

答案 0 :(得分:0)

使用wireshark后,我发现对表存储服务的调用是$orberby=partitionkey,rowkey&$top=1

由于azure表存储不支持排序,因此我收到NotImplemented例外。

现在的问题是为什么WCF数据服务在执行操作时会放置orderby