为什么 Container.Configure 花了这么长时间?
Why is Container.Configure taking so long?
今天我正在调查我们系统中的缓慢问题。通过插入一些调试日志,我将范围缩小到以下部分:
var db = MyDbContext.ForShard(shardKey);
_logger.Debug("Point 1");
container.Configure(cfg =>
{
_logger.Debug("Anon 1");
cfg.For<MyDbContext>().Use(db);
_logger.Debug("Anon 2");
});
_logger.Debug("Point 2");
数据库已分片,shardKey
确定使用哪个分片。调用 MyDbContext.ForShard()
returns 一个数据库连接,其连接字符串指向正确的分片。有问题的片段然后告诉 StructureMap Container
使用这个实例进行依赖注入。
在日志中,每行之间的时间延迟可以忽略不计,除了 "Anon 2" 和 "Point 2" 之间的时间间隔可以在一秒左右。不好。但是我从来没有见过 StructureMap 花了这么长时间来配置一个用法。
我是不是做错了什么?
正如来自 StructureMap 的好人在他们的 GitHub 页面上的回答:
@CoreyKaylor says: The configure call shouldn't happen during runtime, only startup so if this call is happening for every web request as an example it would be extremely slow as the configure call requires a global lock. A better alternative if you absolutely need runtime overrides is to use a nested container for the scope of an http request and pass TypeArguments to the nested container. That's all assuming you're running the latest version of StructureMap too.
在我的例子中,这转化为:
var db = MyDbContext.ForShard(shardKey);
var typeArgs = new TypeArguments();
typeArgs[typeof(MyDbContext)] = db;
var nested = container.GetNestedContainer(typeArgs);
今天我正在调查我们系统中的缓慢问题。通过插入一些调试日志,我将范围缩小到以下部分:
var db = MyDbContext.ForShard(shardKey);
_logger.Debug("Point 1");
container.Configure(cfg =>
{
_logger.Debug("Anon 1");
cfg.For<MyDbContext>().Use(db);
_logger.Debug("Anon 2");
});
_logger.Debug("Point 2");
数据库已分片,shardKey
确定使用哪个分片。调用 MyDbContext.ForShard()
returns 一个数据库连接,其连接字符串指向正确的分片。有问题的片段然后告诉 StructureMap Container
使用这个实例进行依赖注入。
在日志中,每行之间的时间延迟可以忽略不计,除了 "Anon 2" 和 "Point 2" 之间的时间间隔可以在一秒左右。不好。但是我从来没有见过 StructureMap 花了这么长时间来配置一个用法。
我是不是做错了什么?
正如来自 StructureMap 的好人在他们的 GitHub 页面上的回答:
@CoreyKaylor says: The configure call shouldn't happen during runtime, only startup so if this call is happening for every web request as an example it would be extremely slow as the configure call requires a global lock. A better alternative if you absolutely need runtime overrides is to use a nested container for the scope of an http request and pass TypeArguments to the nested container. That's all assuming you're running the latest version of StructureMap too.
在我的例子中,这转化为:
var db = MyDbContext.ForShard(shardKey);
var typeArgs = new TypeArguments();
typeArgs[typeof(MyDbContext)] = db;
var nested = container.GetNestedContainer(typeArgs);