【问题标题】:Query options cannot be applied to the requested resource查询选项无法应用于请求的资源
【发布时间】:2012-01-09 18:01:07
【问题描述】:

我创建了一个非常简单的 .NET 4.0 Web 项目,带有 WPF 客户端。

Web 解决方案具有 WCF 数据服务,服务操作返回 IQueryable<string>

WPF 客户端引用该服务并直接在查询中使用CreateQuery().Take() 调用服务操作。

很遗憾,我收到以下错误消息:

Query options $orderby, $inlinecount, $skip and $top cannot be applied to the requested resource.

如果我在浏览器中使用http://localhost:20789/WcfDataService1.svc/GetStrings()?$top=3 查看服务,我会收到同样的错误。

有什么想法吗?如果我需要在某处上传解决方案,请告诉我。

谢谢!

WcfDataService1.svc.cs:

namespace WPFTestApplication1
{
    [System.ServiceModel.ServiceBehavior(IncludeExceptionDetailInFaults = true)]
    public class WcfDataService1 : DataService<DummyDataSource>
    {
        public static void InitializeService(DataServiceConfiguration config)
        {
            config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
            config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);
            config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
        }

        [WebGet]
        public IQueryable<string> GetStrings()
        {
            var strings = new string[]
            {
            "aa",
            "bb",
            "cc",
            "dd",
            "ee",
            "ff",
            "gg",
            "hh",
            "ii",
            "jj",
            "kk",
            "ll"
            };
            var queryableStrings = strings.AsQueryable();
            return queryableStrings;
        }
    }

    public class DummyEntity
    {
        public int ID { get; set; }
    }

    public class DummyDataSource
    {
        //dummy source, just to have WcfDataService1 working
        public IQueryable<DummyEntity> Entities { get; set; }
    }
}

MainWindow.xaml.cs:(WPF)

    public MainWindow()
    {
        InitializeComponent();

        ServiceReference1.DummyDataSource ds = new ServiceReference1.DummyDataSource(new Uri("http://localhost:20789/WcfDataService1.svc/"));
        var strings = ds.CreateQuery<string>("GetStrings").Take(3);

        //exception occurs here, on enumeration
        foreach (var str in strings)
        {
            MessageBox.Show(str);
        }
    }

【问题讨论】:

    标签: c# wpf wcf wcf-data-services


    【解决方案1】:

    WCF 数据服务(以及 OData)不支持对原始或复杂类型的集合进行查询操作。服务操作不被视为 IQueryable,而只是 IEnumerable。 您可以向服务操作添加参数,以仅返回指定数量的结果。

    在规范中是这样描述的: URI 列表 - URI13 是返回原始类型集合的服务操作。 http://msdn.microsoft.com/en-us/library/dd541212(v=PROT.10).aspx 然后是描述系统查询选项的页面: http://msdn.microsoft.com/en-us/library/dd541320(v=PROT.10).aspx 表格底部描述了哪些查询选项可用于哪些 uri 类型。 URI13 只允许 $format 查询选项。

    【讨论】:

    • 谢谢!您对此有任何参考,因此我们可以将其添加到答案中?
    • 更新了对规范的引用。
    猜你喜欢
    • 1970-01-01
    • 2022-09-24
    • 2016-02-07
    • 1970-01-01
    • 2017-07-09
    • 1970-01-01
    • 2015-09-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多