解決微軟企業類庫 EnterpriseLibrary 中 DatabaseFactory.CreateDatabase 失敗問題

今天用 netTiers 生成了一套代碼,用的時候發現如果使用  DatabaseFactory.CreateDatabase()  失敗,拋出以下異常
Activation error occured while trying to get instance of type Database, key ""   
在 Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance(Type serviceType, String key)
   在 Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance[TService](String key)
   在 Microsoft.Practices.EnterpriseLibrary.Data.DatabaseFactory.InnerCreateDatabase(String name)

原始 app.config
<?xml version="1.0"?>
<configuration>
	<configSections>
		<section name="enterpriseLibrary.ConfigurationSource" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ConfigurationSourceSection, Microsoft.Practices.EnterpriseLibrary.Common" />
	</configSections>

	<enterpriseLibrary.ConfigurationSource selectedSource="File Configuration Source">
		<sources>
			<add name="File Configuration Source" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common"
				filePath="entlib.config" />
		</sources>
	</enterpriseLibrary.ConfigurationSource>
	<connectionStrings>
		<add name="netTiersConnectionString" connectionString="Data Source=(local)\SQLEXPRESS;Initial Catalog=testDb;Persist Security Info=True;User ID=sa;Password=******" providerName="System.Data.SqlClient"/>
	</connectionStrings>
</configuration>


 entlib.config
<configuration>
	<configSections>
		<section name="securityConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Security.Configuration.SecuritySettings, Microsoft.Practices.EnterpriseLibrary.Security" />
		<section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging" />
		<section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling" />
		<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data" />
		<section name="cachingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Caching.Configuration.CacheManagerSettings, Microsoft.Practices.EnterpriseLibrary.Caching" />
	</configSections>
	<dataConfiguration defaultDatabase="netTiersConnectionString">
	</dataConfiguration>
	<securityConfiguration defaultAuthorizationInstance="NetTiers.RuleProvider"
	  defaultSecurityCacheInstance="">
		.....
	</securityConfiguration>
	<loggingConfiguration name="Logging Application Block" tracingEnabled="true"
	  defaultCategory="General" logWarningsWhenNoCategoriesMatch="true">
		....
	</loggingConfiguration>
	<exceptionHandling>
		....
	</exceptionHandling>
	<cachingConfiguration defaultCacheManager="YGame.Server.Entities.EntityCache">
		....
	</cachingConfiguration>
</configuration>

調整後,把 entlib.config 內容全部搬到 app.config ,問題就解決了。
<?xml version="1.0"?>
<configuration>
	<configSections>
		<section name="securityConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Security.Configuration.SecuritySettings, Microsoft.Practices.EnterpriseLibrary.Security" />
		<section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging" />
		<section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling" />
		<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data" />
		<section name="cachingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Caching.Configuration.CacheManagerSettings, Microsoft.Practices.EnterpriseLibrary.Caching" />
	</configSections>

	<dataConfiguration defaultDatabase="netTiersConnectionString">
	</dataConfiguration>
	<securityConfiguration defaultAuthorizationInstance="NetTiers.RuleProvider"
	  defaultSecurityCacheInstance="">
		.....
	</securityConfiguration>
	<loggingConfiguration name="Logging Application Block" tracingEnabled="true"
	  defaultCategory="General" logWarningsWhenNoCategoriesMatch="true">
		....
	</loggingConfiguration>
	<exceptionHandling>
		....
	</exceptionHandling>
	<cachingConfiguration defaultCacheManager="YGame.Server.Entities.EntityCache">
		....
	</cachingConfiguration>

	<connectionStrings>
		<add name="netTiersConnectionString" connectionString="Data Source=(local)\SQLEXPRESS;Initial Catalog=testDb;Persist Security Info=True;User ID=sa;Password=*****" providerName="System.Data.SqlClient"/>
	</connectionStrings>
</configuration>




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章