How do I resolve NHibernate.MappingException - c#

I have a database, hbm mapping file and the App.config located in a class library. Now from a test project I reference that library and attempt to call a HibernateHelper class I create, at runtime the following error is thrown :
NHibernate.MappingException : Could not compile the mapping document: HibernateExample.Mappings.Products.hbm.xml
Please keep in mind that this is a class library that is being reference from a Test project.
If I change it output type to console application, it runs fine. But when I change it back to class library and reference it from my Test Project it throws the above mention error.
I tried adding config.Configure() but that throws a NhibernateDuplicateMapping exception.
FIXED:
Fixed the duplication mapping issue by removing from appconfig. and fixed the problem mapping entity by placing a hibernate.cfg.xml file in my Test project as well.
public sealed class NHibernateHelper
{
private static ISessionFactory _sessionFactory;
const string Connectionstring = "servicestring";
public static void OpenSession()
{
var config = new Configuration();
config.Configure();
config.AddAssembly(Assembly.GetCallingAssembly());
_sessionFactory = config.BuildSessionFactory();
}
public static ISession GetCurrentSession()
{
ISession session = null;
if (_sessionFactory == null)
OpenSession();
if (_sessionFactory != null)
{
session = _sessionFactory.OpenSession();
}
return session;
}
public static void CloseSessionFactory()
{
if (_sessionFactory != null)
{
_sessionFactory.Close();
}
}
// var dsn = ConfigurationManager.ConnectionStrings[Connectionstring].ConnectionString;
//config.SessionFactory().Integrate.Using<MsSqlCeDialect>().Connected.ByAppConfing(dsn);
// System.Diagnostics.Debug.WriteLine("My connection string: "+dsn);
//Get NHibernate configuration
//_sessionFactory = config.BuildSessionFactory();
//config.AddAssembly("HibernateExample");
}
Any ideas?
<?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.driver_class"> NHibernate.Driver.SqlServerCeDriver</property>
<property name="dialect">NHibernate.Dialect.MsSqlCeDialect</property>
<property name="connection.connection_string">Data Source=FirstSample.sdf;</property>
<property name="show_sql">true</property>
<mapping assembly="HibernateExample"/>
</session-factory>
</hibernate-configuration>
<connectionStrings>
<add name="testconnectionstring"
connectionString="Data Source=|DataDirectory|\FirstSample.sdf;Integrated Security=True"
providerName="Microsoft.SqlServerCe.Client.3.5" />
</connectionStrings>
<runtime>
<assemblyBinding xmlns="urnchemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Data.SqlServerCe" publicKeyToken="89845DCD8080CC91" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-9.0.242.0" newVersion="3.5.0.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="HibernateExample" namespace="HibernateExample.Domain" >
<class name="Product" table="Products">
<id name="Id" type="integer">
<generator class="identity"/>
</id>
<property name="Name" type="string"/>
<property name="Category" type="string"/>
<property name="Discontinued" />
</class>
</hibernate-mapping>
Exception Thrown:
Test 'NunitTest.TestClass.canquerydb' failed: NHibernate.MappingException : Could not compile the mapping document: HibernateExample.Mappings.Products.hbm.xml
----> System.InvalidOperationException : Could not find the dialect in the configuration
at NHibernate.Cfg.Configuration.LogAndThrow(Exception exception)
at NHibernate.Cfg.Configuration.AddDeserializedMapping(HbmMapping mappingDocument, String documentFileName)
at NHibernate.Cfg.Configuration.ProcessMappingsQueue()
at NHibernate.Cfg.Configuration.AddInputStream(Stream xmlInputStream, String name)
at NHibernate.Cfg.Configuration.AddResource(String path, Assembly assembly)
at NHibernate.Cfg.Configuration.AddAssembly(Assembly assembly)
at NHibernate.Cfg.Configuration.AddAssembly(String assemblyName)
NHibernateTest\NHibernateHelper.cs(21,0): at HibernateExample.NHibernateTest.NHibernateHelper.openSession()
NHibernateTest\NHibernateHelper.cs(28,0): at HibernateExample.NHibernateTest.NHibernateHelper.GetCurrentSession()
TestClass.cs(21,0): at NunitTest.TestClass.canquerydb()
--InvalidOperationException
at NHibernate.Dialect.Dialect.GetDialect(IDictionary`2 props)
at NHibernate.Cfg.Configuration.AddDeserializedMapping(HbmMapping mappingDocument, String documentFileName)

From the error, it appears that you are not configuring the Dialect before adding the mapping. This is required.
Here's a simple piece of basic configuration code:
var configuration = new Configuration();
configuration.SessionFactory().Integrate.Using<MsSql2012Dialect>()
.Connected.ByAppConfing("connName");//sic
//now you can add the mappings

Related

System.Configuration.ConfigurationErrorsException - Unrecognized element 'setting'

Unrecognized element 'setting'. (C:\Dev\DOT.NET\AutoPay\ibeam.config line 15)
AppInfo.Sections.Get("AutoPay.Common.Credentials") 'AppInfo.Sections.Get("AutoPay.Common.Credentials")'
threw an exception of type
'System.Configuration.ConfigurationErrorsException' System.Configuration.ConfigurationSection
{System.Configuration.ConfigurationErrorsException}
My config file:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="AppInfo" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<section name="AutoPay.Common.Credentials"
type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
allowExeDefinition="MachineToLocalUser"
requirePermission="false" />
</sectionGroup>
</configSections>
<AppInfo>
<AutoPay.Common.Credentials>
<setting name="UserName" serializeAs="String"><value>********</value></setting>
<setting name="Password" serializeAs="String"><value>********</value></setting>
<setting name="ServiceUrl" serializeAs="String"><value>********</value></setting>
</AutoPay.Common.Credentials>
</AppInfo>
</configuration>
Base (abstract) class:
public abstract class BaseConfigInfo : ConfigurationSectionGroup
{
protected Configuration Configuration;
protected ConfigurationSectionGroup AppInfo;
protected ClientSettingsSection Credentials;
protected BaseConfigInfo(string configFile)
{
var fileMap = new ConfigurationFileMap(configFile);
Configuration = ConfigurationManager.OpenMappedMachineConfiguration(fileMap);
AppInfo = Configuration.GetSectionGroup("AppInfo");
// THIS IS WHERE THE ERROR POPS //
Credentials = (ClientSettingsSection)AppInfo.Sections.Get("AutoPay.Common.Credentials");
}
protected string GetCredentialsString(string kvpName)
{
var setting = Credentials.Settings.Get(kvpName);
return setting.Value.ValueXml.InnerText;
}
public string UserName { get { return GetCredentialsString("UserName"); } }
public string Password { get { return GetCredentialsString("Password"); } }
public string ServiceUrl { get { return GetCredentialsString("ServiceUrl"); } }
}
Sample concrete class:
public class ConfigInfoIbeam : BaseConfigInfo
{
public ConfigInfoIbeam() : base(ConfigurationManager.AppSettings["ConfigInfoIbeam"]) { }
}
I know I am missing something simple here. I'm running in .NET 4. Nothing too usefull showing up when I try to google for this error. Any ideas?
I found the problem. In my config I had this:
<AppInfo>
<AutoPay.Common.Credentials>
<setting name="UserName" serializeAs="String"><value>********</value></setting>
<setting name="Password" serializeAs="String"><value>********</value></setting>
<setting name="ServiceUrl" serializeAs="String"><value>********</value></setting>
</AutoPay.Common.Credentials>
</AppInfo>
I changed it to:
<AppInfo>
<AutoPay.Common.Credentials>
<setting name="UserName" serializeAs="String">
<value>********</value>
</setting>
<setting name="Password" serializeAs="String">
<value>********</value>
</setting>
<setting name="ServiceUrl" serializeAs="String">
<value>********</value>
</setting>
</AutoPay.Common.Credentials>
</AppInfo>
Don't ask me why the underlying xml reader cannot differentiate between the two. However, problem solved! grrrr...
I think you need <settings> </settings> wrapping your three <setting> ......

An unhandled exception of type 'System.IO.FileNotFoundException' - Dll & remoting

I'm trying to make a game of Nim where the logic takes place on a server and a client presents the game to get better at .NET Remoting.
I have a dll I built from this class library:
namespace Nim_Common
{
public interface computerCommon
{
int[] startGame(int columnNumber);
int[] computeTurn(int[] penStatus);
bool checkWin();
}
}
That dll I add as a reference to my client project and my server project, and add the dll to the bin/Debug directory in each project.
This is the relevant part of my client code:
using Nim_Common;
namespace Nim
{
public partial class Form1 : Form
{
private computerCommon computerServerHandler;
...
public Form1()
{
InitializeComponent();
computerToolStripMenuItem.Select();
newColumnNumber = -1;
RemotingConfiguration.Configure("client.exe.config");
computerServerHandler = (computerCommon)Activator.GetObject(typeof(computerCommon), "http://localhost:1234/_Server_");
StartGame(this, null);
}
private void StartGame(object sender, EventArgs e)
{
int max = 0;
if(columns != null)
for (int i = 0; i < columns.Length; i++) Controls.Remove(columns[i]);
int[] temp = computerServerHandler.startGame(newColumnNumber);
columns = new Column[temp.Length];
...
}
And in the server part:
using Nim_Common;
namespace Nim_Server
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
RemotingConfiguration.Configure("server.exe.config");
}
}
class ServerPart : MarshalByRefObject, computerCommon
{
...
public ServerPart()
{
...
}
public int[] startGame(int columnNumber)
{
...
}
public int[] computeTurn(int[] penStatus)
{
...
}
public bool checkWin()
{
...
}
}
}
server.exe.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<system.runtime.remoting>
<application>
<channels>
<channel ref="http server" port="1234" />
</channels>
<service>
<wellknown mode ="SingleCall" type="Nim_Server.ServerPart, Nim_Server" objectUri="_Server_" />
</service>
</application>
</system.runtime.remoting>
</configuration>
client.exe.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<system.runtime.remoting>
<application>
<channels>
<channel ref="http client" />
</channels>
<client>
<wellknown type="Nim_Common.computerCommon, Nim_Common" url="http://localhost:1234" />
</client>
</application>
</system.runtime.remoting>
</configuration>
My firewall is off if that matters.
Everything is fine in compile time, the server runs fine. When the client reaches this line, it throws the exception:
int[] temp = computerServerHandler.startGame(newColumnNumber);
The exception is something like:
An unhandled exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll
Additional information: Could not load file or one of its dependencies, the system could not find "Nim_Common". // This is shown in my native language so I'm improvising a bit with the translation.
What is going on and how do I fix this?
Thanks.
I was receiving this error from one of my own compiled dlls. It turns out I had filled in the AssemblyCultureAttribute in my AssemblyInfo.cs file. The project built without issue but would not load in any referenced projects when used.
According to msdn :
Putting this attribute on an assembly and using something other than the empty string ("") for the culture name will make this assembly look like a satellite assembly, rather than a main assembly that contains executable code. Labeling a traditional code library with this attribute will break it, because no other code will be able to find the library's entry points at runtime.

Could not instantiate connection provider: NHibernate.Driver.MySqlDataDriver

I'm making a simple web project using NHibernate and i'm stuck at this error whenever i try to build the sessionfactory.
The line that causes the exception is this
ISessionFactory sessionFactory = new Configuration().Configure().BuildSessionFactory();
People with similar problem seems to solve them by referencing Mysql.data.dll which i've already done, and checked that the dll is in my bin folder.
i suspect the fault lies in my hibernate.cfg.xml which looks like this
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="dialect">NHibernate.Dialect.MySQLDialect</property>
<property name="connection.provider">NHibernate.Driver.MySqlDataDriver</property>
<property name="connection.driver_class">NHibernate.Driver.SqlServerCeDriver</property>
<property name="connection.connection_string">connectionstring</property>
<property name='proxyfactory.factory_class'>NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
<mapping assembly="Mcgvd" />
</session-factory>
</hibernate-configuration>
the tutorial i followed to make this project was using a helperclass to create the sessionfactory looking like this
public sealed class NHibernateHelper
{
private const string CurrentSessionKey = "nhibernate.current_session";
private static readonly ISessionFactory sessionFactory;
static NHibernateHelper()
{
sessionFactory = new Configuration().Configure().BuildSessionFactory();
}
public static ISession GetCurrentSession()
{
HttpContext context = HttpContext.Current;
ISession currentSession = context.Items[CurrentSessionKey] as ISession;
if (currentSession == null)
{
currentSession = sessionFactory.OpenSession();
context.Items[CurrentSessionKey] = currentSession;
}
return currentSession;
}
public static void CloseSession()
{
HttpContext context = HttpContext.Current;
ISession currentSession = context.Items[CurrentSessionKey] as ISession;
if (currentSession == null)
{
// No current session
return;
}
currentSession.Close();
context.Items.Remove(CurrentSessionKey);
}
public static void CloseSessionFactory()
{
if (sessionFactory != null)
{
sessionFactory.Close();
}
}
}
hibernate.cfg.xml and hibernate.hbm.xml are both located at the root of my project.
What am I doing wrong here ?
Your configuration is wrong.
<property name="connection.provider">NHibernate.Driver.MySqlDataDriver</property>
<property name="connection.driver_class">NHibernate.Driver.SqlServerCeDriver</property>
You've specified a driver for the connection provider, and a SQL Server CE driver when you're apparently using MySQL. Try:
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.driver_class">NHibernate.Driver.MySqlDataDriver</property>
besides the fault Jamie found i had to change the connection.provider property where i for some reason had put mysqldatadriver.
this is the working configuration i ended up with.
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="dialect">NHibernate.Dialect.MySQLDialect</property>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.driver_class">NHibernate.Driver.MySqlDataDriver</property>
<property name="connection.connection_string">connectionstring</property>
<property name='proxyfactory.factory_class'>NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
<mapping assembly="Mcgvd" />
</session-factory>
</hibernate-configuration>

The unit test adapter failed to connect to the data source

I'm having an issue with my Datasource for my unit test. I'm wanting to keep the records in XML. As far as I can tell this is supported but I keep getting this error "The unit test adapter failed to connect to the data source...".
I have set up my app.config as follows:
<configuration>
<configSections>
<section name="microsoft.visualstudio.testtools" type="Microsoft.VisualStudio.TestTools.UnitTesting.TestConfigurationSection, Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
</configSections>
<connectionStrings>
<add name="PersonTestData" connectionString="Dsn=XML Files;dbq=PersonTestData.xml;defaultdir=.\; driverid=790;maxbuffersize=2048;pagetimeout=5" providerName="System.Data.Odbc" />
</connectionStrings>
<microsoft.visualstudio.testtools>
<dataSources>
<add name="PersonTestData" connectionString="PersonTestData" dataTableName="PersonData" dataAccessMethod="Sequential"/>
</dataSources>
</microsoft.visualstudio.testtools>
</configuration>
The Code that I'm using is this:
[TestMethod()]
[DeploymentItem("PersonTestData.xml")]
[DataSource("PersonTestData")]
public void CompareToTest()
{
Person Test = (Person)TestContext.DataRow["Person"];
Int32 result = Main.CompareTo(Test);
Assert.IsNotNull(result);
}
And Finally the XML file It's self:
<?xml version="1.0" encoding="utf-8" ?>
<PersonData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Person>
<LastName>Jones</LastName>
<FirstName>Bill</FirstName>
<Age>24</Age>
</Person>
<Person>
<LastName>West</LastName>
<FirstName>John</FirstName>
<Age>24</Age>
</Person>
<Person>
<LastName>Jones</LastName>
<FirstName>Bill</FirstName>
<Age>24</Age>
</Person>
</PersonData>
Not sure where I'm going wrong at this point.
I think you don't need an ODBC connection string to read the xml file. Simply use the DataSource attribute as below. Also "PersonTestData.xml" properties. CopyToOutputDirectory set to "CopyAlways".
[TestClass]
public class UnitTest1
{
private TestContext testContextInstance;
public TestContext TestContext
{
get { return testContextInstance; }
set { testContextInstance = value; }
}
[TestMethod]
[DeploymentItem("PersonTestData.xml")]
[DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML",
"|DataDirectory|\\PersonTestData.xml",
"Person",
DataAccessMethod.Sequential)]
public void CompareToTest()
{
var row = TestContext.DataRow;
var firstName = row["FirstName"].ToString();
var lastName = row["LastName"].ToString();
//Asserts...
}
}

Common.Logging with PostSharp and NLog 2.0

I use Common.Logging as a wrapper around NLog 2.0. I've done this so that I can replace NLog with another logging provider in the future.
I also use PostSharp to not write a try catch block everytime I need one. I have a class that inherits the OnMethodBoundaryAspect:
[Serializable]
public class LogMethodAttribute : OnMethodBoundaryAspect
{
private ILog logger;
public LogMethodAttribute()
{
this.logger = LogManager.GetCurrentClassLogger();
}
public override void OnEntry(MethodExecutionArgs args)
{
logger.Debug(string.Format("Entering {0}.{1}.", args.Method.DeclaringType.Name, args.Method.Name));
}
public override void OnExit(MethodExecutionArgs args)
{
logger.Debug(string.Format("Leaving {0}.{1}.", args.Method.DeclaringType.Name, args.Method.Name));
}
public override void OnException(MethodExecutionArgs args)
{
logger.Error(args.Exception.Message,args.Exception);
}
}
I have configured Common.Logging as follows in my web.config:
<configSections>
<sectionGroup name="common">
<section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" />
</sectionGroup>
</configSections>
<common>
<logging>
<factoryAdapter type="Common.Logging.NLog.NLogLoggerFactoryAdapter, Common.Logging.NLog20">
<arg key="configType" value="FILE" />
<arg key="configFile" value="~/NLog.config" />
</factoryAdapter>
</logging>
</common>
NLog.Config looks like this:
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
throwExceptions="true"
internalLogLevel="Debug"
internalLogToConsoleError="true"
internalLogFile="c:\new projects/nlog-app.txt"
>
<!--
See http://nlog-project.org/wiki/Configuration_file
for information on customizing logging rules and outputs.
-->
<targets>
<target name="database"
xsi:type="Database"
commandText="INSERT INTO LogEvent(EventDateTime, EventLevel, UserName, MachineName, EventMessage, ErrorSource, ErrorClass, ErrorMethod, ErrorMessage, InnerErrorMessage) VALUES(#EventDateTime, #EventLevel, #UserName, #MachineName, #EventMessage, #ErrorSource, #ErrorClass, #ErrorMethod, #ErrorMessage, #InnerErrorMessage)"
dbProvider="System.Data.SqlClient">
<connectionString>
Data Source=...;Initial Catalog=myDB;User Id=user;Password=pass;
</connectionString>
<installConnectionString>
Data Source=...;Initial Catalog=myDB;User Id=user;Password=pass;
</installConnectionString>
<!-- parameters for the command -->
<parameter name="#EventDateTime" layout="${date:s}" />
<parameter name="#EventLevel" layout="${level}" />
<parameter name="#UserName" layout="${identity}" />
<parameter name="#MachineName" layout="${machinename}" />
<parameter name="#EventMessage" layout="${message}" />
<parameter name="#ErrorSource" layout="${event-context:item=error-source}" />
<parameter name="#ErrorClass" layout="${event-context:item=error-class}" />
<parameter name="#ErrorMethod" layout="${event-context:item=error-method}" />
<parameter name="#ErrorMessage" layout="${event-context:item=error-message}" />
<parameter name="#InnerErrorMessage" layout="${event-context:item=inner-error-message}" />
<!-- commands to install database -->
<install-command>
<text>CREATE DATABASE myDB</text>
<connectionString> Data Source=...;Initial Catalog=myDB;User Id=user;Password=pass;</connectionString>
<ignoreFailures>true</ignoreFailures>
</install-command>
<install-command>
<text>
CREATE TABLE LogEvent(
EventId int primary key not null identity(1,1),
EventDateTime datetime,
EventLevel nvarchar(50),
UserName nvarchar(50),
MachineName nvarchar(1024),
EventMessage nvarchar(MAX),
ErrorSource nvarchar(1024),
ErrorClass nvarchar(1024),
ErrorMethod nvarchar(1024),
ErrorMessage nvarchar(MAX),
InnerErrorMessage nvarchar(MAX));
</text>
</install-command>
<!-- commands to uninstall database -->
<uninstall-command>
<text>DROP DATABASE myDB</text>
<connectionString> Data Source=...;Initial Catalog=myDB;User Id=user;Password=pass;</connectionString>
<ignoreFailures>true</ignoreFailures>
</uninstall-command>
</target>
</targets>
<rules>
<logger name="*" levels="Error" writeTo="database" />
</rules>
</nlog>
The problem is that nothing is inserted in my table. When I put a logger in for example my HomeController on the index page and I call my logger.Error("an error") it adds a record to my table.
Can somebody help me?
Are you decorating your controller methods with the LogMethodAttribute that you created?
Also, you'll want to adjust your logger rule to include more levels outside of just "Error", otherwise that's all you'll log.
Give this a try:
<rules>
<logger name="*" minLevel="Trace" writeTo="database" />
</rules>
Edit:
Have you tried moving your logger initialization into your method?
public override void OnEntry(MethodExecutionArgs args)
{
this.logger = LogManager.GetCurrentClassLogger();
logger.Debug(string.Format("Entering {0}.{1}.", args.Method.DeclaringType.Name, args.Method.Name));
}
Per Donald Belcham's Pluralsight course, aspect constructors are not executed at runtime, so perhaps your logger is not getting set properly.
add a static property logger in your class Aspect
public class LogAspect : OnMethodBoundaryAspect
{
/// <summary>
/// Gets or sets the logger.
/// </summary>
public static ILogger logger { get; set; }
set logger variable in your application init method with your ILogger class and exclude all methods before this initialization with AttributeExclude.
[LogAspect(AttributeExclude = true)]
protected void Application_Start()
{
_windsorContainer = new WindsorContainer();
ApplicationDependencyInstaller.RegisterLoggingFacility(_windsorContainer);
LogAspect.logger = _windsorContainer.Resolve<ILogger>();

Categories