Transform Web.config Hibernate configuration VS 2012 - c#

I have Web.config:
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" requirePermission="false"/>
</configSections>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.driver_class">NHibernate.Driver.OracleClientDriver</property>
<property name="connection.connection_string">{old_connection}</property>
<property name="dialect">NHibernate.Dialect.Oracle10gDialect</property>
<property name="proxyfactory.factory_class">NHibernate.Bytecode.DefaultProxyFactoryFactory, NHibernate</property>
<property name="show_sql">false</property>
<property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
</session-factory>
</hibernate-configuration>
</configuration>
I apply a transformation. Web.Release.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"
xmlns:hib="urn:nhibernate-configuration-2.2">
<configSections>
<section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" requirePermission="false"/>
</configSections>
<hib:hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<hib:session-factory>
<hib:property xdt:Transform="Replace" xdt:Locator="Match(name)" name="connection.connection_string">{new_connection}</hib:property>
</hib:session-factory>
</hib:hibernate-configuration>
</configuration>
Run in VS2012 in Release, the transformation does not occur. The string is not replaced.
In what could be the reason?

The transformation does not happen because the element names differs from your baseline Web.config. If you remove the hib namespace, transformation will take place.
<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property xdt:Transform="Replace" xdt:Locator="Match(name)" name="connection.connection_string">{new_connection}</property>
</session-factory>
</hibernate-configuration>
</configuration>
Another thing to note. If your current configuration is Release and you are running your application through Visual Studio, the server will point to the root of your project. In the root, you will have Web.config, Web.Debug.config and Web.Release.config and the server will pick-up the usual configuration file, without the transformation (ie Web.config).

Related

NHibernate searches for wrong filename

I'm new to nhibernate and tried a few tutorials but none of them helped with that problem:
I did my mappings, classes, ... and when I'm trying to add my class to configuration, hibernate is searching for the wrong filename (I guess in the wrong directory).
This is my hibernate.cfg.xml:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
</configSections>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="connection.connection_string">Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-WebApplication2-20170915093558.mdf;Initial Catalog=aspnet-WebApplication2-20170915093558;Integrated Security=True</property>
<property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
<mapping assembly="WebApplication1"/>
</session-factory>
</hibernate-configuration>
</configuration>
This is my Test-entity:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="WebApplication1"
namespace="WebApplication1.Domain">
<class name="Test" table="Test" lazy="false">
<id name="Id">
<generator class="native" />
</id>
<property name="Name" />
<property name="DatasetCreationDate" />
</class>
</hibernate-mapping>
namespace WebApplication1.Domain
{
public class Test
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual DateTime DatasetCreationDate { get; set; }
}
}
This is the code I'm now calling:
Configuration configuration = new Configuration();
configuration.AddAssembly(Assembly.GetCallingAssembly());
configuration.AddClass(typeof(Test));
configuration.Configure();
SessionFactory = configuration.BuildSessionFactory();
On this line I get following error by nhibernate:
configuration.AddClass(typeof(Test));
Resource not found: WebApplication1.Domain.Test.hbm.xml
This is my structure:
Any idea what is going wrong here? I think my file-structure is also wrong.
So I found the problems myself (with a bit of help by Radim Köhler):
1st problem
The properties for the .hbm.xml-files
Build Action: Embedded Resource.
Copy to Output Directory: Do not copy
2nd problem
I was adding the assembly twice.
Once in the hibernate.cfg.xml: <mapping assembly="WebApplication1"/>
and once when building the session factory: configuration.AddAssembly(Assembly.GetCallingAssembly());
I now deleted the one in the hibernate.cfg.xml

migrate Configuration Management Application Block .net1.1 to system.configuration

I have to migrate a large .net 1.1 application to .net 3.5.
The system used Configuration Management Application Block (CMAB) to load config files starting with the app.config file as:
<configuration>
<configSections>
<section
name="applicationConfigurationManagement"
type="Microsoft.ApplicationBlocks.ConfigurationManagement.ConfigurationManagerSectionHandler,Microsoft.ApplicationBlocks.ConfigurationManagement, Version=1.0.0.0,Culture=neutral,PublicKeyToken=e64cb664730084d3" />
<section
name="MyAppSystemSettings"
type="Microsoft.ApplicationBlocks.ConfigurationManagement.XmlHashtableSectionHandler,Microsoft.ApplicationBlocks.ConfigurationManagement, Version=1.0.0.0,Culture=neutral,PublicKeyToken=e64cb664730084d3" />
</configSections>
<appSettings>
<!-- Key / Value Pairs -->
</appSettings>
<!-- ## Configuration Management Settings ## -->
<applicationConfigurationManagement defaultSection="MyAppSystemSettings">
<configSection name="MyAppSystemSettings">
<configCache enabled="true" refresh="* 6 * * *" />
<configProvider
assembly="Microsoft.ApplicationBlocks.ConfigurationManagement, Version=1.0.0.0,Culture=neutral,PublicKeyToken=e64cb664730084d3"
type="Microsoft.ApplicationBlocks.ConfigurationManagement.Storage.XmlFileStorage"
signed="false"
refreshOnChange="false"
encrypted="false"
path="C:\Program Files\MyApp\Config\MyAppSystemSettings.Config" />
</configSection>
</applicationConfigurationManagement>
</configuration>
As can be seen this is referencing the MyAppSystemSettings.Config 'sub' config file which looks like:
<configuration>
<MyAppSystemSettings>
<XmlSerializableHashtable xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Entries>
<Entry>
<key xsi:type="xsd:string">Key0</key>
<value xsi:type="xsd:string">Value0</value>
</Entry>
<Entry>
<key xsi:type="xsd:string">Key1</key>
<value xsi:type="xsd:string">Value1</value>
</Entry>
<Entry>
<key xsi:type="xsd:string">ExtraConfigFile</key>
<value xsi:type="xsd:string">C:\Program Files\MyApp\Config\ExtraConfig.xml</value>
</Entry>
</XmlSerializableHashtable>
</MyAppSystemSettings>
</configuration>
Considering that the app.config file refers to 10+ 'sub' config files similar to MyAppSystemSettings.Config and that they each contain 100+ entries I'm looking for the simplest way to migrate them to use System.Configuration correctly to give me the same results as the .net 1.1 solution using CMAB?

How to create a Spring object type Integer?

Could anyone please help me take a look at this issue since I can only create a Spring object type String? When I try to create another Spring object type, I get the below error
Error
Class Initialization method MessengerLyncSDK2013.Testcases.Test.UnitTest1.ClassInitialize threw exception. System.Configuration.ConfigurationErrorsException: System.Configuration.ConfigurationErrorsException: Error creating context 'spring.root': Error creating object with name 'serverPort' defined in 'config [D:\Working Projects\lync2013\MessengerLyncSDK2013\TestResults\thanh.viet.le_LGVN13307-WIN7 2014-03-17 11_17_21\Out\MessengerLyncSDK2013.DLL.config#spring/objects] line 9' : Could not resolve matching constructor. ---> Spring.Objects.Factory.ObjectCreationException: Error creating object with name 'serverPort' defined in 'config [D:\Working Projects\lync2013\MessengerLyncSDK2013\TestResults\thanh.viet.le_LGVN13307-WIN7 2014-03-17 11_17_21\Out\MessengerLyncSDK2013.DLL.config#spring/objects] line 9' : Could not resolve matching constructor..
Spring object in xml file
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="spring">
<section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core" />
<section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
</sectionGroup>
</configSections>
<spring>
<context>
<resource uri="config://spring/objects" />
</context>
<objects xmlns="http://www.springframework.net" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.net http://www.springframework.net/xsd/spring-objects.xsd">
<object id="connectServer" type="string">
<constructor-arg value="server.com"/>
</object>
<object id="serverPort" type="System.Int32" factory-method="Copy">
<constructor-arg index="0">
<value>5222</value>
</constructor-arg>
</object>
</objects>
</spring>
</configuration>
For more details, I am using visual studio 2010 with C#.
Try it, note for factory-method="Parse":
<object id="MyInt" type="System.Int32" factory-method="Parse">
<constructor-arg index="0">
<value>123</value>
</constructor-arg>
</object>
See also: How do I create a spring .Net standalone object of type Int32 defined in the IOC context file?
You can create an object with all your configuration and then just inject it:
<object id="ServerConfig" type"...">
<property name="ServerPort" value="5222"/>
...
</object>
<object id="Server" type"...">
<!-- Constructor injection -->
<constructor-arg name="configuration" ref="ServerConfig"/>
<!-- OR Property injection -->
<property name="Configuration" ref="ServerConfig"/>
</object>

Split EnterpriseLibrary Block configurations into multiple files

Is it possible to split the configuration for an enterprise library block into multiple files?
For example: I have three assemblies and one hosting project. I want to store the entlib v6 Exception Handling Application Block (EHAB) configuration for each assembly in a separate config file located in the particular assembly.
The hosting project references the three assemblies:
assembly_X
ehabX.config
assembly_Y
ehabY.config
assembly_Z
ehabZ.config
Hosting project
using ehabX.config
using ehabY.config
using ehabZ.config
I already tried the following:
App.config in the hosting project:
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="enterpriseLibrary.ConfigurationSource" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ConfigurationSourceSection, Microsoft.Practices.EnterpriseLibrary.Common" requirePermission="true" />
</configSections>
<enterpriseLibrary.ConfigurationSource>
<sources>
<add name="ehabX" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common" filePath="ExceptionHandling\ehabX.config" />
<add name="ehabY" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common" filePath="ExceptionHandling\ehabY.config" />
<add name="ehabZ" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common" filePath="ExceptionHandling\ehabZ.config" />
</sources>
<redirectSections>
<add sourceName="ehabX" name="exceptionHandling" />
<add sourceName="ehabY" name="exceptionHandling" />
<add sourceName="ehabZ" name="exceptionHandling" />
</redirectSections>
</enterpriseLibrary.ConfigurationSource>
</configuration>
ehabX.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling"/>
</configSections>
<exceptionHandling>
<exceptionPolicies>
<add name="Swallow NotImplementedException">
<exceptionTypes>
<add type="System.NotImplementedException, mscorlib" postHandlingAction="None" name="NotImplementedException"/>
</exceptionTypes>
</add>
</exceptionPolicies>
</exceptionHandling>
</configuration>
ehabY.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling"/>
</configSections>
<exceptionHandling>
<exceptionPolicies>
<add name="Swallow ArgumentException">
<exceptionTypes>
<add type="System.ArgumentException, mscorlib" postHandlingAction="None" name="NotImplementedException"/>
</exceptionTypes>
</add>
</exceptionPolicies>
</exceptionHandling>
</configuration>
The ehabZ.config is omitted.
Using the EHAB with the policy "Swallow NotImplementedException" works fine. But if I try to use the policy "Swallow ArgumentException" defnied in ehabY.config I get this error message:
Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionHandlingException: The policy with name 'Swallow ArgumentException' cannot be found. Exception handling aborted.
Any suggestions?
Unfortunately, there is nothing out of the box that will let you merge multiple configuration sources into one configuration set.
I think you can probably do what you want but you will have to do some coding. You'll need to read in the appropriate configuration sources and create a custom IConfigurationSource that will merge them all. Additive merge of different config sources? has an example of a MergeConfigurationSource that could help you.

Could not compile the mapping document, Could not instantiate dialect class Nhibernate.Dialect.MsSql2008Dialect

I am using Visual Studio 2010 with NHibernate 3.2.0.GA, I do have a web application with the following Web.Config File:
<?xml version="1.0"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<configSections>
<section name="hibernate-configuration"
type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate"/>
</configSections>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory name ="Nhibernate.Test">
<property name="connection.driver_class">
NHibernate.Driver.SqlClientDriver</property>
<property name="connection.connection_string">
Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Mahshid\Desktop\BugTracker\BugTracker\App_Data\BugTrackerDB.mdf;
Integrated Security=True;User Instance=True
</property>
<property name ="adonet.batch_size">10</property>
<property name="show_sql">true</property>
<property name="dialect">Nhibernate.Dialect.MsSql2008Dialect</property>
<property name="command_timeout">60</property>
<property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
<property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory,
NHiberante.ByteCode.LinFu</property>
</session-factory>
</hibernate-configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
</configuration>
I added my sql database as a local one inside vs2010, I also do have two nhibernate hbm.xml files with as follows:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
namespace="BugTracker.Model"
assembly ="BugTracker">
<class name="Bug" table ="Bugs" lazy="false">
<id name ="BugId" column ="BugId" type="int"
unsaved-value="0">
<generator class="native"/>
</id>
<property name="Desc" column="Description"/>
<property name="Fixed" column ="Fixed"/>
<many-to-one name="Application"
class="Application"
column="ApplicationId"
cascade="all"
not-null="true"/>
</class>
</hibernate-mapping>
And:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="BugTracker.Model"
assembly="BugTracker">
<class name="Application" table="Applications" lazy="false">
<id name="ApplicationId" column ="ApplicationId" type="int" unsaved-value ="0">
<generator class ="native"></generator>
</id>
<property name ="Name" column="Name"/>
<component access ="field.camelcase-underscore" name ="Developer"
class="Developer">
<property access ="field.camelcase-underscore"
column ="DeveloperFirstName" name="FirstName"/>
<property access ="field.camelcase-underscore"
column="DeveloperLastName" name="LastName"/>
</component>
<bag cascade="all-delete-orphan"
inverse ="true"
name ="Bugs"
lazy="false"
access ="field.camelcase-underscore">
<key column ="ApplicationId"/>
<one-to-many class ="Bug"/>
</bag>
</class>
</hibernate-mapping>
I set them as embedded resource, believe me but I get the exception in the following code part:
private static void Init()
{
NHibernate.Cfg.Configuration config;
config = new NHibernate.Cfg.Configuration();
config.AddAssembly("BugTracker");
config.Configure();
_SessionFactory = config.BuildSessionFactory();
}
With this message:
Could not compile the mapping document: BugTracker.Model.Bug.hbm.xml
Inner Exception Message:
Could not instantiate dialect class Nhibernate.Dialect.MsSql2008Dialect
I know that this is beginer's issue but i am just a newbie! but i appreciate your ideas...
Shouldn't the name of dialect be NHibernate.Dialect.MsSql2008Dialect? I mean H must be capital?

Categories