Reputation: 423
I've created a very simple .NET 4.0 Web project, with an WPF client.
The web solution has a WCF Data Service with a service operation returning an IQueryable<string>
.
The WPF client references that service and calls the service operation directly, using CreateQuery()
and .Take()
directly on the query.
Unfortunately, I get the following error message:
Query options $orderby, $inlinecount, $skip and $top cannot be applied to the requested resource.
If I view the service in a browser using http://localhost:20789/WcfDataService1.svc/GetStrings()?$top=3
, I get the same error.
Any ideas ? Let me know if I need to upload the solution somewhere.
Thanks!
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);
}
}
Upvotes: 1
Views: 4115
Reputation: 13320
WCF Data Services (and OData as well) doesn't support query operation on collections of primitive or complex types. The service operation is not seen as IQueryable but just as IEnumerable. You can add a parameter to the service operation to only return you a specified number of results.
In the spec it's described like this: List of URIs - URI13 is a service operation returning a collection of primitive types. http://msdn.microsoft.com/en-us/library/dd541212(v=PROT.10).aspx And then the page describing system query options: http://msdn.microsoft.com/en-us/library/dd541320(v=PROT.10).aspx At the bottom the table describes which query options are available for which uri type. URI13 only allows the $format query option.
Upvotes: 5