相信许多熟悉NHibernate的开发人员初识NHibernate时肯定读过这篇文章...
本问题基于此文开始处代码,代码十分简单,如下:
NHibernateTest代码
![](/Images/OutliningIndicators/ContractedBlock.gif)
SessionFixture类
using System;
using NUnit.Framework;
using NHibernate;
namespace NHibernateTest
{
///
/// SessionsFixture 的摘要说明。
///
///
[TestFixture]
public class SessionsFixture
{
public SessionsFixture()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
[Test] // 测试能否取得NHB会话工厂。
public void FactoryTest()
{
ISessionFactory sf = Sessions.Factory;
Assert.IsNotNull( sf, "get sessionfactory fail!" );
}
[Test] // 测试能否取得NHB会话。
public void GetSessionTest()
{
ISession s = Sessions.GetSession();
Assert.IsNotNull( s, "get session fail!" );
}
}
}
![](/Images/OutliningIndicators/ContractedBlock.gif)
Sessions类
using System;
using NHibernate;
using NHibernate.Cfg;
using System.Reflection;
namespace NHibernateTest
{
///
/// Sessions 的摘要说明。
///
public class Sessions
{
private static readonly object lockObj = new object();
private static ISessionFactory _factory;
public Sessions()
{
}
public static ISessionFactory Factory
{
get
{
if ( _factory == null )
{
lock ( lockObj )
{
if ( _factory == null )
{
NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration ();
cfg.AddAssembly(Assembly.GetExecutingAssembly(),true);
_factory = cfg.BuildSessionFactory();
}
}
}
return _factory;
}
}
public static ISession GetSession()
{
return Factory.OpenSession();
}
}
}
配置文件如下:
NHibernateTest.dll.config文件
![](/Images/OutliningIndicators/ContractedBlock.gif)
config配置
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="nhibernate" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
</configSections>
<nhibernate>
<add
key="hibernate.show_sql"
value="true"
/>
<add
key="hibernate.connection.provider"
value="NHibernate.Connection.DriverConnectionProvider"
/>
<add
key="hibernate.dialect"
value="NHibernate.Dialect.MsSql2000Dialect"
/>
<add
key="hibernate.connection.driver_class"
value="NHibernate.Driver.SqlClientDriver"
/>
<add
key="hibernate.connection.connection_string"
value="Server=127.0.0.1;initial catalog=Northwind;Integrated Security=SSPI"
/>
</nhibernate>
<!-- This section contains the log4net configuration settings -->
<log4net>
<!-- Define some output appenders -->
<appender name="rollingFile" type="log4net.Appender.RollingFileAppender,log4net" >
<param name="File" value="log.txt" />
<param name="AppendToFile" value="true" />
<param name="RollingStyle" value="Date" />
<param name="DatePattern" value="yyyy.MM.dd" />
<param name="StaticLogFileName" value="true" />
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d [%t] %-5p %c [%x] [%X{auth}] - %m%n" />
</layout>
</appender>
<!--Setup the root category, add the appenders and set the default priority -->
<root>
<priority value="ALL" />
<appender-ref ref="rollingFile" />
</root>
</log4net>
</configuration>
用VS.NET compile(当然添加对NHibernate.dll的引用)后,我运行的nunit-gui2.2.6,测试此程序集,“一道绿烟”顺利过关(我同时用了nunit-console同样成功)。
接下来,问题出现了。我为了之后的开发及compile的方便,编写了一个Nant(0.85.rc4版)的build文件,如下:
![](/Images/OutliningIndicators/ContractedBlock.gif)
Nant build文件
<?xml version="1.0" encoding="gb2312"?>
<project name="TestRemote" default="run" basedir=".">
<property name="solution.configuration" value="debug"/>
<property name="solution.filename" value="E:\Study\2006\NHibernate\start\start.sln"/>
<property name="build.outputdir" value="E:\Study\2006\NHibernate\start\build"/>
<target name="t1">
<vssget username="Admin" password="Admin" localpath="E:\Study\2006\DailyBuild" dbpath="E:\SourceManager\srcsafe.ini" path="$/Bank.root/Bank" />
</target>
<target name="clean">
<delete>
<fileset>
<exclude name="build/*.config"/>
<include name="build/**"/>
</fileset>
</delete>
</target>
<target name="t2">
<solution configuration="${solution.configuration}" outputdir="${build.outputdir}\" solutionfile="${solution.filename}" />
</target>
<target name="run">
<call target="clean"/>
<call target="t2"/>
<call target="nunit"/>
</target>
<target name="nunit">
<nunit2>
<formatter type="Plain"/>
<formatter type="Xml" usefile="true" extension=".xml" outputdir="${build.outputdir}\"/>
<test assemblyname="${build.outputdir}\NHibernateTest.dll" appconfig="${build.outputdir}\nunit-version.config"/>
</nunit2>
<nunit2report todir="${build.outputdir}" out="index.html">
<fileset>
<include name="${build.outputdir}\NHibernateTest.dll-results.xml"/>
</fileset>
</nunit2report>
</target>
</project>
(当然你需调整期中目录适合你的开发环境),并同时指定了一个config文件用于告诉NAnt的nunit2 task(上述文件中<test appconfig="...." ...即)
![](/Images/OutliningIndicators/ContractedBlock.gif)
nunit-version.config
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="nunit.framework" publicKeyToken="96d09a1eb7f44a77" culture="Neutral" />
<bindingRedirect oldVersion="2.2.0.0" newVersion="2.2.6.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
使用的nunit的版本(指定程序集)为2.2.6.0(GAC中有)
--另外说一下,我上一次用此种设置结合CruiseControl.NET实现的DailyBuild顺利完成,此表明配置文件的设定应该是没问题的---
运行Nant,问题出现了:
![NAnt中nunit2 Task时的错误](/images/cnblogs_com/sharplife/error.JPG)
即使你拷贝用上述已测试通过的程序集,仅运行nant的unittest target,同样出现上述问题,而实际上该程序集是没问题的(各位发现问题一定告诉我),请各位也是一下看是不是会出现问题,也请解决此问题的开发者在此留言指导,感激不尽!
是程序集的问题?NAnt的问题?NHibernate的(不敢不敢...) ?我的问题还请大家帮助......
本问题基于此文开始处代码,代码十分简单,如下:
NHibernateTest代码
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
using System;
using NUnit.Framework;
using NHibernate;
namespace NHibernateTest
{
///
/// SessionsFixture 的摘要说明。
///
///
[TestFixture]
public class SessionsFixture
{
public SessionsFixture()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
[Test] // 测试能否取得NHB会话工厂。
public void FactoryTest()
{
ISessionFactory sf = Sessions.Factory;
Assert.IsNotNull( sf, "get sessionfactory fail!" );
}
[Test] // 测试能否取得NHB会话。
public void GetSessionTest()
{
ISession s = Sessions.GetSession();
Assert.IsNotNull( s, "get session fail!" );
}
}
}
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
using System;
using NHibernate;
using NHibernate.Cfg;
using System.Reflection;
namespace NHibernateTest
{
///
/// Sessions 的摘要说明。
///
public class Sessions
{
private static readonly object lockObj = new object();
private static ISessionFactory _factory;
public Sessions()
{
}
public static ISessionFactory Factory
{
get
{
if ( _factory == null )
{
lock ( lockObj )
{
if ( _factory == null )
{
NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration ();
cfg.AddAssembly(Assembly.GetExecutingAssembly(),true);
_factory = cfg.BuildSessionFactory();
}
}
}
return _factory;
}
}
public static ISession GetSession()
{
return Factory.OpenSession();
}
}
}
配置文件如下:
NHibernateTest.dll.config文件
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="nhibernate" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
</configSections>
<nhibernate>
<add
key="hibernate.show_sql"
value="true"
/>
<add
key="hibernate.connection.provider"
value="NHibernate.Connection.DriverConnectionProvider"
/>
<add
key="hibernate.dialect"
value="NHibernate.Dialect.MsSql2000Dialect"
/>
<add
key="hibernate.connection.driver_class"
value="NHibernate.Driver.SqlClientDriver"
/>
<add
key="hibernate.connection.connection_string"
value="Server=127.0.0.1;initial catalog=Northwind;Integrated Security=SSPI"
/>
</nhibernate>
<!-- This section contains the log4net configuration settings -->
<log4net>
<!-- Define some output appenders -->
<appender name="rollingFile" type="log4net.Appender.RollingFileAppender,log4net" >
<param name="File" value="log.txt" />
<param name="AppendToFile" value="true" />
<param name="RollingStyle" value="Date" />
<param name="DatePattern" value="yyyy.MM.dd" />
<param name="StaticLogFileName" value="true" />
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d [%t] %-5p %c [%x] [%X{auth}] - %m%n" />
</layout>
</appender>
<!--Setup the root category, add the appenders and set the default priority -->
<root>
<priority value="ALL" />
<appender-ref ref="rollingFile" />
</root>
</log4net>
</configuration>
用VS.NET compile(当然添加对NHibernate.dll的引用)后,我运行的nunit-gui2.2.6,测试此程序集,“一道绿烟”顺利过关(我同时用了nunit-console同样成功)。
接下来,问题出现了。我为了之后的开发及compile的方便,编写了一个Nant(0.85.rc4版)的build文件,如下:
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
<?xml version="1.0" encoding="gb2312"?>
<project name="TestRemote" default="run" basedir=".">
<property name="solution.configuration" value="debug"/>
<property name="solution.filename" value="E:\Study\2006\NHibernate\start\start.sln"/>
<property name="build.outputdir" value="E:\Study\2006\NHibernate\start\build"/>
<target name="t1">
<vssget username="Admin" password="Admin" localpath="E:\Study\2006\DailyBuild" dbpath="E:\SourceManager\srcsafe.ini" path="$/Bank.root/Bank" />
</target>
<target name="clean">
<delete>
<fileset>
<exclude name="build/*.config"/>
<include name="build/**"/>
</fileset>
</delete>
</target>
<target name="t2">
<solution configuration="${solution.configuration}" outputdir="${build.outputdir}\" solutionfile="${solution.filename}" />
</target>
<target name="run">
<call target="clean"/>
<call target="t2"/>
<call target="nunit"/>
</target>
<target name="nunit">
<nunit2>
<formatter type="Plain"/>
<formatter type="Xml" usefile="true" extension=".xml" outputdir="${build.outputdir}\"/>
<test assemblyname="${build.outputdir}\NHibernateTest.dll" appconfig="${build.outputdir}\nunit-version.config"/>
</nunit2>
<nunit2report todir="${build.outputdir}" out="index.html">
<fileset>
<include name="${build.outputdir}\NHibernateTest.dll-results.xml"/>
</fileset>
</nunit2report>
</target>
</project>
(当然你需调整期中目录适合你的开发环境),并同时指定了一个config文件用于告诉NAnt的nunit2 task(上述文件中<test appconfig="...." ...即)
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="nunit.framework" publicKeyToken="96d09a1eb7f44a77" culture="Neutral" />
<bindingRedirect oldVersion="2.2.0.0" newVersion="2.2.6.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
使用的nunit的版本(指定程序集)为2.2.6.0(GAC中有)
--另外说一下,我上一次用此种设置结合CruiseControl.NET实现的DailyBuild顺利完成,此表明配置文件的设定应该是没问题的---
运行Nant,问题出现了:
即使你拷贝用上述已测试通过的程序集,仅运行nant的unittest target,同样出现上述问题,而实际上该程序集是没问题的(各位发现问题一定告诉我),请各位也是一下看是不是会出现问题,也请解决此问题的开发者在此留言指导,感激不尽!
是程序集的问题?NAnt的问题?NHibernate的(不敢不敢...) ?我的问题还请大家帮助......