public static IMessagingServices CreateMessagingServices() { try { // RabbitMQ is currently the only supported service return new RabbitMQMessagingServices(); } catch (Exception ex) { TechnicalProblemException tpEx = new TechnicalProblemException("Error creating rabbitmq messaging provider.", ex); Log.Error(tpEx.ToString()); throw tpEx; } }
/// <summary> /// Factory-method for different persistences. /// </summary> /// <param name="type">Type of persistence to create.</param> /// <param name="schemaUpdate">Indicates if schema is updated ('true') or recreated from scratch ('false').</param> /// <param name="persistenceService">Reference to persistence service.</param> /// <param name="transactionService">Reference to transaction service.</param> /// /// <param name="conversationFactory">Reference to conversation factory for creating conversations.</param> public static void CreatePersistenceService( PersistenceServiceType type, bool schemaUpdate, out IPersistenceServices persistenceService, out ITransactionServices transactionService, out IConversationFactory conversationFactory) { Configuration configuration = new Configuration(); FluentConfiguration fluentConfiguration = Fluently.Configure(configuration); // fetch connection string from configuration file System.Configuration.ConnectionStringSettings connectionSettings = System.Configuration.ConfigurationManager.ConnectionStrings["DatabaseConnection"]; Contract.Assert(connectionSettings != null, "A database connection setting needs to be defined in the App.config."); string connectionString = connectionSettings.ConnectionString; Contract.Assert(connectionString != null, "A database connection string needs to be defined in the App.config."); // set persistencetype switch (type) { case PersistenceServiceType.MSSQL2008: fluentConfiguration = fluentConfiguration.Database( MsSqlConfiguration.MsSql2008.ConnectionString(connectionString)); break; case PersistenceServiceType.MySQL: fluentConfiguration = fluentConfiguration.Database( MySQLConfiguration.Standard.ConnectionString(connectionString)); break; } // get all user assemblies ICollection<Assembly> allAssemblies = AppDomain.CurrentDomain.GetAssemblies().Where(assembly => assembly.ManifestModule.Name != "<In Memory Module>" && !assembly.FullName.StartsWith("mscorlib") && !assembly.FullName.StartsWith("System") && !assembly.FullName.StartsWith("Microsoft")).ToList(); foreach (Assembly mappingAssembly in allAssemblies) { // find all types that derive from ClassMap<> IList<Type> types = mappingAssembly.GetTypes().Where(t => t != typeof(AutoMapping<>) && t.BaseType != null && t.BaseType.IsGenericType && t.BaseType.GetGenericTypeDefinition() == typeof(ClassMap<>)).ToList(); // if there are any, we add their assembly if (types.Count > 0) { fluentConfiguration = fluentConfiguration.Mappings(m => m.FluentMappings.AddFromAssembly(mappingAssembly)); } } try { configuration = fluentConfiguration .ExposeConfiguration(cfg => { if (schemaUpdate) { new SchemaUpdate(cfg) .Execute(false, true); } else { new SchemaExport(cfg) .Create(false, true); } }) .BuildConfiguration(); } catch (FluentConfigurationException fluentEx) { if (fluentEx.InnerException != null) { if (fluentEx.InnerException is HibernateException) { if (fluentEx.InnerException.Message.Contains("Table") && fluentEx.InnerException.Message.Contains("already exists")) { TechnicalProblemException tpEx = new TechnicalProblemException("Error building FluentNHibernate configuration. Try dropping and re-creating database schema.", fluentEx); Log.Fatal(tpEx.ToString()); throw tpEx; } } } } catch (Exception ex) { TechnicalProblemException tpEx = new TechnicalProblemException("Error building FluentNHibernate configuration.", ex); Log.Fatal(tpEx.ToString()); throw tpEx; } NHibernatePersistenceServices nhPersistenceService = new NHibernatePersistenceServices(configuration); persistenceService = nhPersistenceService as IPersistenceServices; transactionService = nhPersistenceService as ITransactionServices; conversationFactory = persistenceService as IConversationFactory; }