2012-01-09 25 views
1

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

回答

4

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只允许$格式查询选项。

+0

谢谢!你有没有这方面的参考,所以我们可以将它添加到答案? – 2012-01-10 19:41:45

+0

参考规范更新了回复。 – 2012-01-11 08:01:17