幾日前又將一個asp.net的舊項目搬遷至linux下,該項目是基於asp.net 4.0 + Nhibernate + FluentNhibernate實現的,在遷移的過程中碰到一個少見的問題,爬了很多文,包括中文,英文,最后在某日文網站(靠google翻譯)上找到答案,可謂相當蛋疼,特些記錄
先介紹下,要遷移至的環境是centos6.5 + MONO + Jexus
該情況發生在Nhibernate + mono下,但不是一定會發生,原先我移遷的幾個項目里,也有一些是采用Nhibernate的,就沒發生這樣的問題
初始:
遷移發現如下錯誤
於是,老老實實進web.config修改配置
<globalization culture="en-US" uiCulture="en-US"/>
發現修改后,還是這樣的問題,有些苦惱了,仔細追蹤,發現下面有提示是Nhibernate OpenSession時出錯,看來問題出在這上面
Unhandled Exception: System.ArgumentNullException: Argument cannot be null. Parameter name: type at System.Activator.CheckType (System.Type type) [0x00000] at System.Activator.CreateInstance (System.Type type, Boolean nonPublic) [0x00000] at NHibernate.AdoNet.SqlClientSqlCommandSet..ctor () [0x00000] at NHibernate.AdoNet.SqlClientBatchingBatcher..ctor (NHibernate.AdoNet.ConnectionManager connectionManager, IInterceptor interceptor) [0x00000]
這時候試了很多方法,包括重寫了一些nhibernate的調用方法,爬了很多文,一直沒有找到解決方案,直到最后從某個角落找到了一個解釋,拋出該錯誤的原因還有解釋出來,只是翻譯看得太累...
發生該錯誤的原因原來是:
NHibernate.AdoNet.SqlClientSqlCommandSet
[Test] public void Test() { Assembly sysData = Assembly.Load("System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"); var sqlCmdSetType = sysData.GetType("System.Data.SqlClient.SqlCommandSet"); Assert.IsTrue(sqlCmdSetType != null, "Could not find SqlCommandSet!"); }
用以上測試,找不到SqlCommandSet....
似乎是由於平台的原因造成
目前解決辦法
nhibernate:
配置
<property name="adonet.batch_size">0</property>
FluentNhibernate:
return Fluently.Configure() .Database( FluentNHibernate.Cfg.Db.MsSqlConfiguration.MsSql2008 .ConnectionString(s => s.Server("YourServer").Database("YourDB").Username("username").Password("userpwd")).AdoNetBatchSize(0)) .Mappings(m => m.FluentMappings .AddFromAssemblyOf<xxxxxx>() ).BuildSessionFactory();
即添加.AdoNetBatchSize(0)
至此問題解決,以上以作記錄,也希望能幫遇到同樣問題的人少走一些彎路!
參考URL:
https://github.com/codesharp/infrastructure/issues/15
http://sta-blockhead.blogspot.com/2009/06/systemdatasqlclientsqlcommandset.html