2012-01-09 77 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