配置Enterprise Library 5.0数据访问应用程序块

时间:2011-04-10 00:18:59

标签: c# winforms unit-testing enterprise-library data-access-app-block

我正在试图弄清楚如何配置企业库5.0数据访问应用程序块。 运行我的unittest时,出现以下错误:

Microsoft.Practices.ServiceLocation.ActivationException was caught
  Message=Activation error occured while trying to get instance of type Database, key "PokerAdviserProvider"
  InnerException: Microsoft.Practices.Unity.ResolutionFailedException
       Message=Resolution of the dependency failed, type = "Microsoft.Practices.EnterpriseLibrary.Data.Database", name = "PokerAdviserProvider".
Exception occurred while: while resolving.
Exception is: InvalidOperationException - The type Database cannot be constructed. You must configure the container to supply this value.

我得到的代码行:

var db = DatabaseFactory.CreateDatabase("PokerAdviserProvider");

App.config中:

<configuration>
    <configSections>
        <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" />
    </configSections>
    <dataConfiguration defaultDatabase="PokerAdviserProvider" />
    <connectionStrings>
        <add name="PokerAdviserProvider" connectionString="server=(localhost);Initial Catalog=PokerAdviser;uid=abc;pwd=xyz"
            providerName="System.Data.SqlClient" />
    </connectionStrings>
</configuration>

我一直在谷歌上搜索一下,并找到了一些答案,这些设置也应放在我的unittest项目的app.Config中,但这没有什么区别。

我有点卡在这里,所以任何帮助都非常感谢。

修改

我引用了正确的dll(来自Program Files,而不是来自源代码),所以这不是问题。

3 个答案:

答案 0 :(得分:2)

我终于解决了这个问题:

Error: Activation error occured while trying to get instance of type Database, key "<database name>"

Inner Exception:  Resolution of the dependency failed, type = Microsoft.Practices.EnterpriseLibrary.Data.Database

我在Windows 7,Enlib 5.0上运行VS 2010。以下对我有用。想要传播这个词

  1. 确保您对Microsoft.Practices.Unity.dll

  2. 有适当的引用
  3. 获取VS 2010的最新服务包

答案 1 :(得分:0)

终于明白了。我在我的webservice的类库中使用DAAB,并认为我必须在该库中创建一个app.config。应该知道这不起作用。这样做时,我的思绪可能很遥远......

我在webservice的web.config中进行了配置,现在一切顺利运行。

答案 2 :(得分:0)

请参阅这两篇好文章post1&amp;关于企业库配置的post2