C# 查询选项无法应用于请求的资源
我用WPF客户端创建了一个非常简单的.NET4.0Web项目 web解决方案有一个WCF数据服务,服务操作返回一个C# 查询选项无法应用于请求的资源,c#,wpf,wcf,wcf-data-services,C#,Wpf,Wcf,Wcf Data Services,我用WPF客户端创建了一个非常简单的.NET4.0Web项目 web解决方案有一个WCF数据服务,服务操作返回一个IQueryable WPF客户端引用该服务并直接调用服务操作,直接在查询上使用CreateQuery()和.Take() 很遗憾,我收到以下错误消息: Query options $orderby, $inlinecount, $skip and $top cannot be applied to the requested resource. 如果我在浏览器中使用http://
IQueryable
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; }
}
}
命名空间WPFTestApplication1
{
[System.ServiceModel.ServiceBehavior(IncludeExceptionDetailInFaults=true)]
公共类WcfDataService1:数据服务
{
公共静态void InitializeService(DataServiceConfiguration配置)
{
config.SetEntitySetAccessRule(“*”,EntitySetRights.AllRead);
config.SetServiceOperationAccessRule(“*”,ServiceOperationRights.All);
config.DataServiceBehavior.MaxProtocolVersion=DataServiceProtocolVersion.V2;
}
[WebGet]
公共IQueryable GetStrings()
{
变量字符串=新字符串[]
{
“aa”,
“bb”,
“抄送”,
“dd”,
“ee”,
“ff”,
“gg”,
"hh",,
“ii”,
“jj”,
“kk”,
“ll”
};
var queryableStrings=strings.AsQueryable();
返回queryableStrings;
}
}
公营部门
{
公共int ID{get;set;}
}
公共类DummyDataSource
{
//虚拟源,只是为了让WcfDataService1工作
公共IQueryable实体{get;set;}
}
}
MainWindow.xaml.cs:(WPF)
public主窗口()
{
初始化组件();
ServiceReference1.DummyDataSource ds=新ServiceReference1.DummyDataSource(新Uri(“http://localhost:20789/WcfDataService1.svc/"));
var strings=ds.CreateQuery(“GetStrings”).Take(3);
//枚举时,此处发生异常
foreach(字符串中的var str)
{
MessageBox.Show(str);
}
}
WCF数据服务(以及OData)不支持对基本类型或复杂类型集合的查询操作。服务操作不是IQueryable,而是IEnumerable。
您可以向服务操作中添加参数,以仅返回指定数量的结果
在规范中描述如下:
URI列表-URI13是一个返回基元类型集合的服务操作。
然后是描述系统查询选项的页面:
在底部,该表描述了可用于哪个uri类型的查询选项。URI13只允许$format查询选项。WCF数据服务(以及OData)不支持对基本类型或复杂类型集合的查询操作。服务操作不是IQueryable,而是IEnumerable。
您可以向服务操作中添加参数,以仅返回指定数量的结果
在规范中描述如下:
URI列表-URI13是一个返回基元类型集合的服务操作。
然后是描述系统查询选项的页面:
在底部,该表描述了可用于哪个uri类型的查询选项。URI13只允许$format查询选项。谢谢!您对此有任何参考资料,以便我们将其添加到答案中吗?更新了对规范的参考,谢谢!您是否对此有任何参考,以便我们可以将其添加到答案中?使用规范的参考更新了响应。
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);
}
}