【发布时间】:2019-01-08 04:32:29
【问题描述】:
我的 ASP.NET MVC 应用程序中有自托管 WebAPI。我想在执行我的 API 操作之一时执行一些异步操作。 异步操作依赖于 DbContext 以及其他一些依赖项。
以下是我的简单注射器配置。
public class SimpleInjectorIntegrator
{
private static Container container;
public static Container Setup()
{
container = new Container();
container.Options.DefaultScopedLifestyle = Lifestyle.CreateHybrid(
defaultLifestyle: new WebRequestLifestyle(),
fallbackLifestyle: new AsyncScopedLifestyle());
container.Register<IBaseRepository<User>, BaseRepository<User>>(Lifestyle.Scoped);
container.Register<ComputationService>(Lifestyle.Scoped);
container.Register<ILog, Logger>(Lifestyle.Scoped);
container.RegisterMvcControllers(Assembly.GetExecutingAssembly());
}
public static T Get<T>() where T : class
{
if (container == null)
throw new InvalidOperationException("Container hasn't been initialized.");
return container.GetInstance<T>();
}
}
Global.asax.cs 看起来像这样。
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
var container = SimpleInjectorIntegrator.Setup();
GlobalConfiguration.Configure(WebApiConfig.Register);
...some other code...
DependencyResolver.SetResolver(new SimpleInjectorDependencyResolver(container));
}
}
下面是 API 控制器。
public class ExperimentUploadController : ApiController
{
private ComputationService _service = SimpleInjectorIntegrator.Get<ComputationService>();
public IHttpActionResult Started(InputModel model)
{
...Do Something...
var task = Task.Run(() =>
{
_service.Do(model.Id);
});
}
}
API 依赖于ComputationService,它使用存储库执行与数据库的连接。当我尝试从 ComputationService 访问数据库时,它会抛出 DbContext 已被释放。
ComputationService 代码如下所示:
public class ComputationService
{
private IBaseRepository<User> _userRepo = SimpleInjectorIntegrator.Get<User>();
public void Do(int id)
{
///throws here
var user = _userRepo.Get(id);
}
}
我不知道为什么会这样。
【问题讨论】:
-
您在等待
task吗?如果您不这样做,则请求可能在Do方法完成之前完成(并因此处理DbContext)。 -
我不希望线程等待任务完成。我希望我的 API 返回并让我的服务执行任务,并且在服务中我正在注入所有必需的依赖项。
-
也许this 会有所帮助?
标签: c# asp.net-mvc dependency-injection simple-injector