System.InvalidOperationException:无法构造类型数据库
我一直在考虑将一些旧代码重构为基于 net 4.0 的新 WCF 服务,但在本应简单的练习中遇到了一些困难!
场景;
WCF 服务通过 HTTP 托管,实现我们的 ServiceContract,它连接到本地 Sql Server。
当尝试针对服务调用运行简单的 NUnit 测试时,我收到以下错误;
* HelpManager.Tests.GetPage.GetPageById 错误 例外: System.ServiceModel.FaultException`1[System.ServiceModel.ExceptionDetail]: 尝试获取类型实例时发生激活错误 数据库,键“HelpManagement”(故障详细信息等于 An ExceptionDetail,可能由以下人员创建 IncludeExceptionDetailInFaults=true,其值为: Microsoft.Practices.ServiceLocation.ActivationException:激活 尝试获取数据库键类型的实例时发生错误 「帮助管理」----> Microsoft.Practices.Unity.ResolutionFailedException:解决 依赖失败,类型= “Microsoft.Practices.EnterpriseLibrary.Data.Database”,名称= “帮助管理”。 while:解决时发生异常。例外 是: InvalidOperationException - 数据库类型不能是 建造的。您必须配置容器以提供该值。 - - - - - - - - - - - - - - - - - - - - - - - - 当时的 例外,容器是:
正在解决 Microsoft.Practices.EnterpriseLibrary.Data.Database、帮助管理 ----> System.InvalidOperationException:数据库类型不能是 建造的。您必须配置容器以提供此值。
在 Microsoft.Practices.ObjectBuilder2.DynamicMethodConstructorStrategy.GuardTypeIsNonPrimiti...)。
我们的(非常标准的)WCF web.config 看起来像;
<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="true"/>
</configSections>
<dataConfiguration defaultDatabase="HelpManagement"/>
<connectionStrings>
<add name="HelpManagement" connectionString="server=(local);database=ieq;uid=;pwd=" providerName="System.Data.SqlClient"/>
</connectionStrings>
以及用于调用它的代码示例;
private const string DB_HelpManagement = "HelpManagement";
var db = DatabaseFactory.CreateDatabase(DB_HelpManagement);
谷歌等人的经历并不有趣。我检查了版本等,它们似乎都引用了来自 GAC 的相同 5.0.14,因此不确定问题是什么。
提前致谢
I have been looking into refactoring some old code into a new WCF service, based on net 4.0 and have into a little difficulty with what should be a simple exercise!
The scenario;
WCF Service hosted over HTTP, implementing our ServiceContract, which connects to a local Sql Server.
When attempting to run a simple NUnit test against the Service Call, I get the following error;
* HelpManager.Tests.GetPage.GetPageById Fault
Exception:
System.ServiceModel.FaultException`1[System.ServiceModel.ExceptionDetail]:
Activation error occured while trying to get instance of type
Database, key "HelpManagement" (Fault Detail is equal to An
ExceptionDetail, likely created by
IncludeExceptionDetailInFaults=true, whose value is:
Microsoft.Practices.ServiceLocation.ActivationException: Activation
error occured while trying to get instance of type Database, key
"HelpManagement" ---->
Microsoft.Practices.Unity.ResolutionFailedException: Resolution of the
dependency failed, type =
"Microsoft.Practices.EnterpriseLibrary.Data.Database", name =
"HelpManagement". Exception occurred while: while resolving. Exception
is: InvalidOperationException - The type Database cannot be
constructed. You must configure the container to supply this value.
----------------------------------------------- At the time of the
exception, the container was:Resolving
Microsoft.Practices.EnterpriseLibrary.Data.Database,HelpManagement
----> System.InvalidOperationException: The type Database cannot be
constructed. You must configure the container to supply this value.
at
Microsoft.Practices.ObjectBuilder2.DynamicMethodConstructorStrategy.GuardTypeIsNonPrimiti...).
Our (pretty standard) WCF web.config for this, looks like;
<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="true"/>
</configSections>
<dataConfiguration defaultDatabase="HelpManagement"/>
<connectionStrings>
<add name="HelpManagement" connectionString="server=(local);database=ieq;uid=;pwd=" providerName="System.Data.SqlClient"/>
</connectionStrings>
and an example of the code used to call it;
private const string DB_HelpManagement = "HelpManagement";
var db = DatabaseFactory.CreateDatabase(DB_HelpManagement);
Google et al have been no fun. I have checked versions etc and they all appear to be referencing the same 5.0.14 from GAC, so unsure as to what the problem is.
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想知道 Microsoft.Practices.ObjectBuilder.dll 和 Microsoft.Practices.EnterpriseLibrary.Common - 它们是否在 WCF 项目中引用?
I wonder about Microsoft.Practices.ObjectBuilder.dll and Microsoft.Practices.EnterpriseLibrary.Common -- are they referenced in the WCF project?
听起来这可能是你的配置。您是否在配置文件中指定了 dataConfiguration defaultDatabase 值(例如 web.config)?
It sounds like it might be your configuration. Did you specify the dataConfiguration defaultDatabase value in your config file (e.g. web.config)?