我想为ASP.NET MVC应用程序中的一个控制器添加每次会话的生活方式,但是目前没有这种选择。我搜索了stackoverflow,找到了下一个解决方案
public class PerSessionLifestyleManager : AbstractLifestyleManager
{
private readonly string PerSessionObjectID = "PerSessionLifestyleManager_" + Guid.NewGuid().ToString();
public override object Resolve(CreationContext context)
{
if (HttpContext.Current.Session[PerSessionObjectID] == null)
{
// Create the actual object
HttpContext.Current.Session[PerSessionObjectID] = base.Resolve(context);
}
return HttpContext.Current.Session[PerSessionObjectID];
}
public override void Dispose()
{
}
}
但我希望能够写一些像
cr => cr.LifeStyle.PerSession.Named(cr.Implementation.Name)
我使用Castle Windsor 3.0,发现CastleType.en命名空间内包含LifestyleType枚举,DefaultKernel正在使用它。我的建议是重写DefaultKernel,但我真的不知道该怎么做才能免费且几乎没有错误,就好像DLL附带的PerSession生活方式一样。
最佳答案
因此,您要问两件事。
首先是如何实现生活方式。好的出发点是看如何实现每个Web请求的生活方式(将Scoped生活方式与自定义范围和范围访问器一起使用)
其次,如何在API中展示这些内容。我建议使用扩展方法,用LifestyleScoped<YourCustomScopeAccessor>()
封装对LifestylePerSession()
的下级调用,类似于WCF工具does it的方法。