问题来自博问的一个提问 .net core 多线程数据保存的时候DbContext被释放 。

TCPService 通过构造函数注入了 ContentService , ContentService 的实例依赖了 AppDbContext (继承自 EF Core 的 DbContext)。在 TCPService 中通过 Thread.Start 启动了一个新的线程执行了 TCPService 中的 Receive 方法,在 Receive 方法中通过 ContentService 进行保存数据库的操作,而在访问 AppDbContext 的实例时出现对象已被 Disposed 的错误。

Object name: 'AppDbContext'. --->System.ObjectDisposedException: Cannot access a disposed object. A common cause of this error is disposing a context 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, or wrapping the context in a using statement. If you are using dependency injection, you should let the dependency injection container take care of disposing context instances.

针对这个问题,尝试改为通过构造函数注入 IServiceProvider ,在 TCPService.Receive (在新线程中执行的方法)中通过 IServiceScope 解析

using (var scope = _serviceProvider.CreateScope())
{
var contentService = scope.ServiceProvider.GetRequiredService<ContentService>();
//...
}

结果发现 IServiceProvider 也被 Disposed

System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'IServiceProvider'.

由此可以推断在 ASP.NET Core 中在新建的线程中无法通过依赖注入的方式访问实现了 IDisposable 接口的对象(单例对象除外,但实现 IDisposable 接口的类型通常不会注册为单例),也就是只要请求一结束,实现 IDisposable 接口的对象的 Dispose 方法就会被调用。

那如何解决这个问题呢?

1)最下下策的解决方法是将 DbContext 注册为单例

services.AddDbContext<AppDbContext>(options => { }, ServiceLifetime.Singleton);

它会带来很多副作用,不考虑。

2)在保存数据至数据库的实现方法中基于 DbContextOptions (它是单例)手工 new AppDbContext ,用这个 DbContext 实例进行保存操作。

public class ContentService : Repository<Content>
{
private readonly DbContextOptions _options; public ContentService(AppDbContext Context, DbContextOptions options) : base(Context)
{
_options = options;
} public override async Task<bool> SaveAsync(Content entity)
{
using (var context = new AppDbContext(_options))
{
context.Set<Content>().Add(entity);
return await context.SaveChangesAsync() > ;
}
}
}

实测有效。

ASP.NET Core 新建线程中使用依赖注入的问题的更多相关文章

  1. ASP.NET Core - 在ActionFilter中使用依赖注入

    上次ActionFilter引发的一个EF异常,本质上是对Core版本的ActionFilter的知识掌握不够牢固造成的,所以花了点时间仔细阅读了微软的官方文档.发现除了IActionFilter.I ...

  2. ASP.NET Core 1.0 中的依赖项管理

    var appInsights=window.appInsights||function(config){ function r(config){t[config]=function(){var i= ...

  3. 在.NET Core控制台程序中使用依赖注入

    之前都是在ASP.NET Core中使用依赖注入(Dependency Injection),昨天遇到一个场景需要在.NET Core控制台程序中使用依赖注入,由于对.NET Core中的依赖注入机制 ...

  4. dotnet core在Task中使用依赖注入的Service/EFContext

    C#:在Task中使用依赖注入的Service/EFContext dotnet core时代,依赖注入基本已经成为标配了,这就不多说了. 前几天在做某个功能的时候遇到在Task中使用EF DbCon ...

  5. Asp.Net Core 第04局:依赖注入

    总目录 前言 本文介绍Asp.Net Core中默认的依赖注入(DI)模式. 环境 1.Visual Studio 2017 2.Asp.Net Core 2.2 开局 第一手:依赖注入说明 1.一个 ...

  6. ASP.NET Core应用的7种依赖注入方式

    ASP.NET Core框架中的很多核心对象都是通过依赖注入方式提供的,如用来对应用进行初始化的Startup对象.中间件对象,以及ASP.NET Core MVC应用中的Controller对象和V ...

  7. ASP.NET Core 配置文件(无处不在的依赖注入)

    前烟: .NET Core 中取消了以往的 XML 节点配置文件,改用了 *.json 格式. 在 Startup.cs 文件中,构造方法 build appsetting.json 文件, 本文主要 ...

  8. asp.net core 系列之Dependency injection(依赖注入)

    这篇文章主要讲解asp.net core 依赖注入的一些内容. ASP.NET Core支持依赖注入.这是一种在类和其依赖之间实现控制反转的一种技术(IOC). 一.依赖注入概述 1.原始的代码 依赖 ...

  9. ASP.NET Core MVC 控制器创建与依赖注入

    本文翻译自<Controller activation and dependency injection in ASP.NET Core MVC>,由于水平有限,故无法保证翻译完全准确,欢 ...

随机推荐

  1. linux 使用的部分命令

    搜索所有运行着的线程 ps -A | grep apt-get 你会得到类似下面的输出: root ? Ss : : /bin/sh /usr/lib/apt/apt.systemd.daily _a ...

  2. CommonJs、AMD、CMD模块化规范

    /** * CommonJS 模块化规范 * CommonJS规范加载模块是同步的,也就是说,只有加载完成,才能执行后面的操作 */ /*-------Node.js遵循Commonjs规范----- ...

  3. Ubuntu shutdown now 关机后 开机黑屏

    一重装gdm3      失败 sudo apt-get remove --purge nvidia-* # 卸载nvidia相关组件 sudo apt purge gdm gdm3 # 卸载gdm和 ...

  4. HTML&javaSkcript&CSS&jQuery&ajax(二)

    一.HTML 1.标签<a href="http:www.baidu.com">This is a link </a>         <img sr ...

  5. jquery----Ajax补充

    jquery实现ajax请求 <script> //$.ajax的两种使用方式: //$.ajax(settings); //$.ajax(url,[settings]); $(" ...

  6. vue :基本语法格式

    <!DOCTYPE html><html><head> <meta charset="utf-8"> <title>Vu ...

  7. VisualSVN Server如何安装和使用

    首先来下载和搭建SVN服务器. 现在Subversion已经迁移到apache网站上了,下载地址: http://subversion.apache.org/packages.html 这是二进制文件 ...

  8. scss文件使用笔记

    1.编写兼容性代码 例如透明度,兼容IE @mixin mOpacity($o){ opacity:$o/100; filter:alpha(opacity=$o); } //引用 .box{ @in ...

  9. Java接口自动化测试之HTTPClient学习(四)

    pom.xml  文件中dependency <dependencies> <dependency> <groupId>org.testng</groupId ...

  10. [SDOI2018]原题识别

    题解: ..感觉挺烦得 而且我都没有注意到树随机这件事情.. 就写个30分的莫队.. #include <bits/stdc++.h> using namespace std; #defi ...