using System; using System.Collections.Generic; using System.Diagnostics; using System.Globalization; using System.IO; using System.ServiceModel; using System.Threading; using log4net; using log4net.Appender; using log4net.Layout; using log4net.Repository.Hierarchy; using NHibernate; using NHibernate.Cfg; namespace BeWo.Data { public class DefaultHibernateSessionManager { private static Dictionary _SessionFactories = new Dictionary(); private static object _Lock = string.Empty; private static ISession currentSession; public static ISession CurrentSession { get { var frame = new StackFrame(1); var method = frame.GetMethod(); var callingMethodName = method.Name; return currentSession ?? OpenSession(); } set { currentSession = value; } } public static string CurrentTenant { get; set; } private static ISessionFactory SessionFactory { get { try { Monitor.Enter(_Lock); if (!_SessionFactories.ContainsKey(CurrentTenant)) { #if DEBUG //TODO: wieder einkommentieren! //var hierarchy = (Hierarchy)LogManager.GetRepository(); //var logger = (Logger)hierarchy.GetLogger("NHibernate.SQL"); //logger.AddAppender(new TraceAppender { Layout = new SimpleLayout() }); //hierarchy.Configured = true; #endif var baseDirectory = AppDomain.CurrentDomain.BaseDirectory; var splittedBaseDirectory = baseDirectory.Split('\\'); // 0 -3 var correctBaseDirectory = string.Empty; for(var i = 0; i < 4; i++) { correctBaseDirectory += splittedBaseDirectory[i] + "\\"; } _SessionFactories[CurrentTenant] = new Configuration().Configure(BS.Shared.Core.Utils.CreateSavePath(correctBaseDirectory, @"Multitenancy\" + CurrentTenant + ".config")).BuildSessionFactory(); } return _SessionFactories[CurrentTenant]; } catch (Exception e) { //String path = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SessionFactoryError.txt"); //System.IO.File.AppendAllText(path, String.Format("{0:dd.MM.yyyy HH:mm:ss}", DateTime.Now) + "\n" + e.Message + "\n" + e.StackTrace); Debug.WriteLine("<<<<< " + e.StackTrace + e.InnerException?.Message ?? string.Empty); throw new FaultException(e.StackTrace + e.InnerException?.Message ?? string.Empty); } finally { Monitor.Exit(_Lock); } } } private static ISessionFactory RebuildSessionFactory() { ResetSessionFactory(CurrentTenant); return SessionFactory; } public static void ResetAllSessionFactories() { try { var tenants = new string[_SessionFactories.Keys.Count]; _SessionFactories.Keys.CopyTo(tenants, 0); foreach (var item in tenants) { ResetSessionFactory(item); } _SessionFactories = new Dictionary(); } catch (Exception) { //ignore? } } public static void ResetSessionFactory(string tenant) { var t = tenant; if (string.IsNullOrEmpty(t)) { t = CurrentTenant; } if (_SessionFactories.ContainsKey(t)) { try { var fact = _SessionFactories[t]; if (!fact.IsClosed) { fact.Close(); } fact.Dispose(); } catch (Exception) { //ignore exception?? } _SessionFactories.Remove(t); } } public static void CloseSession() { if (currentSession != null) { currentSession.Close(); currentSession.Dispose(); currentSession = null; } } public static ISession OpenSession() { try { Thread.CurrentThread.CurrentCulture = new CultureInfo("de-DE"); CloseSession(); var session = SessionFactory.OpenSession(new UpdInsInterceptor()) ?? RebuildSessionFactory().OpenSession(new UpdInsInterceptor()); currentSession = session; return currentSession; } catch (Exception e) { //throw new FaultException("ttttt" + e.ToString()); throw new FaultException(e.StackTrace + e.InnerException?.Message ?? string.Empty); } } public static bool TenantExists() { var path = BS.Shared.Core.Utils.CreateSavePath(AppDomain.CurrentDomain.BaseDirectory, @"Multitenancy\" + CurrentTenant + ".config"); return File.Exists(path); } } }