• WebService用Linq查询数据返回列表对象报错:Cannot access a disposed object
  • 发布于 2个月前
  • 258 热度
    1 评论

我用如下Linq查询语句返回一个对象供WebService时,系统报错:System.ObjectDisposedException: Cannot access a disposed object.


 public static PaymentManageRequest GetPaymentManageRequest(int requestID)
        {
            using (PaymentManageDataContext dc = new PaymentManageDataContext(BaseBLL.ConnectionString))
            {
                var query = from n in dc.PaymentManageRequests where n.RequestID == requestID select n;
                return query.FirstOrDefault();
            }
        }
具体报错信息如下:



System.InvalidOperationException: There was an error generating the XML document. ---> System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'DataContext accessed after Dispose.'.
   at System.Data.Linq.DataContext.GetTable(Type type)
   at System.Data.Linq.CommonDataServices.GetDataMemberQuery(MetaDataMember member, Expression[] keyValues)
   at System.Data.Linq.CommonDataServices.DeferredSourceFactory`1.ExecuteKeyQuery(Object[] keyValues)
   at System.Data.Linq.CommonDataServices.DeferredSourceFactory`1.Execute(Object instance)
   at System.Data.Linq.CommonDataServices.DeferredSourceFactory`1.DeferredSource.GetEnumerator()
   at System.Data.Linq.EntitySet`1.Load()
   at System.Data.Linq.EntitySet`1.get_Count()
   at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write5_PaymentManageRequest(String n, String ns, PaymentManageRequest o, Boolean isNullable, Boolean needType)
   at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write7_PaymentManageRequest(Object o)
   at Microsoft.Xml.Serialization.GeneratedAssembly.PaymentManageRequestSerializer.Serialize(Object objectToSerialize, XmlSerializationWriter writer)
   at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
   --- End of inner exception stack trace ---
   at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
   at System.Xml.Serialization.XmlSerializer.Serialize(TextWriter textWriter, Object o, XmlSerializerNamespaces namespaces)
   at System.Xml.Serialization.XmlSerializer.Serialize(TextWriter textWriter, Object o)
   at System.Web.Services.Protocols.XmlReturnWriter.Write(HttpResponse response, Stream outputStream, Object returnValue)
   at System.Web.Services.Protocols.HttpServerProtocol.WriteReturns(Object[] returnValues, Stream outputStream)
   at System.Web.Services.Protocols.WebServiceHandler.WriteReturns(Object[] returnValues)
   at System.Web.Services.Protocols.WebServiceHandler.Invoke()


这种错误是什么意思,该怎么解决?

用户评论