1
我知道我可以在SO上找到這個問題,並提供了很多答案,但是我已經嘗試了很多這些答案,但他們沒有工作。沒有會話綁定到當前上下文:NHibernate
我使用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);
}
}
我希望你有足夠的信息來解決我的問題。如果您需要更多信息,請與我們聯繫。 在此先感謝。
沒有人能幫助我通過這個? :/ – Sllix 2012-07-11 07:06:50
對於存儲庫 - 似乎你沒有使用綁定在http模塊中的會話,而是由ninject注入的會話。也許你可以刪除http模塊,如果你不使用它。 – 2012-07-11 08:16:10
爲什麼要刪除?我用它。 – Sllix 2012-07-11 08:21:02