我的理解是不应该在并发Web请求之间共享DbContext实例,所以绝对不能跨线程.但是如何在非并发Web请求中共享它呢?
由于线程敏捷性(ASP.Net中线程敏捷性的含义是什么?),我是否正确,一个线程可以在它死之前处理多个Web请求?
如果是这样,依赖为每个线程注入DbContext实例是否安全?
原因是我使用Unity,它不包括每个请求的生命周期选项.从MVC,EF - DataContext单例实例Per-Web-Request在Unity中,我想我可以使用自定义LifetimeManager; 我只是想知道使用PerThreadLifetimeManager是否安全和充足.
asp.net-mvc entity-framework dependency-injection unity-container dbcontext
我只是c#的初学者,并接管了一个工作项目,其中包括由其他2位开发人员提供的代码.从sql读取数据时代码存在内存泄漏.基本上,代码从数据库中读取ping结果并将其返回到一个迷你图中,然后我将其显示在仪表板上.希望你能帮忙.
[ActionName("get-response-times")]
public JsonResult GetResponseTime()
{
try
{
//todo...get list of sites we we want to check from database
var entities = new Entities();
var sites = entities.Sites.ToList();
var status = new List<ResponseDataModel>();
foreach (var site in sites)
{
var response = Ping(site.URL);
site.SiteResponseHistories.Add(new SiteResponseHistory
{
CreateDate = DateTime.UtcNow,
ResponseTime = (int)response,
Site = site
});
status.Add(new ResponseDataModel
{
ResponseTime = (int)response,
Name = site.Name,
ID = site.Id,
History = site.SiteResponseHistories.OrderByDescending(a => a.CreateDate).Select(a => a.ResponseTime.GetValueOrDefault(0)).Take(100).Reverse().ToArray()
});
}
entities.SaveChanges();
return Json(status); …Run Code Online (Sandbox Code Playgroud) 我完成了建议的步骤,将Ninject添加到我的MVC应用程序中.我向DbContext控制器的构造函数添加了一个参数.
控制器:
public class MyController : BaseController
{
public ArticlesController(MyDbContext context)
: base(context)
{ }
}
Run Code Online (Sandbox Code Playgroud)
基础控制器:
public class BaseController : Controller
{
protected DbContext MyDbContext;
public BaseController(MyDbContext context)
{
MyDbContext = context;
}
}
Run Code Online (Sandbox Code Playgroud)
这似乎运作良好.但是请给我一些问题.
Ninject能否确保我DbContext的清理和及时处理?
我为所有应用程序的控制器创建了一个基类来处理任何常见的初始化等.基类DbContext在构造函数中接受我的参数的实例.但这需要我也将此参数添加到我的应用程序中的每个控制器.有没有办法不要求这个?
我不确定创建一个我的实例是多么昂贵DbContext.是否有任何方法可以进行优化,只有在请求实际要求我访问数据库时才会创建它.
我的应用程序使用实体框架。由于我希望DbContext在单个请求中重用my ,因此我将其注册为Lifestyle.Scoped,如下所示:
container.Register<MyDbContext>(Lifestyle.Scoped);
Run Code Online (Sandbox Code Playgroud)
其他类得到这个MyDbContext注入。例如,请参阅以下存储库:
应用程序库是:
public class ApplicationsRepository : IApplicationsRepository
{
private readonly MyDbContext _dbContext;
public ApplicationsRepository(MyDbContext dbContext)
{
_dbContext = dbContext;
}
public void Save()
{
_dbContext.Save();
}
public Application GetByName(string appName)
{
var dbApplication = _dbContext.APPLICATIONS.FirstOrDefault(a => a.NAME == appName);
return ApplicationMapper.MapApplicationFromAPPLICATIONS(dbApplication);
}
}
Run Code Online (Sandbox Code Playgroud)
但是还有无数其他类依赖于MyDbContext. 其他类可能不MyDbContext直接依赖,但会注入一个依赖于MyDbContext.
我的问题是:我应该为这些课程使用什么样的生活方式管理以及如何实施?
请在以下情况下纠正我.(问题在最后)
(我问了一个类似的问题,这个问题是没有组织的,并且被投票结束.所以我把这里的问题概括为一个范围,可以回答确切的答案.)
我正在开发一个使用nhibernate作为ORM的多层Web应用程序.我的图层结构如下
使用上面的层,类和接口如下放置.
ProductController.cs(UI层)
public class ProductController : Controller
{
ProductServices _ProductServices;
NHibernate.ISession _Session;
public ProductController()
{
_Session = SessionManager.GetCurrentSession();
_ProductServices = new ProductServices(
new ProductRepository(), _Session);
}
// Cont..
}
Run Code Online (Sandbox Code Playgroud)
ProductServices.cs(服务层)
public class ProductServices : IProductServices
{
protected IProductRepository _ProductRepository;
protected NHibernate.ISession _Session;
public ProductServices(IProductRepository productRepository,
NHibernate.ISession session)
{
_ProductRepository = productRepository;
_Session = session;
_ProductRepository.SetSession(_Session);
}
// cont...
}
Run Code Online (Sandbox Code Playgroud)
ProductRepository.cs(存储库层)
public class ProductRepository : IProductRepository
{
NHibernate.ISession _Session;
public void SetSession(NHibernate.ISession session)
{ …Run Code Online (Sandbox Code Playgroud)