Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7088079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:45:12+00:00 2026-05-28T07:45:12+00:00

I’m using Spring.NET 1.3.2, NHibernate 3.1 and the OSIV pattern in a ASP.NET application.

  • 0

I’m using Spring.NET 1.3.2, NHibernate 3.1 and the OSIV pattern in a ASP.NET application.

I have a custom EventListener that needs to be request scoped because it uses
HttpContext.Current.Items as a constructor dependency.

Since ISession is also request scoped, I should be able to use Spring.NET to manage these dependencies for me.

The problem is that EventListener, like IInterceptor, is a property of ISessionFactory which is not request scoped (it’s a singleton). The mismatch between the web object scopes is problematic.

I tried the following XML snippet, but the conditional expression always yields null. I think this is due to Spring creating EventListener object at an application level scope, and before HttpContext.Current.Items has had a chance to be populated.

  <object id="EventListener" scope="request" type="MyEventListener,DAL">

        <constructor-arg index="0" expression="T(System.Web.HttpContext).Current.Items.Contains('Principal')?T(System.Web.HttpContext).Current.Items['Principal']:null"/>

  </object>

So my requirement is:

  1. Configure custom EventListener object in Spring so that it is created on a per-request basis

  2. EventListener instantiation must occur late enough in the request lifecycle so that HttpContext.Current.Items[‘Princpial’] has been populated by a custom IHttpModule

  3. The EventListener instance is injected into the the current OSIV ISession

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T07:45:13+00:00Added an answer on May 28, 2026 at 7:45 am

    I think you’re trying to do this the wrong way around. According to the NHibernate documentation EventListeners should basically be considered singletons for your application.

    Even when you register your custom EventListener with the SessionFactory, say at the start of the request, you still have no guarantee whatsoever that this listener will only receive events raised in the current HttpContext.

    Instead you should register your eventhandlers globally, when configuring the session factory. If you need context information, such as a principal, you can inject a dependency with a (http)context-aware implementation or implement it as an ambient context.

    From the custom listener, you can get access to the session that raised the event. Take for instance this ILoadEventListener implementation:

    public class CustomLoadEventListener : ILoadEventListener
    {
        private IPrincipalProvider _principalProvider;
    
        public CustomLoadEventListener(IPrincipalProvider provider)
        {
            _principalProvider = provider;
        }
    
        public void OnLoad(LoadEvent @event, LoadType loadType)
        {
            var sessionThatRaisedTheEvent = @event.Session;
            var principalForTheCurrentContext = _principalProvider.GetCurrentPrincipal();
        }
    }
    
    public interface IPrincipalProvider
    {
        IPrincipal GetCurrentPrincipal();
    }
    
    public class HttpContextPrincipalProvider : IPrincipalProvider
    {
        public IPrincipal GetCurrentPrincipal()
        {
            return System.Web.HttpContext.Current.User;
        }
    }
    

    This might not be the answer you hoped to get, but IMO what your are asking for would be fighting the infrastructure, which I generally try to avoid.

    If you post some more details on what you are trying to achieve, we might be able to propose a better solution. What should your EventListener do?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have thousands of HTML files to process using Groovy/Java and I need to
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
I am reading a book about Javascript and jQuery and using one of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.