.NET Web应用DbContext实例引发System.ObjectDisposedException问题排查
ASP.NET Web应用串口扫码后DbContext已释放异常问题解决求助
开发了一个用于验证的ASP.NET Web应用,通过串口连接扫码器,扫描二维码后获取数据并刷新页面使用该数据。当前遇到的问题是:扫码后数据在控制器的mySerialPort_Data方法中接收,调用SelectedPN方法加载刷新数据时,持续出现DbContext实例已释放的错误。
相关代码
mySerialPort_Data方法
public void mySerialPort_Data(object sender, SerialDataReceivedEventArgs e) { try { string data = _serialPort.ReadExisting(); barcode = data.Split(";"); codeValue = barcode[0].Substring(barcode[0].IndexOf(":") +1); SelectedPN(pn_No); } catch (IOException ex) { Console.WriteLine(ex); } }
SelectedPN方法
public ActionResult SelectedPN(String nameobj) { pn_No= nameobj; _serialPort.WriteTimeout = 500; _serialPort.DataReceived += new SerialDataReceivedEventHandler(mySerialPort_Data); if (!_serialPort.IsOpen) { try { _serialPort.Open(); } catch (IOException ex) { Console.WriteLine(ex); } } ViewBag.check9 = codeValue; List <double> newList = new List<double>(); var num = 0; var bill = 4; var categoryFromDb = _db.Categories1.Where(j => j.MaterialNumber == nameobj); foreach (var obj in categoryFromDb) { newList.Add(obj.ComponentNumber); } num = newList.Count; var duplicates = newList.GroupBy(x => x) .SelectMany(g => g.Skip(1)) .Distinct() .ToList(); ViewBag.check1 = check; //testlist.Add(Convert.ToDouble(data1.X2)); ViewBag.check1 = check; ViewBag.check2 = "background-color: #00FF00;"; ViewBag.check3 = 3; ViewBag.check4 = num; //ViewBag.check5 = ''; ViewBag.update = num; changeValue = num; ViewBag.check6 = testlist; ViewBag.check7 = duplicates; return View( categoryFromDb ); }
错误信息
System.ObjectDisposedException: 'Cannot access a disposed context instance. A common cause of this error is disposing a context instance that was resolved from dependency injection and then later trying to use the same context instance elsewhere in your application. This may occur if you are calling 'Dispose' on the context instance, or wrapping it in a using statement. If you are using dependency injection, you should let the dependency injection container take care of disposing context instances. Object name: 'ApplicationDbContext'.'
已尝试的方案
- 了解到DbContext默认生命周期为Scoped,改为
ServiceLifetime.Transient,问题仍未解决。 - 尝试使用
using创建ApplicationDbContext实例,出现"未配置数据库提供程序"的错误,相关错误信息如下:
InvalidOperationException: No database provider has been configured for this DbContext. A provider can be configured by overriding the 'DbContext.OnConfiguring' method or by using 'AddDbContext' on the application service provider. If 'AddDbContext' is used, then also ensure that your DbContext type accepts a DbContextOptions<TContext> object in its constructor and passes it to the base constructor for DbContext. Microsoft.EntityFrameworkCore.Internal.DbContextServices.Initialize(IServiceProvider scopedProvider, DbContextOptions contextOptions, DbContext context) ...
- 最终堆栈跟踪:
Microsoft.EntityFrameworkCore.dll!Microsoft.EntityFrameworkCore.DbContext.CheckDisposed() Unknown Microsoft.EntityFrameworkCore.dll!Microsoft.EntityFrameworkCore.DbContext.ContextServices.get() Unknown ... System.Private.CoreLib.dll!System.Threading.Thread.StartCallback() Unknown
现寻求解决该DbContext已释放异常的方案。
内容的提问来源于stack exchange,提问作者joeOla
相关产品推荐
相关产品推荐

