kel*_*oti 3 structuremap factory-method c#-4.0 open-generics
我正在尝试注册工厂方法来创建开放泛型类型的实例MongoCollection<>
.但是,当我GetInstance
看来它使用的是MongoCollection的构造函数而不是工厂方法.
var mongo = new MongoConfiguration("mongodb://localhost", "test");
For(typeof (MongoCollection<>)).Use(c =>
{
var requestedType = c.BuildStack.Current.RequestedType; // set breakpoint here
var type = requestedType.GetGenericArguments()[0];
return mongo.GetCollection(type);
});
Run Code Online (Sandbox Code Playgroud)
然后我做
ObjectFactory.GetInstance<MongoCollection<User>>();
Run Code Online (Sandbox Code Playgroud)
当我运行该GetInstance
行时,它永远不会在工厂方法中遇到断点,但它会抛出一个StructureMapException
说法"没有为PluginFamily MongoDb.Driver.MongoServerSettings定义默认实例".有一个构造函数MongoCollection
需要一个MongoServerSettings
,但我不希望结构图使用该构造函数,我希望它使用我的工厂方法.
任何想法为什么它不使用工厂方法?这是一个错误吗?