Unity Container PerResolveLiveTimeManager with multi-database and multiple requests on IIS

Unity Container PerResolveLiveTimeManager with multi-database and multiple requests on IIS

我有一个关于 Unity Container 的问题。我的 MVC 应用程序从 Application_Start 开始于 Global.asax,它是 Unity Container 的容器,其工作方式如下

_container = new UnityContainer();
_container.RegisterType(typeof(MainBCUnitOfWork), new PerResolveLIfeTimeManager());

据我所知,IIS 将在其生命周期中仅实例化 MainBCUnitOfWork 类型一次,并将在所有请求中使用同一实例,这就是我使用 PerResolveLifeTimeManager 类型的 LifeTimeManager 的原因。

我的应用程序在这种模式下一直运行良好,但是我正在尝试利用共享数据库访问/跨数据库访问,其中所需的访问将来自(会话,查询字符串)并使用以下方法更改数据库:

public void ChangeDatabase(string database)
{
    Database.Connection.ConnectionString = "server=localhost;User Id=root;password=mypassword;Persist Security Info=True;database=" + database;
}

在我的本地测试中,一切正常,但如果 IIS 同时处理许多请求,我在生产中遇到问题。

我做了一些研究,发现IIS每次只处理一个请求的参考资料,如果需要处理多个请求,我应该激活Web Garden,但这会带来其他问题。看到这个link

我的问题是,IIS 服务器每次只处理一个请求,与来源无关吗?

在执行期间更改数据库会干扰仍在进行中的先前请求吗?

我使用没有 PerRequestLIfetimeManager 的 Utity 2,它会根据每个请求实例化 MainBCUnitOfWork,此处已说明 MVC, EF - DataContext singleton instance Per-Web-Request in Unity

如果我更新到较新的版本并为每个请求使用一个实例,会对我的性能产生什么影响?

对于这种情况有何建议?

From what I know, IIS will instantiate the type MainBCUnitOfWork only one time on its life cycle and will utilize the same instance in all requests, which is why I am using LifeTimeManager of type PerResolveLifeTimeManager.

这是错误的。看看这篇文章 (one and two)。 PerResolveLifeTimeManager 不是单身生活时间经理。对于每个 Resolve,您将获得 MainBCUnitOfWork 的新实例。

What is recommended for this situation?

使用PerRequestLifeTimeManager是web应用的最佳选择。对于每个请求,您将获得一个新的 UnitOfWork 独立实例。