无法从 WCF 数据服务返回自定义类
我正在尝试从 WCF 数据服务返回自定义类。我的自定义类是:
[DataServiceKey("ID")]
public class Applist {
public int ID { get; set; }
public string Name { get; set; }
}
我的数据服务看起来像:
public static void InitializeService(IDataServiceConfiguration config)
{
config.RegisterKnownType(typeof(Applist));
config.SetEntitySetAccessRule("*", EntitySetRights.All);
config.SetServiceOperationAccessRule("GetApplications", ServiceOperationRights.AllRead);
}
[WebGet]
public IQueryable<Applist> GetApplications() {
var result = (from p in this.CurrentDataSource.Applications
orderby p.ApplicationName
group p by p.ApplicationName into g
select new Applist { ID = g.Min(p => p.id), Name = g.Key });
return result.AsQueryable();
}
但是,当我运行该服务时,它给了我一个错误:
Request Error Request Error The server encountered an error processing the request.
The exception message is 'Unable to load metadata for return type
'System.Linq.IQueryable`1[ApplicationService.Applist]' of method
'System.Linq.IQueryable`1[ApplicationService.Applist] GetApplications()'
相同的查询在 LINQPad 中运行得非常好。
I am trying to return a custom class from my WCF data service. My custom class is:
[DataServiceKey("ID")]
public class Applist {
public int ID { get; set; }
public string Name { get; set; }
}
My data service looks like:
public static void InitializeService(IDataServiceConfiguration config)
{
config.RegisterKnownType(typeof(Applist));
config.SetEntitySetAccessRule("*", EntitySetRights.All);
config.SetServiceOperationAccessRule("GetApplications", ServiceOperationRights.AllRead);
}
[WebGet]
public IQueryable<Applist> GetApplications() {
var result = (from p in this.CurrentDataSource.Applications
orderby p.ApplicationName
group p by p.ApplicationName into g
select new Applist { ID = g.Min(p => p.id), Name = g.Key });
return result.AsQueryable();
}
However when I run the service, it gives me an error:
Request Error Request Error The server encountered an error processing the request.
The exception message is 'Unable to load metadata for return type
'System.Linq.IQueryable`1[ApplicationService.Applist]' of method
'System.Linq.IQueryable`1[ApplicationService.Applist] GetApplications()'
The same query runs perfectly fine in LINQPad.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请参考下面的博客。它详细解释了这种情况和可能的解决方案:
http://samuelmueller.com/ 2009/11/在wcf-data-services中使用投影和dtos/
Refer to the below blog. It explains this scenario and a possible solution in detail:
http://samuelmueller.com/2009/11/working-with-projections-and-dtos-in-wcf-data-services/