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

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

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

尝试在 DataServiceQuery 上使用 IncludeTotalCount() 时,我也会遇到错误

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 异常。

最佳答案

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

由于 Azure 表存储不支持排序,因此我收到了 NotImplemented 异常。

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

关于c# - 通过 WCF 数据服务公开 Azure 表存储缺少 OData 功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17368714/

相关文章:

azure - 使用哪些 GA API 来访问客户交互原始数据?

azure - 将 Azure 服务总线与 Android 连接

c# - WCF 数据服务/ Entity Framework : Logging client's username, 身份验证、授权

c# - 在 GridView 的空单元格中显示消息

c# - 使用 LINQ 获取 1-2-3 分数并将其绑定(bind)到 View

javascript - AngularJS - MVC ASP.NET 目录结构

wcf - 如何禁用 WCF 数据服务的身份验证方案

c# - 在 2 个已知值之间找到一个字符串

azure - 为什么 Azure 在我的 cspkg 中找不到 IISConfigurator.exe?

azure - 通过 Azure 服务总线进行身份验证的 WCF 数据服务