2016-06-27 01:45:38 +02:00
using System ;
using System.Collections.Generic ;
using System.Diagnostics ;
using System.Globalization ;
using System.IO ;
using System.ServiceModel ;
using System.Threading ;
using NHibernate ;
using NHibernate.Cfg ;
using log4net ;
using log4net.Appender ;
using log4net.Layout ;
using log4net.Repository.Hierarchy ;
namespace BeWo.Data
{
public class WCFHibernateSessionManager
{
private static Dictionary < string , ISessionFactory > _SessionFactories = new Dictionary < string , ISessionFactory > ( ) ;
private static object _Lock = string . Empty ;
2025-01-31 15:15:40 +01:00
private static readonly string _LogsDirectory = @"C:\BeWoPlaner\service\Host\logs" ;
2016-06-27 01:45:38 +02:00
//private static readonly ILog log = LogManager.GetLogger("WCFHibernateSessionManager");
public static ISession CurrentSession
{
get
{
2016-07-18 06:14:17 +02:00
return SessionOperationContextExt . Current = = null ? null : SessionOperationContextExt . Current . Session ;
2016-06-27 01:45:38 +02:00
}
private set
{
SessionOperationContextExt . Current . Session = value ;
}
}
private static ISessionFactory SessionFactory
{
get
{
try
{
Monitor . Enter ( _Lock ) ;
2025-01-31 15:15:40 +01:00
if ( ! _SessionFactories . ContainsKey ( MultitenancyOperationContextExt . Current . Tenant ) )
2016-06-27 01:45:38 +02:00
{
#if DEBUG
2019-07-01 15:11:19 +02:00
//TODO: wieder einkommentieren!
2023-01-11 17:26:38 +01:00
var hierarchy = ( Hierarchy ) LogManager . GetRepository ( ) ;
var logger = ( Logger ) hierarchy . GetLogger ( "NHibernate.SQL" ) ;
logger . AddAppender ( new TraceAppender { Layout = new SimpleLayout ( ) } ) ;
hierarchy . Configured = true ;
2016-06-27 01:45:38 +02:00
#endif
2020-09-23 22:10:52 +02:00
_SessionFactories [ MultitenancyOperationContextExt . Current . Tenant ] = new Configuration ( ) . Configure ( BS . Shared . Core . Utils . CreateSavePath ( AppDomain . CurrentDomain . BaseDirectory , @"Multitenancy\" + MultitenancyOperationContextExt . Current . Tenant + ".config" ) ) . BuildSessionFactory ( ) ;
2016-06-27 01:45:38 +02:00
}
return _SessionFactories [ MultitenancyOperationContextExt . Current . Tenant ] ;
}
2025-01-31 15:15:40 +01:00
catch ( Exception e )
2016-06-27 01:45:38 +02:00
{
2025-01-31 15:15:40 +01:00
var path = Path . Combine ( AppDomain . CurrentDomain . BaseDirectory , "SessionFactoryError.txt" ) ;
if ( Directory . Exists ( _LogsDirectory ) )
{
File . AppendAllText ( Path . Combine ( _LogsDirectory , "WCFSessionError.txt" ) , $"SessionFactory get {DateTime.Now:dd.MM.yyyy HH:mm:ss}" + "\n" + e . Message + "\n" + e . StackTrace ) ;
}
2024-10-31 15:38:24 +01:00
2016-06-27 01:45:38 +02:00
Debug . WriteLine ( "<<<<< " + e . StackTrace + ( e . InnerException ! = null ? e . InnerException . Message : string . Empty ) ) ;
throw new FaultException ( e . StackTrace + e . InnerException ! = null ? e . InnerException . Message : string . Empty ) ;
}
finally
{
Monitor . Exit ( _Lock ) ;
}
}
}
public static void ResetAllSessionFactories ( )
{
try
{
String [ ] tenants = new String [ _SessionFactories . Keys . Count ] ;
_SessionFactories . Keys . CopyTo ( tenants , 0 ) ;
foreach ( var item in tenants )
{
ResetSessionFactory ( item ) ;
}
_SessionFactories = new Dictionary < string , ISessionFactory > ( ) ;
}
catch ( Exception )
{
//ignore?
}
}
public static void ResetSessionFactory ( string tenant )
{
String t = tenant ;
if ( String . IsNullOrEmpty ( t ) )
t = MultitenancyOperationContextExt . Current . Tenant ;
if ( _SessionFactories . ContainsKey ( t ) )
{
try
{
ISessionFactory fact = _SessionFactories [ t ] ;
if ( ! fact . IsClosed )
{
fact . Close ( ) ;
}
fact . Dispose ( ) ;
}
catch ( Exception )
{
//ignore exception??
}
_SessionFactories . Remove ( t ) ;
}
}
private static ISessionFactory RebuildSessionFactory ( )
{
ResetSessionFactory ( MultitenancyOperationContextExt . Current . Tenant ) ;
return SessionFactory ;
}
public static void CloseSession ( )
{
if ( CurrentSession ! = null )
{
CurrentSession . Close ( ) ;
CurrentSession . Dispose ( ) ;
CurrentSession = null ;
}
}
public static ISession OpenSession ( )
{
try
{
2025-04-03 10:25:09 +02:00
if ( ! TenantExists ( ) )
{
return null ;
}
2016-06-27 01:45:38 +02:00
Thread . CurrentThread . CurrentCulture = new CultureInfo ( "de-DE" ) ;
//System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
CloseSession ( ) ;
ISession session = SessionFactory . OpenSession ( new UpdInsInterceptor ( ) ) ;
if ( session = = null )
{
session = RebuildSessionFactory ( ) . OpenSession ( new UpdInsInterceptor ( ) ) ;
}
CurrentSession = session ;
return CurrentSession ;
}
catch ( Exception e )
{
2025-01-31 15:15:40 +01:00
if ( Directory . Exists ( _LogsDirectory ) )
{
File . AppendAllText ( Path . Combine ( _LogsDirectory , "WCFSessionError.txt" ) , $"OpenSession {DateTime.Now:dd.MM.yyyy HH:mm:ss}" + "\n" + e . Message + "\n" + e . StackTrace ) ;
}
2024-10-31 15:38:24 +01:00
2025-01-31 15:15:40 +01:00
throw new FaultException ( e . StackTrace + e . InnerException ? . Message ) ;
2016-06-27 01:45:38 +02:00
}
}
public static bool TenantExists ( )
{
2024-05-06 13:14:35 +02:00
var tenant = MultitenancyOperationContextExt . Current ? . Tenant ? ? SessionFacade . Tenant ;
2025-01-31 15:15:40 +01:00
var path = BS . Shared . Core . Utils . CreateSavePath ( AppDomain . CurrentDomain . BaseDirectory , @"Multitenancy\" + tenant + ".config" ) ;
2016-06-27 01:45:38 +02:00
return File . Exists ( path ) ;
}
}
}