0

我知道我可以在 SO 上找到这个问题,有很多答案,但我已经尝试了很多这些答案,但没有奏效。

我将 NHibernate 与 Ninject 一起使用。我只有在一个发布操作后才会收到此错误,所有其他操作都可以正常工作。

这是我更改设置表行和 nHibernate 配置的代码。

NHibernate/Ninject 配置:1)

class ArtexNinjectModule: NinjectModule
{
    /// <summary>
    /// For dependency injection with Ninject
    /// </summary>
    public override void Load()
    {


        this.Bind<IRepository<Guide>>().To<GuideRepository>();
        this.Bind<IRepository<GuideReservation>>().To<GuideReservationRepository>();
        this.Bind<IRepository<Alert>>().To<AlertRepository>();
        this.Bind<IRepository<ReceiveAlert>>().To<ReceiveAlertRepository>();
        this.Bind<IRepository<TourType>>().To<TourTypeRepository>();
        this.Bind<IRepository<Role>>().To<RoleRepository>();
        this.Bind<IRepository<Language>>().To<LanguageRepository>();
        this.Bind<IRepository<GuideLanguage>>().To<GuideLanguageRepository>();
        this.Bind<IRepository<Member>>().To<MemberRepository>();
        this.Bind<IRepository<Reservation>>().To<ReservationRepository>();
        this.Bind<IRepository<Groep>>().To<GroepRepository>();
        this.Bind<IRepository<Individual>>().To<IndividualRepository>();
        this.Bind<IRepository<Address>>().To<AddressRepository>();
        this.Bind<IRepository<BlockedDate>>().To<BlockedDateRepository>();
        this.Bind<IRepository<Setting>>().To<SettingRepository>();
        this.Bind<ISessionFactory>().ToMethod(x => NHibernateHelper.SessionFactory).InSingletonScope();
        this.Bind<ISession>().ToMethod(x => NHibernateHelper.SessionFactory.OpenSession()).InRequestScope();

    }

}

2)

public static class NHibernateHelper
{
    private static ISessionFactory _sessionFactory;

    public static ISessionFactory SessionFactory
    {
        get { return _sessionFactory ?? (_sessionFactory = CreateSessionFactory()); }
    }

    private static ISessionFactory CreateSessionFactory()
    {

        return new Configuration()
            .Configure()
            .AddAssembly(typeof(NHibernateHelper).Assembly)
            .BuildSessionFactory();
    }
}

3)

public class NHHttpModule : IHttpModule 
{

    protected readonly ILog Logger = LogManager.GetLogger("Application");
    public void Init(HttpApplication context)
    {


        context.EndRequest += ApplicationEndRequest; context.BeginRequest += ApplicationBeginRequest; 
    }      
    public void ApplicationBeginRequest(object sender, EventArgs e) 
    {

        if (!CurrentSessionContext.HasBind(NHibernateHelper.SessionFactory))
        {
            CurrentSessionContext.Bind(NHibernateHelper.SessionFactory.OpenSession());
            Logger.Debug("BEGIN REQUEST");
        }
    }      
    public void ApplicationEndRequest(object sender, EventArgs e) 
    {
        Logger.Debug("END REQUEST");
        ISession currentSession = CurrentSessionContext.Unbind(NHibernateHelper.SessionFactory);
        if (currentSession != null)
        {
            currentSession.Close();
            currentSession.Dispose();
        }
    }
    public void Dispose()
    {  

    }
}

这是动作:

        [TransactionFilter]
        [HttpPost]
        public void Options(string maxPersonsHeadquarters, string maxPersonsTextile, string maxPerGroupHeadquarters, string maxPerGroupTextile, string deleteWithGuide, string deleteWithConfirmedGuide, string deleteWithConfirmedGroupIndi)
        {
            Setting oldSetting = _settingRepo.GetList().ToList().ElementAt(0);
            try
            {
                oldSetting.MaxPersonsHeadquarters = int.Parse(maxPersonsHeadquarters);
                oldSetting.MaxPersonsPerGroupHeadquarters = int.Parse(maxPerGroupHeadquarters);
                oldSetting.MaxPersonsTextile = int.Parse(maxPersonsTextile);
                oldSetting.MaxPersonsPerGroupTextile = int.Parse(maxPerGroupTextile);

                oldSetting.DeleteWithConfirmedGroup = bool.Parse(deleteWithConfirmedGroupIndi);
                oldSetting.DeleteWithConfirmedGuide = bool.Parse(deleteWithConfirmedGuide);
                oldSetting.DeleteWithGuide = bool.Parse(deleteWithGuide);

                _settingRepo.Edit(oldSetting);
            }
            catch (Exception e) {
                logger.Error("SettingController/Option: " + e.Message );    
            }



        }

我希望你有足够的信息来解决我的问题。如果您需要更多信息,请与我们联系。提前致谢。

4

1 回答 1

0

我找到了答案。你们不知道答案,因为这是一个 jQuery 问题。

单击该按钮时,这将转到我的 jQuery 函数。这个函数给我的控制器发了一个帖子。

但是按钮输入类型是“提交”,所以我的控制器有 2 个帖子。

2 个帖子,1 个会话 ==> 你得到一个“没有会话绑定到当前上下文”-错误!

谢谢大家的帮助。

于 2012-07-17T13:26:53.970 回答