WCF数据服务操作强制转换问题
我在从服务操作返回我认为是 IQueryable 的内容时遇到转换问题,但显然是 DbQuery。
更新:我的一些代码(ServiceOp,返回 IEnumerable)基于 Scott Hanselman 的 此处的帖子< /a>:
[WebGet]
public IQueryable<Post> GetPopularPosts()
我已经设置了基本的 WCF 数据服务。根据 更新错误,我将其定义为:
public class Api : DataService<ObjectContext>
而不是来自 TestDb。我将 CreateDataSource() 设置为:
protected override ObjectContext CreateDataSource()
{
var testDb = new TestDb();
var objectContext = ((IObjectContextAdapter)testDb).ObjectContext;
objectContext.ContextOptions.ProxyCreationEnabled = false;
return objectContext;
}
在我的 InitializeService 方法中,我调用:
config.SetServiceOperationAccessRule("GetStuff", ServiceOperationRights.AllRead);
我的服务操作是:
[WebGet]
public IQueryable<Stuff> GetStuff()
{
var context = new TestDb();
return context.Stuff;
}
问题是,即使该方法完成,并且 context.Stuff 中有项目,服务器也会返回错误:
An error occurred while processing this request. Exception has been thrown by the target of an invocation. System.Reflection.TargetInvocationException
...
Unable to cast object of type 'System.Data.Entity.Infrastructure.DbQuery' to type 'System.Linq.IQueryable`
Anythought on这?
I am having a casting issue on returning what I thought was an IQueryable from a service operation, but apparently is a DbQuery.
Update: I am basing some of my code (the ServiceOp, returning IEnumerable) off of Scott Hanselman's post here:
[WebGet]
public IQueryable<Post> GetPopularPosts()
I have setup a basic WCF DataService. As per the update bug, I defined it as:
public class Api : DataService<ObjectContext>
rather than as from TestDb. I setup the CreateDataSource() as:
protected override ObjectContext CreateDataSource()
{
var testDb = new TestDb();
var objectContext = ((IObjectContextAdapter)testDb).ObjectContext;
objectContext.ContextOptions.ProxyCreationEnabled = false;
return objectContext;
}
In my InitializeService method, I call:
config.SetServiceOperationAccessRule("GetStuff", ServiceOperationRights.AllRead);
And my service operation is:
[WebGet]
public IQueryable<Stuff> GetStuff()
{
var context = new TestDb();
return context.Stuff;
}
The problem is that even though the method completes, and context.Stuff has items in it, the server returns an error:
An error occurred while processing this request. Exception has been thrown by the target of an invocation. System.Reflection.TargetInvocationException
...
Unable to cast object of type 'System.Data.Entity.Infrastructure.DbQuery' to type 'System.Linq.IQueryable`
Any thoughts on this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请记住,WCF 服务无法返回接口类型!它们需要是具体的 DataContract 类型。尤其是 IQueryable,因为 IQueryable 对象实际上是“如何调用数据”而不是实际的数据。
尝试返回一个列表:
编辑
显然你可以返回 IEnumerable,但不能返回 IQueryable。鉴于我对 IQueryable 是什么的解释,这是有道理的。
Keep in mind that a WCF service cannot return interface types! They need to be concrete DataContract-ed types. Especially IQueryable since an IQueryable object is really a "how to call data" instead of actually data.
Try returning a List instead:
EDIT
Apparently you CAN return IEnumerable, but not IQueryable. Which makes sense given my explanation of what IQueryable is.