ObjectContext 实例已被释放,不能再用于需要连接的操作
我正在开发 WCF 数据服务。当我尝试从客户端访问它时,出现以下异常:
ObjectContext 实例已被释放,不能再用于需要连接的操作。
代码:
[WebGet]
public IQueryable<Student> GetUsersByClassId(string classId)
{
Check.Argument.IsNotEmptyOrNull(classId, "classId");
using (var db = new schoolContext(connectionString))
{
((IObjectContextAdapter)db).ObjectContext.ContextOptions.ProxyCreationEnabled = false;
((IObjectContextAdapter)db).ObjectContext.ContextOptions.LazyLoadingEnabled = false;
var studentQry = from s in db.Student.Include("Class")
where s.Class.Id == classId
select s;
if(studentQry == null)
return new List<Student>().AsQueryable();
else
return studentQry;
}
I am developing a WCF Data Service. when I try accessing it from the client side, I get this exception :
The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.
Code:
[WebGet]
public IQueryable<Student> GetUsersByClassId(string classId)
{
Check.Argument.IsNotEmptyOrNull(classId, "classId");
using (var db = new schoolContext(connectionString))
{
((IObjectContextAdapter)db).ObjectContext.ContextOptions.ProxyCreationEnabled = false;
((IObjectContextAdapter)db).ObjectContext.ContextOptions.LazyLoadingEnabled = false;
var studentQry = from s in db.Student.Include("Class")
where s.Class.Id == classId
select s;
if(studentQry == null)
return new List<Student>().AsQueryable();
else
return studentQry;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我怀疑问题出在标记为
...
的代码中,而您没有显示该代码。在返回查询之前,请务必充分评估您的查询。例如,不要只是这样做:
尝试这样做:
编辑:
现在您已经更改了您的问题以反映您正在返回代码>,有一个单独的问题。通过从您的方法返回
IQueryable
而不是IEnumerable
IQueryableIQueryable
,您建议可以直接在服务器端“查询”该类型。然而,WCF 正在通过网络对其进行序列化,并且需要使用具体的实现类型。允许
IEnumerable
,但不允许IQueryable
。您应该将其切换为可以完全评估的类型,以便可以正确传递结果。I suspect the problem is in the code marked
...
, which you're not showing.Make sure to fully evaluate your query before returning it. For example, instead of just doing:
Try doing:
Edit:
Now that you've changed your question to reflect that you're returning
IQueryable<T>
instead ofIEnumerable<T>
, there is a separate issue. By returningIQueryable<T>
from your method, you're suggesting that the type can be "queried" directly on the server side.However, WCF is serializing this across the wire, and needs to use a concrete implementation type.
IEnumerable<T>
is allowed, but notIQueryable<T>
. You should switch this to a type that will evaluate fully so the results can be passed correctly.这是因为Using语句。当您的函数完成执行时,它会释放对象上下文。当枚举结果 IQueryable 时,它会尝试使用已处置的对象,因此您会收到异常。删除使用,让您的服务实现 IDisposable 并在 IDisposable.Dispose() 中处置您的对象上下文
This is because of Using statement. When your function finishes execution, it disposes object context. When the result IQueryable is enumerated, it tries to use disposed object, so you get the Exception. Remove using, let your sevice implement IDisposable and dispose your object context in IDisposable.Dispose()
客户端可能正在尝试访问子属性,例如,如果student.classes是另一个实体,并且您尝试在客户端上访问它。需要指定包含任何子实体
Its probably that the client is trying to access a sub property, eg if student.classes is another entity and you try to access it on client. Need to specify include on any sub entities
这是你的使用造成的。 DataContext 在 linq 查询计算之前被释放。您可以返回 StudentQry.ToList() 并查看是否有效。如果没有,请尝试
最后,它很难看,但如果您不使用 using,则不会遇到此问题。它最终应该通过垃圾收集来处理和清理。
It is your using that is causing it. The DataContext is being disposed before the linq query evaluates. You can return studentQry.ToList() and see if that works. If that doesn't, try
Finally, it is ugly, but if you don't use a using, you will not have this issue. It should get disposed and cleaned up by garbage collection eventually.