PerRequestTask Executes Several times

Mar 24, 2011 at 8:38 PM

I'm using framework 4 VS 2010.

 

And what I want is to create a single transaction per request using nHibernate.

the problem is that Execute() method rund every time the server is requesting a file, jpg, css, javascript, etc.

 

 

ths code is the following:

 

 public class NHibernateSession : MvcExtensions.PerRequestTask
    {

        private static ISessionProvider _sessionProvider;
        private readonly HttpContextBase _httpContextBase;


        public NHibernateSession(ISessionProvider sessionProvider, HttpContextBase httpContextBase)
        {
            _sessionProvider = sessionProvider;
            _httpContextBase = httpContextBase;
        }


        protected override void DisposeCore()
        {
            _sessionProvider.CommitTransaction();
        }

      
        public override TaskContinuation Execute()
        {
           
           _sessionProvider.BeginTransaction();
            return TaskContinuation.Continue;
        }
    }

    public class RegisterTasks:MvcExtensions.RegisterPerRequestTasks
    {
        public RegisterTasks(ContainerAdapter container) : base(container)
        {
            container.RegisterAsPerRequest(typeof(NHibernateSession));
            //container.RegisterAsPerRequest(typeof(NHibernateSession));
        }
    }