我有一個問題,試圖讓singleton生命週期在StructureMap中與自定義約定一起使用。Singleton with ASP.NET MVC中的StructureMap自定義約定4
基本上我有一個包含我想成爲一個單使得它在應用程序啓動時創建一次字典定製註冊表類型的類。
我創建了一個自定義的公約將看到一個類的屬性並確定類是否應該是HttpContextScoped或辛格爾頓。
的問題是,當我與Visual Studio調試器中運行應用程序的對象的構造是應該是一個單身被調用每次加載網頁,而不是如我所料發生一次時間。它看起來像對象表現爲HttpContextScoped代替辛格爾頓。
這裏還有一些細節:
StructuremapMvc類app_start文件夾
public static class StructuremapMvc
{
public static void Start()
{
IContainer container = IoC.Initialize();
DependencyResolver.SetResolver(new StructureMapDependencyResolver(container));
GlobalConfiguration.Configuration.DependencyResolver = new StructureMapDependencyResolver(container);
}
}
的Ioc類
公共靜態初始化的IContainer()
ObjectFactory.Initialize(x =>
{
x.Scan(scan =>
{
scan.TheCallingAssembly();
scan.AssemblyContainingType<IConfigManager>();
scan.WithDefaultConventions();
scan.Convention<CustomConvention>();
});
CustomConvention : IRegistrationConvention
public void Process(Type type, Registry registry) public void Process(Type type, Registry registry)
{
var attributes = type.GetCustomAttributes(false);
if (attributes.Length > 0)
{
if (attributes[0] is SingletonAttribute)
{
registry.For(type).Singleton();
}
else if (attributes[0] is HttpContextScopedAttribute)
{
registry.For(type).HttpContextScoped();
}
}
}
[Singleton]
public class MyRegistry : IMyRegistry