我认为你需要创建一个约定来实现这一点:
using StructureMap.Pipeline;
using StructureMap.TypeRules;
using StructureMap.Configuration.DSL;
public class ConcreteLifecycleConvention : StructureMap.Graph.IRegistrationConvention
{
private readonly Type _baseType;
private readonly ILifecycle _lifecycle;
public ConcreteLifecycleConvention(Type baseType, ILifecycle lifecycle)
{
_baseType = baseType;
_lifecycle = lifecycle;
}
public void ScanTypes(TypeSet types, Registry registry)
{
foreach(var type in types.AllTypes())
{
if (type.IsAbstract || !type.CanBeCreated() || !type.CanBeCastTo(_baseType))
continue;
registry.For(type).LifecycleIs(_lifecycle);
}
}
}
在扫描中应用约定:
ObjectFactory.Initialize(c =>
{
c.Scan(scan =>
{
scan.TheCallingAssembly();
scan.With(new ConcreteLifecycleConvention(typeof(ServiceBase),
new UniquePerRequestLifecycle()));
});
});
作为旁注。使用内置约定,您可以使用OnAddedPluginTypes
来指定生命周期,但是没有内置约定可以注册没有接口注册的具体内容
scan.WithDefaultConventions().OnAddedPluginTypes(x =>
x.LifecycleIs(new UniquePerRequestLifecycle()));