我在Rhino Security中遇到了一个关闭但未处理NHibernate会话的问题。看起来Windsor将关闭会话交给AuthorizationService。直到第二次调用下面的方法才会出现此问题。第一个电话工作正常。 Windsor容器配备了瞬态生活方式。
我正在使用Sharp Architecture 2.0 RC。
调用服务/任务由与AuthorizationRepository相同的Windsor容器处理。
对于是什么导致这个问题的任何想法都将不胜感激?
namespace Rhino.Security.Services
{
public class AuthorizationRepository : IAuthorizationRepository
{
//...
public virtual UsersGroup[] GetAssociatedUsersGroupFor(IUser user)
{
ICollection<UsersGroup> usersGroups =
SecurityCriterions.AllGroups(user)
.GetExecutableCriteria(session) //This session is closed and not connected when the line executes
.AddOrder(Order.Asc("Name"))
.SetCacheable(true)
.List<UsersGroup>();
return usersGroups.ToArray(); //This line of code (ICollection...) throws the exception!!!
}
//...
}}
ISession Facility如下所示:
container.AddFacility<FactorySupportFacility>()
.Register(Component.For<ISession>()
.UsingFactoryMethod(() => NHibernateSession.Current)
.LifeStyle.Is(LifestyleType.Transient));
Rhino安全组件注册如下所示:
private static void AddRhinoSecurityComponentsTo(IWindsorContainer container)
{
// For RhinoSecurity
container.Register(
Component.For<IAuthorizationService>()
.ImplementedBy<AuthorizationService>()
.LifeStyle.Is(LifestyleType.Transient),
Component.For<IAuthorizationRepository>()
.ImplementedBy<AuthorizationRepository>()
.LifeStyle.Is(LifestyleType.Transient),
Component.For<IPermissionsBuilderService>()
.ImplementedBy<PermissionsBuilderService>()
.LifeStyle.Is(LifestyleType.Transient),
Component.For<IPermissionsService>()
.ImplementedBy<PermissionsService>()
.LifeStyle.Is(LifestyleType.Transient)
);
}
调用AuthorizationService的方法是以下两种方法之一,具体取决于:
SecurityHelper(1):
public static bool IsAllowed(string operation)
{
User user = (User)HttpContext.Current.Session["User"];
IAuthorizationService authorizationService = ServiceLocator.Current.GetInstance<IAuthorizationService>();
return authorizationService.IsAllowed(user as User, operation);
}
SecurityTasks(2):
public UsersGroup[] GetAssociatedUsersGroupFor(IUser user)
{
return authRepository.GetAssociatedUsersGroupFor(user);
}
NHibernate Init如下所示:
NHibernateSession.Init(
this.webSessionStorage,
new[] { Server.MapPath("~/bin/ClinicalTrials.Infrastructure.dll") },
new AutoPersistenceModelGenerator().Generate(),
Server.MapPath("~/NHibernate.config"));
ysod如下所示:
Session is closed!
Object name: 'ISession'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ObjectDisposedException: Session is closed!
Object name: 'ISession'.
Source Error:
Line 329: public virtual UsersGroup[] GetAssociatedUsersGroupFor(IUser user)
Line 330: {
Line 331: ICollection<UsersGroup> usersGroups =
Line 332: SecurityCriterions.AllGroups(user)
Line 333: .GetExecutableCriteria(session)
Source File: ...\Rhino.Security - nHibernate 3.1\Rhino.Security\Services\AuthorizationRepository.cs Line: 331
Stack Trace:
[ObjectDisposedException: Session is closed!
Object name: 'ISession'.]
NHibernate.Impl.AbstractSessionImpl.ErrorIfClosed() +124
NHibernate.Impl.AbstractSessionImpl.CheckAndUpdateSessionStatus() +31
NHibernate.Impl.SessionImpl.List(CriteriaImpl criteria, IList results) +173
NHibernate.Impl.CriteriaImpl.List(IList results) +80
NHibernate.Impl.CriteriaImpl.List() +105
Rhino.Security.Services.AuthorizationRepository.GetAssociatedUsersGroupFor(IUser user) in ...\Rhino.Security - nHibernate 3.1\Rhino.Security\Services\AuthorizationRepository.cs:331
ClinicalTrialsAdmin.Tasks.SecurityTasks.GetAssociatedUsersGroupFor(IUser user) in ...\Source\Admin\Solutions\ClinicalTrialsAdmin.Tasks\SecurityTasks.cs:89
ClinicalTrialsAdmin.Web.Mvc.Controllers.Permission.PermissionController.ShowUsers() in ...\Source\Admin\Solutions\ClinicalTrialsAdmin.Web.Mvc\Controllers\Permission\PermissionController.cs:228
lambda_method(Closure , ControllerBase , Object[] ) +96
System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +17
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +208
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +27
System.Web.Mvc.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12() +55
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +263
System.Web.Mvc.<>c__DisplayClass17.<InvokeActionMethodWithFilters>b__14() +19
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +263
System.Web.Mvc.<>c__DisplayClass17.<InvokeActionMethodWithFilters>b__14() +19
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +191
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +343
System.Web.Mvc.Controller.ExecuteCore() +116
System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +97
System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +10
System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +37
System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +21
System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +12
System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +50
System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f) +7
System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +22
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +60
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
System.Web.Mvc.<>c__DisplayClassa.<EndProcessRequest>b__9() +23
System.Web.Mvc.<>c__DisplayClass4.<Wrap>b__3() +12
System.Web.Mvc.ServerExecuteHttpHandlerWrapper.Wrap(Func`1 func) +38
System.Web.Mvc.ServerExecuteHttpHandlerWrapper.Wrap(Action action) +65
System.Web.Mvc.ServerExecuteHttpHandlerAsyncWrapper.EndProcessRequest(IAsyncResult result) +71
System.Web.HttpServerUtility.ExecuteInternal(IHttpHandler handler, TextWriter writer, Boolean preserveForm, Boolean setPreviousPage, VirtualPath path, VirtualPath filePath, String physPath, Exception error, String queryStringOverride) +1072
[HttpException (0x80004005): Error executing child request for handler 'System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper'.]
System.Web.HttpServerUtility.ExecuteInternal(IHttpHandler handler, TextWriter writer, Boolean preserveForm, Boolean setPreviousPage, VirtualPath path, VirtualPath filePath, String physPath, Exception error, String queryStringOverride) +3058371
System.Web.HttpServerUtility.Execute(IHttpHandler handler, TextWriter writer, Boolean preserveForm, Boolean setPreviousPage) +77
System.Web.HttpServerUtility.Execute(IHttpHandler handler, TextWriter writer, Boolean preserveForm) +28
System.Web.HttpServerUtilityWrapper.Execute(IHttpHandler handler, TextWriter writer, Boolean preserveForm) +22
System.Web.Mvc.Html.ChildActionExtensions.ActionHelper(HtmlHelper htmlHelper, String actionName, String controllerName, RouteValueDictionary routeValues, TextWriter textWriter) +497
System.Web.Mvc.Html.ChildActionExtensions.RenderAction(HtmlHelper htmlHelper, String actionName, String controllerName) +34
ASP._Page_Views_Administration_Default_cshtml.Execute() in ...\Source\Admin\Solutions\ClinicalTrialsAdmin.Web.Mvc\Views\Administration\Default.cshtml:44
System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +207
System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +81
System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +76
System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +220
System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +115
System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +303
System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +13
System.Web.Mvc.<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19() +23
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +260
System.Web.Mvc.<>c__DisplayClass1e.<InvokeActionResultWithFilters>b__1b() +19
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +260
System.Web.Mvc.<>c__DisplayClass1e.<InvokeActionResultWithFilters>b__1b() +19
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +260
System.Web.Mvc.<>c__DisplayClass1e.<InvokeActionResultWithFilters>b__1b() +19
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +177
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +343
System.Web.Mvc.Controller.ExecuteCore() +116
System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +97
System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +10
System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +37
System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +21
System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +12
System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +50
System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f) +7
System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +22
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +60
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8963149
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184
在_layout.cshtml页面和继承.cshtml页面之间的转换中,会话似乎已关闭。这是正常的吗?我错过了某种方法来控制会话关闭的方式和时间,或者Windsor如何处理会话?
上次会话打开调用堆栈:
> Rhino.Security.DLL!Rhino.Security.Services.AuthorizationRepository.AuthorizationRepository(NHibernate.ISession session) Line 26 C#
[External Code]
Microsoft.Practices.ServiceLocation.DLL!Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance(System.Type serviceType, string key) Line 49 + 0x11 bytes C#
Microsoft.Practices.ServiceLocation.DLL!Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance<Rhino.Security.Interfaces.IAuthorizationService>() Line 90 + 0x4a bytes C#
ClinicalTrialsAdmin.Web.Mvc.DLL!ClinicalTrialsAdmin.Web.Mvc.Controllers.Shared.SecurityHelper.IsAllowed(string operation) Line 38 + 0x24 bytes C#
App_Web_1yy2q1b3.dll!ASP._Page_Views_Permission_UserGridView_cshtml.Execute() Line 179 + 0xb bytes C#
[External Code]
App_Web_3rkjaiom.dll!ASP._Page_Views_Administration_Default_cshtml.Execute() Line 44 + 0x1f bytes C#
[External Code]
Session is still open!
第一个会话关闭调用堆栈:
> Rhino.Security.DLL!Rhino.Security.Services.AuthorizationRepository.AuthorizationRepository(NHibernate.ISession session) Line 26 C#
[External Code]
Microsoft.Practices.ServiceLocation.DLL!Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance(System.Type serviceType, string key) Line 49 + 0x11 bytes C#
Microsoft.Practices.ServiceLocation.DLL!Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance<Rhino.Security.Interfaces.IAuthorizationService>() Line 90 + 0x4a bytes C#
ClinicalTrialsAdmin.Web.Mvc.DLL!ClinicalTrialsAdmin.Web.Mvc.Controllers.Shared.SecurityHelper.IsAllowed(string operation) Line 38 + 0x24 bytes C#
App_Web_paosedyq.dll!ASP._Page_Views_Shared__Layout_cshtml.Execute() Line 222 + 0x98 bytes C#
[External Code]
Session is now closed!
我仍然遇到这个问题,并没有找到任何新的解决方案。每隔一段时间,会话最终都会在Rhino Security代码中关闭。因此,Rhino抛出System.ObjectDisposedException。