C# 如何对每个请求的会话中的单例类进行验证
我有一个配置类,用于基础中的第一个用户访问。如果firebird或oracle使用该类,则需要放置设置和服务。设置保存在加密文件中,每次启动应用程序时,请检查这些设置是否良好C# 如何对每个请求的会话中的单例类进行验证,c#,nhibernate,singleton,session-per-request,C#,Nhibernate,Singleton,Session Per Request,我有一个配置类,用于基础中的第一个用户访问。如果firebird或oracle使用该类,则需要放置设置和服务。设置保存在加密文件中,每次启动应用程序时,请检查这些设置是否良好 private static Settings _data; public static Settings Data { get { if (_data == null) {
private static Settings _data;
public static Settings Data
{
get
{
if (_data == null)
{
_data = new Settings();
if (_data.FilePath != null && !Directory.Exists(Path.GetDirectoryName(_data.FilePath)))
{
Directory.CreateDirectory(Path.GetDirectoryName(_data.FilePath));
}
_data.Load(_data.FilePath);
}
return _data;
}
}
然而,当我对所有请求使用会话时,我决定改变存在同步问题的情况,然后决定对每个请求实施会话,但每次开始应用程序时,都会创建会话,并且需要创建会话服务数据,这些数据是在singleton类中创建的。实现的一种方法是在Global.asax中创建会话之前进行验证
protected void Application_BeginRequest()
{
if (!Settings.Data.Valid())
throw new SingletonException();
var session = SessionManager.SessionFactory.OpenSession();
if (!session.Transaction.IsActive)
session.BeginTransaction(IsolationLevel.ReadCommitted);
CurrentSessionContext.Bind(session);
}
这是我的会议经理
private SessionManager()
{
if (Settings.Data.SGDB == 1)
{
sessionFactory = Fluently.Configure()
.Database(new FirebirdConfiguration().ConnectionString(GetConnectionString())
.ShowSql())
.Mappings(GetMappings())
.ExposeConfiguration(c => c.SetProperty(Environment.CurrentSessionContextClass, "web"))
.BuildSessionFactory();
}
if (Settings.Data.SGDB == 0)
{
sessionFactory = Fluently.Configure()
.Database(OracleConfiguration
.Oracle10
.ConnectionString(GetConnectionString())
.ShowSql()
.Driver<OracleManagedDataClientDriver>()
.Dialect<Oracle10gDialect>()
)
.Mappings(GetMappings())
.ExposeConfiguration(c => c.SetProperty(Environment.CurrentSessionContextClass, "web"))
.BuildSessionFactory();
}
}
private SessionManager()
{
if(Settings.Data.SGDB==1)
{
sessionFactory=fluntly.Configure()
.Database(新FirebirdConfiguration().ConnectionString(GetConnectionString())
.ShowSql())
.Mappings(GetMappings())
.ExposeConfiguration(c=>c.SetProperty(Environment.CurrentSessionContextClass,“web”))
.BuildSessionFactory();
}
if(Settings.Data.SGDB==0)
{
sessionFactory=fluntly.Configure()
.数据库(OracleConfiguration)
.Oracle10
.ConnectionString(GetConnectionString())
.ShowSql()
.司机()
.方言()
)
.Mappings(GetMappings())
.ExposeConfiguration(c=>c.SetProperty(Environment.CurrentSessionContextClass,“web”))
.BuildSessionFactory();
}
}
但是,对于每个请求,它都会进行验证,这对我来说是一种更好的方法来验证singleton类?在应用程序启动过程中验证设置,如果设置无效,则不要启动应用程序,因为没有设置,应用程序无法正常工作。执行类似于双锁检查/验证的操作