Exception is: InvalidOperationException - The type Database cannot be constructed. You must configure the container to supply this value.

Topics: Data Access Application Block
Mar 23, 2011 at 7:34 AM

Hi,

I'm getting error when i try to create resolve database object using unity container. I'm trying to read configuration from redirected config section. Following is my code,

                IUnityContainer unityCon = new UnityContainer();
                unityCon.AddNewExtension<EnterpriseLibraryCoreExtension>();
                Database dbobj = unityCon.Resolve<Database>();

My app.config is

 <?xml version="1.0"?>
<configuration>
    <configSections>
        <section name="enterpriseLibrary.ConfigurationSource" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ConfigurationSourceSection, Microsoft.Practices.EnterpriseLibrary.Common, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
    </configSections>
<enterpriseLibrary.ConfigurationSource selectedSource="System Configuration Source"
    parentSource="">
    <sources>
        <add name="System Configuration Source" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.SystemConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
        <add name="DbConfig" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
            filePath="E:\Temp Project\TestRedirectsection\shared.config" />
    </sources>
    <redirectSections>
        <add sourceName="DbConfig" name="dataConfiguration" />
    </redirectSections>
</enterpriseLibrary.ConfigurationSource>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>

 

And my redirected config file is,

<?xml version="1.0" encoding="utf-8" ?>
<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="true" />
    </configSections>
    <dataConfiguration defaultDatabase="Connection String" />
    <connectionStrings>
        <add name="Connection String" connectionString="User Id=bbusers;Password=bluebutton;Initial Catalog=bksnet;Data Source=192.168.2.7;"
            providerName="System.Data.SqlClient" />
    </connectionStrings>
</configuration>

 

When i run the code i'm getting resolutionfailed exception. how to solve this issue?

 

Thanks,

 

 


Mar 23, 2011 at 8:37 AM

Add a redirect section for the connection strings as well.

 

Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

Mar 23, 2011 at 10:16 AM

Thanks, I added a redirect for connection and it is working fine.