更新时间:2023-02-25 21:20:39
有两件事情。你需要这个在DefaultRegistry有控制器的一个更强大的登记:
There's two things. You need this in the DefaultRegistry to have a more robust registration of Controllers:
scan.With(new ControllerConvention());
下面是StructureMap 4 ControllerConvention:
Here is the ControllerConvention for StructureMap 4:
public class ControllerConvention : IRegistrationConvention
{
public void ScanTypes(TypeSet types, Registry registry)
{
foreach (var type in types.AllTypes().Where(type => type.CanBeCastTo<Controller>() && !type.IsAbstract))
{
registry.For(type).LifecycleIs(new UniquePerRequestLifecycle());
}
}
}
二,使用 DependencyResolver
是preferable在创建自己的 DefaultControllerFactory
。 MVC的实现更丰富,你可以看到此处。你可以这样就会复制自己,但是这不是面向未来,最重要的,没有必要因为你可以只使用 DependencyResolver
,让您的code简单。基本上,使用 StructuremapMvc
和 StructureMapDependencyScope
您安装StructureMap.MVC5得到的类。你可以初始化和 StructuremapMvc
配置移动到Global.asax中,如果你preFER。
Second, using the DependencyResolver
is preferable over creating your own DefaultControllerFactory
. MVC's implementation is richer as you can see here. You could copy that to your own but that's not future proof and most of all, not needed because you can just use DependencyResolver
, keeping your code simpler. Basically, use the classes StructuremapMvc
and StructureMapDependencyScope
you get from installing StructureMap.MVC5. You could move the initialization and disposing from StructuremapMvc
to the Global.asax if you prefer.
public static StructureMapDependencyScope StructureMapDependencyScope { get; set; }
public static void End()
{
StructureMapDependencyScope.Dispose();
}
public static void Start()
{
IContainer container = IoC.Initialize();
StructureMapDependencyScope = new StructureMapDependencyScope(container);
DependencyResolver.SetResolver(StructureMapDependencyScope);
DynamicModuleUtility.RegisterModule(typeof(StructureMapScopeModule));
}