WCF数据服务操作强制转换问题

发布于 2024-10-24 03:42:20 字数 1644 浏览 1 评论 0原文

我在从服务操作返回我认为是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

硪扪都還晓 2024-10-31 03:42:20

请记住,WCF 服务无法返回接口类型!它们需要是具体的 DataContract 类型。尤其是 IQueryable,因为 IQueryable 对象实际上是“如何调用数据”而不是实际的数据。

尝试返回一个列表:

[WebGet]
public List<Stuff> GetStuff()
{
    var context = new TestDb();
    return context.Stuff.ToList();
}

编辑

显然你可以返回 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:

[WebGet]
public List<Stuff> GetStuff()
{
    var context = new TestDb();
    return context.Stuff.ToList();
}

EDIT

Apparently you CAN return IEnumerable, but not IQueryable. Which makes sense given my explanation of what IQueryable is.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文