Skip to content

Commit ce9f9d4

Browse files
committed
NH-3807 - Explore alternate way to set global settings in .NET Core.
Can't rely on app.xml for unit tests because there is no App Domains and Assembly.GetEntryAssembly() isn't set correctly.
1 parent 09c50f4 commit ce9f9d4

File tree

4 files changed

+63
-21
lines changed

4 files changed

+63
-21
lines changed

src/NHibernate.Test/App.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
<property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
3636

3737
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver, NHibernate.Driver.SqlClient</property> <!-- Shouldn't be necessary, but is required by some tests -->
38-
<property name="connection.connection_string_name">TestConnectionString</property>
38+
<property name="connection.connection_string">Server=localhost\sqlexpress;Database=nhibernate;Integrated Security=SSPI</property>
3939
<property name="connection.provider">NHibernate.Test.DebugConnectionProvider, NHibernate.Test</property>
4040
<property name="connection.isolation">ReadCommitted</property> <!-- See System.Data.IsolationLevel for valid values -->
4141
<!--property name="connection.connection_string" /-->

src/NHibernate.Test/ConnectionStringTest/NamedConnectionStringFixture.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public void ConnectionStringInSettingsOverrideNamedConnectionSTring()
3232
Assert.AreEqual(conStr, cp.PublicConnectionString);
3333
}
3434

35+
#if !NETCOREAPP2_0
3536
[Test]
3637
public void CanGetNamedConnectionStringFromConfiguration()
3738
{
@@ -42,6 +43,7 @@ public void CanGetNamedConnectionStringFromConfiguration()
4243

4344
Assert.AreEqual("TestConnectionString-TestConnectionString", cp.PublicConnectionString);
4445
}
46+
#endif
4547
}
4648

4749
public class MockConnectionProvider : ConnectionProvider

src/NHibernate.Test/TestsContext.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System.Configuration;
2+
using System.IO;
3+
using System.Xml;
4+
5+
using NHibernate.Cfg.ConfigurationSchema;
6+
using NUnit.Framework;
7+
8+
namespace NHibernate.Test
9+
{
10+
[SetUpFixture]
11+
public class TestsContext
12+
{
13+
[OneTimeSetUp]
14+
public void RunBeforeAnyTests()
15+
{
16+
string assemblyPath = Path.Combine(TestContext.CurrentContext.TestDirectory, @"NHibernate.Test.dll");
17+
var configuration = ConfigurationManager.OpenExeConfiguration(assemblyPath);
18+
ConfigurationSection configSection = configuration.GetSection(CfgXmlHelper.CfgSectionName);
19+
20+
using (XmlTextReader reader = new XmlTextReader(configSection.SectionInformation.GetRawXml(), XmlNodeType.Document, null))
21+
{
22+
HibernateConfiguration config = new HibernateConfiguration(reader);
23+
NHibernate.Cfg.Environment.InitializeGlobalProperties(config);
24+
}
25+
}
26+
}
27+
}

src/NHibernate/Cfg/Environment.cs

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -209,27 +209,19 @@ static Environment()
209209
}
210210

211211
GlobalProperties = new Dictionary<string, string>();
212-
GlobalProperties[PropertyUseReflectionOptimizer] = bool.TrueString;
213-
LoadGlobalPropertiesFromAppConfig();
214-
VerifyProperties(GlobalProperties);
215-
216-
BytecodeProviderInstance = BuildBytecodeProvider(GlobalProperties);
217-
EnableReflectionOptimizer = PropertiesHelper.GetBoolean(PropertyUseReflectionOptimizer, GlobalProperties);
218-
219-
if (EnableReflectionOptimizer)
220-
{
221-
log.Info("Using reflection optimizer");
222-
}
212+
InitializeGlobalProperties(LoadGlobalPropertiesFromAppConfig());
223213
}
224214

225-
private static void LoadGlobalPropertiesFromAppConfig()
215+
private static IHibernateConfiguration LoadGlobalPropertiesFromAppConfig()
226216
{
227-
object config = ConfigurationManager.GetSection(CfgXmlHelper.CfgSectionName);
217+
var configuration = ConfigurationManager.OpenExeConfiguration(@"C:\dev\OSS\nhibernate-core\src\NHibernate.Test\bin\Debug\netcoreapp2.0\NHibernate.Test.dll");
218+
object config = configuration.GetSection(CfgXmlHelper.CfgSectionName);
219+
//object config = ConfigurationManager.GetSection(CfgXmlHelper.CfgSectionName);
228220

229221
if (config == null)
230222
{
231223
log.Info(string.Format("{0} section not found in application configuration file", CfgXmlHelper.CfgSectionName));
232-
return;
224+
return null;
233225
}
234226

235227
var nhConfig = config as IHibernateConfiguration;
@@ -239,18 +231,39 @@ private static void LoadGlobalPropertiesFromAppConfig()
239231
string.Format(
240232
"{0} section handler, in application configuration file, is not IHibernateConfiguration, section ignored",
241233
CfgXmlHelper.CfgSectionName));
242-
return;
234+
return null;
243235
}
244236

245-
GlobalProperties[PropertyBytecodeProvider] = nhConfig.ByteCodeProviderType;
246-
GlobalProperties[PropertyUseReflectionOptimizer] = nhConfig.UseReflectionOptimizer.ToString();
247-
if (nhConfig.SessionFactory != null)
237+
return nhConfig;
238+
}
239+
240+
public static void InitializeGlobalProperties(IHibernateConfiguration nhConfig)
241+
{
242+
GlobalProperties.Clear();
243+
GlobalProperties[PropertyUseReflectionOptimizer] = bool.TrueString;
244+
245+
if (nhConfig != null)
248246
{
249-
foreach (var kvp in nhConfig.SessionFactory.Properties)
247+
GlobalProperties[PropertyBytecodeProvider] = nhConfig.ByteCodeProviderType;
248+
GlobalProperties[PropertyUseReflectionOptimizer] = nhConfig.UseReflectionOptimizer.ToString();
249+
if (nhConfig.SessionFactory != null)
250250
{
251-
GlobalProperties[kvp.Key] = kvp.Value;
251+
foreach (var kvp in nhConfig.SessionFactory.Properties)
252+
{
253+
GlobalProperties[kvp.Key] = kvp.Value;
254+
}
252255
}
253256
}
257+
258+
VerifyProperties(GlobalProperties);
259+
260+
BytecodeProviderInstance = BuildBytecodeProvider(GlobalProperties);
261+
EnableReflectionOptimizer = PropertiesHelper.GetBoolean(PropertyUseReflectionOptimizer, GlobalProperties);
262+
263+
if (EnableReflectionOptimizer)
264+
{
265+
log.Info("Using reflection optimizer");
266+
}
254267
}
255268

256269
internal static void ResetSessionFactoryProperties()

0 commit comments

Comments
 (0)