1

我知道这个问题之前已经在这里发布过,并且我已经找到了尽可能多的答案,但我仍然无法获得世界上最简单的测试。

1)我创建了我的测试并确保它在 VS2008 中工作,然后在 VS2010 中打开了解决方案(所以它都是定义工作的,所有 3.5 代码以及所有程序集引用 2.0/3.0/3.5 引用)

2)我添加了以下配置

  <runtime>
    <loadFromRemoteSources enabled="true"/>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
       <qualifyAssembly partialName="System.Data.SQLite" fullName="System.Data.SQLite, Version=1.0.60.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139" />
    </assemblyBinding>
  </runtime>
  <startup useLegacyV2RuntimeActivationPolicy="true">
      <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>

3) 我尝试针对 1.0.60.0 x86 和 1.0.66.0 x64 SqlLite dll 进行测试

4) 我尝试在 x86 和 x64 模式下运行测试

还是没有通过。我错过了什么?

(哦,我正在使用 SQLite20Driver,并且 Copy Local 设置为 true)

测试是一个简单的 Configure()

var cfg = Fluently.Configure(new NHibernate.Cfg.Configuration().Configure())
                       .Mappings(m => m.FluentMappings.AddFromAssemblyOf<UserMap>()
                           .Conventions.AddFromAssemblyOf<RequiredPropertyConvention>())
                       .BuildConfiguration();

var sessionFactory = cfg.BuildSessionFactory();

错误

NHibernate.HibernateException:“找不到程序集 System.Data.SQLite 中的 IDbCommand 和 IDbConnection 实现。确保程序集 System.Data.SQLite 位于应用程序目录或全局程序集缓存中。如果程序集在GAC,使用应用程序配置文件中的元素来指定程序集的全名。”

堆栈跟踪

   at NHibernate.Driver.ReflectionBasedDriver..ctor(String driverAssemblyName, String connectionTypeName, String commandTypeName)
   at NHibernate.Driver.SQLite20Driver..ctor()
   at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
   at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache)
   at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean skipCheckThis, Boolean fillCache)
   at System.Activator.CreateInstance(Type type, Boolean nonPublic)
   at System.Activator.CreateInstance(Type type)
   at NHibernate.Bytecode.ActivatorObjectsFactory.CreateInstance(Type type)
   at NHibernate.Connection.ConnectionProvider.ConfigureDriver(IDictionary`2 settings)
   at NHibernate.Connection.ConnectionProvider.ConfigureDriver(IDictionary`2 settings)
   at NHibernate.Connection.ConnectionProvider.Configure(IDictionary`2 settings)
   at NHibernate.Connection.ConnectionProviderFactory.NewConnectionProvider(IDictionary`2 settings)
   at NHibernate.Cfg.SettingsFactory.BuildSettings(IDictionary`2 properties)
   at NHibernate.Cfg.Configuration.BuildSettings()
   at NHibernate.Cfg.Configuration.BuildSessionFactory()

NHibernate 配置

  <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.SQLite20Driver</property>
      <property name="connection.connection_string">Data Source=nhibernate.db;Version=3</property>
      <property name="dialect">NHibernate.Dialect.SQLiteDialect</property>
      <property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
      <property name="query.substitutions">true=1, false=0</property>
    </session-factory>
  </hibernate-configuration>
4

1 回答 1

1

如果您要将 VS2008 项目迁移到 2010,或者甚至开始一个新项目,那么您的项目可能会引用尚未针对 .NET 4 框架编译的第 3 方程序集(目前)。要让 V2.0 程序集在 .Net 4 运行时中运行,您需要启用混合模式,方法是将以下内容添加到您的 web.config/app.config:

<configuration>
  <startup  useLegacyV2RuntimeActivationPolicy="true">
       <supportedRuntime version="v4.0"/>
  </startup>
</configuration>

useLegacyV2RuntimeActivationPolicy:这告诉运行时将针对旧版本 .NET 构建的程序集与 V4 运行时而不是它们构建时使用的运行时绑定(强制它们使用 V4)

supportedRuntime:让 .NEt 知道您的应用程序支持 V4 运行时并在 V4 运行时中加载您的应用程序。

对于 UnitTesting,将其添加到包含 TestFixtures 的项目的 app.config 是不够的。您需要将此添加到实际运行测试的应用程序的 app.config 中,因此对于 NUnit GUI,您需要将其添加到 nunit.exe.config。此外,您需要确保您使用的是 2.5.3+ 版本的 NUnit。

<configuration>
  <startup useLegacyV2RuntimeActivationPolicy="true">
    <requiredRuntime version="v4.0" />
    <supportedRuntime version="v4.0"/>
  </startup>
  <runtime>
    <loadFromRemoteSources enabled="true" />
  </runtime>
</configuration>

loadFromRemoteSources:CAS 已在 .Net 4 中弃用,此行强制部分信任 V2 程序集以完全信任的方式运行。

requiredRuntime:这是因为 NUnit 是针对 V2 运行时构建的,要在 V4 中加载,需要告知它必须在 V4 运行时中运行。(而不是只“支持”它)

如果您使用不同的测试运行程序,例如 ReSharper 的测试运行程序、TestDriven.Net 或 DXCore 的测试运行程序,您需要确保您已更改他们的 app.config 以匹配上述内容(或等待它们的 .Net 4 构建被发布)

最后,如果您使用专门针对 x86 编译的第 3 方程序集(这将主要是包装其他 C/C++ 程序集的程序集,如 System.Data.SQLite),您要么需要获取它们的 64 位版本(因为当使用任何 CPU 作为目标时,VS2010 默认编译为 x64)或将目标更改为 x86。

于 2010-09-19T16:14:56.480 回答