Castle+IBatis在WinForm以及ASP.NET中的测试笔记

      前两天面视涉及到Ibatis框架的一些问题,所以这些天来集中精力了解了一下Castle+IBatis,(测试采用的版本:IBatis.DataAccess.1.6.1.0.bin IBatis.DataMapper.1.1.458.0.bin),分别在ASP.Net和WinForm中调试了用Mygeneraction模板生成的程序,以下是在园子里的朋友的一些错误笔记记录汇总以及我的测试用例,仅以此作为我学习IBatis_Castle的一点总结。

 

问题1:

  (1)在ASP.Net中:Error 4 The type or namespace name 'XXX' could not be found (are you missing a using directive or an assembly reference?)

  (2)在WinForm中:Error 4 Cannot embed interop types from assembly 'XXX.dll' because it is missing either the 'ImportedFromTypeLibAttribute' attribute or the 'PrimaryInteropAssemblyAttribute' attribute

  (3)Server Error in '/Web' Application. Ambiguous match found.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Reflection.AmbiguousMatchException: Ambiguous match found.

解决方案:

   将项目的Target Framework设置为3.5或3.5以下即可,至于4.0版本为什么会有问题,查了诸多资料依然不能解决,希望园子里的朋友指点一二。

问题2、

未能加载文件或程序集 或它的某一个依赖项。系统找不到指定的文件。
[ConfigurationException:
- The error occurred while loading SqlMap.
- initialize type alias
- The error occurred in <sqlMap embedded="XXX_SpaceName.Model.Maps.XXX_ClassName.xml, XXX_SpaceName.Model" xmlns="http://ibatis.apache.org/dataMapper" />. 
- Check the DG_URCM.Model.Fxz, DG_URCM.Model.]

解决方案:

 检查配置文件:Maps/XXX.xml

 例如:

 <?xml version="1.0" encoding="UTF-8" ?>
 <sqlMap namespace="Artical" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <alias>
  <typeAlias alias="Artical"  type="Business.Model.Artical,Business.Model" />
  <typeAlias alias="Artical.Key"  type="Business.Model.Artical+Key,Business.Model" />
 </alias>

问题3:SqlMap配置错误)

(1)The error occurred while loading SqlMap.
- The error occurred in <sqlMap resource="App_Core/Maps/Admin.xml" xmlns="
http://ibatis.apache.org/dataMapper" />.

(2)捕捉捉到 IBatisNet.Common.Exceptions.ConfigurationException
Message="\n- The error occurred while configure DaoSessionHandler.\n- The error occurred in...

解决方案:

查看下映射文件CXC.xml

<?xml version="1.0" encoding="utf-8" ?>
<sqlMap namespace="CXC"
xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="SqlMap.xsd">

把它改为:

<?xml version="1.0" encoding="UTF-8" ?>
<sqlMap namespace="CXC" xmlns="http://ibatis.apache.org/mapping xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance">

问题4:rror while configuring the Provider named "sqlServer2.0". Cause : The provider is not in 'providers.config' or is not enabled.
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

堆栈跟踪:


[ConfigurationException: Error while configuring the Provider named "sqlServer2.0". Cause : The provider is not in 'providers.config' or is not enabled.]
   IBatisNet.DataMapper.Configuration.DomSqlMapBuilder.ParseProvider() +412
   IBatisNet.DataMapper.Configuration.DomSqlMapBuilder.Initialize() +2435
   IBatisNet.DataMapper.Configuration.DomSqlMapBuilder.Build(XmlDocument document, DataSource dataSource, Boolean useConfigFileWatcher, Boolean isCallFromDao) +253

[ConfigurationException:
- The error occurred while load DataBase Provider.
- The error occurred in <provider name="sqlServer2.0" xmlns="http://ibatis.apache.org/dataMapper" />. 
- Check the sqlServer2.0.]

解决方案:

将providers.config中对应的的sqlServer2.0的enabled改为true就OK了,默认的是false。

问题5:

“/Web”应用程序中的服务器错误。
--------------------------------------------------------------------------------

未将对象引用设置到对象的实例。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。

堆栈跟踪:


[NullReferenceException: 未将对象引用设置到对象的实例。]
   IBatisNet.DataMapper.Configuration.DomSqlMapBuilder.ParseGlobalProperties() +68
   IBatisNet.DataMapper.Configuration.DomSqlMapBuilder.Initialize() +222
   IBatisNet.DataMapper.Configuration.DomSqlMapBuilder.Build(XmlDocument document, DataSource dataSource, Boolean useConfigFileWatcher, Boolean isCallFromDao) +253

[ConfigurationException:
- The error occurred while Validate SqlMap config.]

解决方案:

将SqlMap中配置文件替换为:

<?xml version="1.0" encoding="utf-8"?>
<sqlMapConfig xmlns="http://ibatis.apache.org/dataMapper" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">即可。

问题6:

“/Web”应用程序中的服务器错误。
--------------------------------------------------------------------------------

未能加载文件或程序集“Castle.MicroKernel, Version=1.0.0.2, Culture=neutral, PublicKeyToken=407dd0808d44fbdc”或它的某一个依赖项。找到的程序集清单定义与程序集引用不匹配。 (异常来自 HRESULT:0x80131040)
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.IO.FileLoadException: 未能加载文件或程序集“Castle.MicroKernel, Version=1.0.0.2, Culture=neutral, PublicKeyToken=407dd0808d44fbdc”或它的某一个依赖项。找到的程序集清单定义与程序集引用不匹配。 (异常来自 HRESULT:0x80131040)

程序集加载跟踪: 下列信息有助于确定程序集“Castle.MicroKernel, Version=1.0.0.2, Culture=neutral, PublicKeyToken=407dd0808d44fbdc”无法加载的原因。
警告: 程序集绑定日志记录被关闭。
要启用程序集绑定失败日志记录,请将注册表值 [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD)设置为 1。
注意: 会有一些与程序集绑定失败日志记录关联的性能损失。
要关闭此功能,请移除注册表值 [HKLM\Software\Microsoft\Fusion!EnableLog]。

堆栈跟踪:
[FileLoadException: 未能加载文件或程序集“Castle.MicroKernel, Version=1.0.0.2, Culture=neutral, PublicKeyToken=407dd0808d44fbdc”或它的某一个依赖项。找到的程序集清单定义与程序集引用不匹配。 (异常来自 HRESULT:0x80131040)]

解决方案:

一般是DLL文件引用版本错误,注意要对Castle.Facilities.IBatisNetIntegration.dll进行重新编译,在你Castle安装目录下Facilities中对IBatisNet进行编译,然后引用进来。

问题7:版本升级过程中需要修改的地方:

1.3.0.0,Castle IOC版本升级到1.0.0.2。升级过程中发现几个问题:
1、IBatisNet的配置文件的格式采用了标准的XML的命名空间:
   原来的providers.config 文件
      <?xml version="1.0" encoding="utf-8" ?>

            <providers>
               <clear/>
                 <provider
                     ……
             </providers>
   更改为
   <?xml version="1.0" encoding="utf-8"?>
          <providers xmlns="http://ibatis.apache.org/providers" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
            <clear/>
             <provider
                     ……
        </providers>
  原来的SqlMap.config文件
 <?xml version="1.0" encoding="utf-8"?>
<sqlMapConfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:noNamespaceSchemaLocation="SqlMapConfig.xsd">
……
</sqlMapConfig>
更改为
<?xml version="1.0" encoding="utf-8"?>
<sqlMapConfig xmlns="http://ibatis.apache.org/dataMapper" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
原来的PO.xml文件
<?xml version="1.0" encoding="UTF-8" ?>
<sqlMap namespace="Sequence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:noNamespaceSchemaLocation="SqlMap.xsd">
……
</sqlMap>
更改为
<?xml version="1.0" encoding="UTF-8" ?>
<sqlMap namespace="Admin" xmlns="http://ibatis.apache.org/mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
……
</sqlMap>

问题8在asp.net中各个配置文件的目录和web.config同目录。在WinForm中各配置文件与.dll的目录一直,即在Bin/Debug/Release目录下.

问题9:

(1)IBatic配置错误:

“/WebView”应用程序中的服务器错误。
--------------------------------------------------------------------------------

Invalid SqlMap.config document. cause :元素 “sqlMapConfig” 不能包含文本。应为可能元素的列表: 'settings properties database alias typeHandlers sqlMaps'。
元素 “sqlMapConfig” 不能包含文本。应为可能元素的列表: 'database alias typeHandlers sqlMaps'。
元素 “sqlMapConfig” 的子元素 “settings” 无效。应为可能元素的列表: 'database alias typeHandlers sqlMaps'。

说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: IBatisNet.Common.Exceptions.ConfigurationException: Invalid SqlMap.config document. cause :元素 “sqlMapConfig” 不能包含文本。应为可能元素的列表: 'settings properties database alias typeHandlers sqlMaps'。
元素 “sqlMapConfig” 不能包含文本。应为可能元素的列表: 'database alias typeHandlers sqlMaps'。
元素 “sqlMapConfig” 的子元素 “settings” 无效。应为可能元素的列表: 'database alias typeHandlers sqlMaps'。

解决方案:

将sqlmap.config的前几行替换为:

<?xml version="1.0" encoding="utf-8"?>
<sqlMapConfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="SqlMapConfig.xsd">
  <settings>
    <setting useStatementNamespaces="false"/>
    <setting cacheModelsEnabled="true"/>
  </settings>

就可以了。

(2)IBatic配置错误:


“/WebView”应用程序中的服务器错误。
--------------------------------------------------------------------------------

Error while configuring the Provider named "provider". Cause : The provider is not in 'providers.config'.
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: IBatisNet.Common.Exceptions.ConfigurationException: Error while configuring the Provider named "provider". Cause : The provider is not in 'providers.config'.

解决方案:

在sqlmap.config中添加:

<properties resource="properties.config"/>
完整的sqlmap.config配置:

<?xml version="1.0" encoding="utf-8"?>
<sqlMapConfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="SqlMapConfig.xsd">
  <settings>
    <setting useStatementNamespaces="false"/>
    <setting cacheModelsEnabled="true"/>
  </settings>

  <properties resource="properties.config"/>
 
 <database> 
  <provider name="${provider}"/>
  <dataSource name="iBatisTutorial"  connectionString="${connectionString}"/>
 </database>

 <sqlMaps>
  <sqlMap resource="Resources/PersonHelper.xml"/>
 </sqlMaps>

</sqlMapConfig>

问题10:

(1)未能加载文件或程序集 或它的某一个依赖项。系统找不到指定的文件。
[ConfigurationException:
- The error occurred while loading SqlMap.
- initialize type alias
- The error occurred in <sqlMap embedded="CdtDistribution.Model.Maps.Fxz.xml, CdtDistribution.Model" xmlns="http://ibatis.apache.org/dataMapper" />. 
- Check the DG_URCM.Model.Fxz, DG_URCM.Model.]

问题的解决办法 
     拷过来的XML文件的命名空间没有改过来

(2)Can't create component 'wbzService' as it has dependencies to be satisfied.
wbzService is waiting for the following dependencies:

Services:
- CdtDistribution.Dao.WBZDao which was registered but is also waiting for dependencies.

wbzDao is waiting for the following dependencies:

Services:
- IBatisNet.DataMapper.SqlMapper which was not registered.

 解决的办法

    private ISqlMapper sqlmap;
        public WBZDao(SqlMapper sqlmap)
        {
            this.sqlmap = sqlmap;
           
        }

改成  private ISqlMapper sqlmap;
        public WBZDao( ISqlMapper sqlmap)
        {
            this.sqlmap = sqlmap;
           
        }

一个字母的问题

(3)
- The error occurred while loading SqlMap.
- apply inline parameterMap
- The error occurred in <sqlMap embedded="CdtDistribution.Model.Maps.Wbz.xml,CdtDistribution.Model" xmlns="http://ibatis.apache.org/dataMapper" />. 
- Check the FindCount.
同上  注意大小写问题
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/swort_177/archive/2009/03/27/4028643.aspx

问题11:

(1) 在WinForm中测试,需要实例化BLLContainer类,即asp.net中Session的一个变通方法:新建一个类库,将所要session的类实例化,用单例模式,然后引用到应用文件中即可(这个问题网上有好多朋友在问,没有WinForm下的测试用例,这样做就可以的,因为上次没来得及把代码发上来,如有需要,请告知)

类库:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Castle.Windsor;///容器
using Business;///业务类

namespace InitContainer
{
    public class InitContainer
    {
        private static readonly object RsnObj = new object();

        private static BLLContainer _BLLContainer = null;

        public static BLLContainer GetInstance()
        {
            if (_BLLContainer == null)
            {
                lock (RsnObj)
                {
                    if (_BLLContainer == null)
                    {
                        _BLLContainer = new BLLContainer();
                    }
                }
            }

            return _BLLContainer;
        }
    }
}

应用程序中调用:

BLLContainer BllContainer = InitContainer.InitContainer.GetInstance();

this.container1 = BllContainer as IWindsorContainer;

ArticalBLL aricalbll = this.container1["ArticalBLL"] as ArticalBLL;

this.dataGridView1.DataSource = aricalbll.Select();

实例效果图:

 

(2)在Asp.net中:

在asp.net的app_code下建一个基类:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Castle.Windsor;

/// <summary>
/// BasePage 的摘要说明
/// </summary>
public class BasePage : Page, IContainerAccessor
{
    private IWindsorContainer container = null;

    /// <summary>
    /// method means set a session("Container")/////
    /// </summary>
    /// <param name="e"></param>
    protected override void OnInit(EventArgs e)
    {
        if (HttpContext.Current.Items["Container"] == null)
        {
            HttpContext.Current.Items["Container"] = new Business.BLLContainer();
        }

        this.container = HttpContext.Current.Items["Container"] as IWindsorContainer;
    }

    #region IContainerAccessor 成员

    /// <summary>
    /// 获取容器实例
    /// </summary>
    public IWindsorContainer Container
    {
        get
        {
            return container;
        }
    }
    #endregion
}

在页面中调用:

public partial class _Default : BasePage    //BasePage封装Ico容器
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!Page.IsPostBack)
            BindArtical();

    }

    protected void BindArtical()
    {
        ArticalBLL thisArticalBLL = this.Container["ArticalBLL"] as ArticalBLL;

        this.GridView1.DataSource = thisArticalBLL.Select();
        this.GridView1.DataBind();
    }

}

实例效果图:

 

  问题12:这里使用IBatis的最新版本,IBatis.DataAccess.1.9.2.bin IBatis.DataMapper.1.6.2.bin,数据表主键类型采用Guid,对以上实例进行了升级,在调试的过程中会遇到以下问题,呵呵,折腾了两天,愣是没找到问题的原因所在。

  IBatisException Error this.ExecuteQueryForObject("Artical_Select", PK) Cause:There is no Get member named 'Id' in class 'Guid'
  解决方案:

   <select id="Artical_Select" resultMap="Artical_AllColumns" parameterClass="Guid" resultClass="Artical">
        SELECT
         [Id],
         [Title],
         [Content]
          FROM Artical
     <dynamic prepend="WHERE">
       <isParameterPresent>
         Id = #Id#
       </isParameterPresent>
       <!--<isNotParameterPresent>
         Id is null
       </isNotParameterPresent>-->
     </dynamic>

   </select>

   注意:返回类型:resultClass="Artical"不要缺少,否则就会出现错误。

posted @ 2010-09-28 21:01  AriLee  Views(3828)  Comments(9Edit  收藏  举报