albertodall / IdentityServer4.NHibernate

IdentityServer4.NHibernate is a persistence layer for IdentityServer 4 configuration data that uses NHibernate to access data stored in a database.
Apache License 2.0
8 stars 8 forks source link

Session is closed exception - AB#57 #11

Closed lcetinapta closed 3 years ago

lcetinapta commented 4 years ago

Hi, I have found another issue. The issue happens when using Client CORS. it happens because of this line of code in CorsPolicyService.cs: using (var session = _context.HttpContext.RequestServices.GetRequiredService<IStatelessSession>())

Here You are explicitly disposing session, and this leads to closing the connection on the database. So next time I request an IStatelessSession instance I will get a closed one. Because You are registering IStatelessSession in the DI container as Scoped, You don't need to manage it's lifetime yourserf. It will be managed by the DI container. I would replace this line with this:

using(var transaction = _session.BeginTransaction()){ ... transaction.RollBack(); }

And would include IStatelessSession as parameter in the constructor of the class: CorsPlicyService like this... public CorsPolicyService(IHttpContextAccessor context, ILogger<CorsPolicyService> logger, IStatelessSession session) { _context = context ?? throw new ArgumentNullException(nameof(context)); _logger = logger; _session = session; }

albertodall commented 4 years ago

Hi @lcetinapta Thanks for reporting the issue. Could you provide a PRs for this? I'll be happy to add your contribution to the project. Thanks!

albertodall commented 3 years ago

In this case, it seems it's not possible to pass the IStatelessSession as a constructor parameter, because of this:

https://github.com/aspnet/CORS/issues/105

Anyway, it's correct to not manage the IStatelessSession lifetime here. I'm going to push a fix.

lcetinapta commented 3 years ago

you can pas the stateless session as func .

Get Outlook for Androidhttps://aka.ms/ghei36


From: Alberto Dallagiacoma notifications@github.com Sent: Friday, September 25, 2020 10:15:56 PM To: albertodall/IdentityServer4.NHibernate IdentityServer4.NHibernate@noreply.github.com Cc: Luka Cetina - PTA l.cetina@pta.hr; Mention mention@noreply.github.com Subject: Re: [albertodall/IdentityServer4.NHibernate] Session is closed exception - AB#57 (#11)

Unfortunately, it's seems it's not possible to pass the IStatelessSession on the constructor in this case, because of this:

aspnet/CORS#105https://github.com/aspnet/CORS/issues/105

Anyway, it's correct to not manage the IStatelessSession lifetime here. I'm going to push a fix.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://github.com/albertodall/IdentityServer4.NHibernate/issues/11#issuecomment-699133790, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ALGSFYCLBWK42KI2OAKKFEDSHT27ZANCNFSM4Q57JRJQ.