1

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

尝试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

调试时服务中没有抛出异常,但是$top=1从浏览器使用命令时,出现NotImplemented异常。

4

1 回答 1

0

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

由于 azure table storage 不支持排序,所以我遇到了NotImplemented异常。

现在的问题是为什么 WCF 数据服务orderby在拍摄时会打开。

于 2013-07-05T15:43:30.267 回答