查询选项无法应用于请求的资源

本文关键字:请求 资源 应用于 选项 查询 | 更新日期: 2023-09-27 18:19:47

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

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);
        }
    }

查询选项无法应用于请求的资源

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查询选项。

相关文章: