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

Topics: Data Access Application Block
Mar 8, 2012 at 5:06 PM

I'm updating EntLib from 4.1 to 5 in our Biztalk Server 2010 application.

Since the upgrade, we are getting an error accessing the database. I have updated the configuration appropriately, even used the configuration tool which came with EntLib 5.  I've tried out the different code samples around along from sources which encountered a similar issue. GACed every single DLL which came with EntLib 5 (note that we're working on new env so there's no trace of EntLib 4 libraries)

I'm now out of ideas on what might be causing this and will revert to 4.1 if I don't find a solution soon.

 

Could the issue be due to mixing of .NET 2 and .NET 4 libraries? (BizTalk 2010 runs on .NET 4 CLR)

 

TIA!

 



Exception type: ActivationException
Source: Microsoft.Practices.ServiceLocation
Target Site: System.Object GetInstance(System.Type, System.String)
The following is a stack trace that identifies the location where the exception occured

   at Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance(Type serviceType, String key)
   at Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance[TService](String key)
   at Microsoft.Practices.EnterpriseLibrary.Data.DatabaseFactory.InnerCreateDatabase(String name)
   at Microsoft.Practices.EnterpriseLibrary.Data.DatabaseFactory.CreateDatabase(String name)
   at Common.DatabaseHandler.DbHandler.ExecuteScalar(String dbConnectionStringName, String query, Dictionary`2 parameters)
   at Common.DatabaseHandler.DbHandler.ExecuteScalar(String dbConnectionStringName, String query)
...
Additional error information:

        Resolution of the dependency failed, type = "Microsoft.Practices.EnterpriseLibrary.Data.Database", name = "DB1".
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,DB1

       
Exception type: ResolutionFailedException
Source: Microsoft.Practices.Unity
Target Site: System.Object DoBuildUp(System.Type, System.Object, System.String, System.Collections.Generic.IEnumerable`1[Microsoft.Practices.Unity.ResolverOverride])
The following is a stack trace that identifies the location where the exception occured

   at Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing, String name, IEnumerable`1 resolverOverrides)
   at Microsoft.Practices.Unity.UnityContainer.Resolve(Type t, String name, ResolverOverride[] resolverOverrides)
   at Microsoft.Practices.Unity.UnityServiceLocator.DoGetInstance(Type serviceType, String key)
   at Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance(Type serviceType, String key)
Additional error information:

        The type Database cannot be constructed. You must configure the container to supply this value.
       
Exception type: InvalidOperationException
Source: Microsoft.Practices.Unity
Target Site: Void GuardTypeIsNonPrimitive(Microsoft.Practices.ObjectBuilder2.IBuilderContext, Microsoft.Practices.ObjectBuilder2.SelectedConstructor)
The following is a stack trace that identifies the location where the exception occured

   at Microsoft.Practices.ObjectBuilder2.DynamicMethodConstructorStrategy.GuardTypeIsNonPrimitive(IBuilderContext context, SelectedConstructor selectedConstructor)
   at Microsoft.Practices.ObjectBuilder2.DynamicMethodConstructorStrategy.PreBuildUp(IBuilderContext context)
   at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context)
   at Microsoft.Practices.ObjectBuilder2.DynamicMethodBuildPlanCreatorPolicy.CreatePlan(IBuilderContext context, NamedTypeBuildKey buildKey)
   at Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context)
   at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context)
   at Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing, String name, IEnumerable`1 resolverOverrides)


 

Below's the 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" />
        <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
    </configSections>

<startup useLegacyV2RuntimeActivationPolicy="true">
        <supportedRuntime version="v4.0" />
    </startup>

    <connectionStrings>
        <add name="DB1" connectionString="Data Source=localhost;Initial Catalog=DB1;Persist Security Info=True;User ID=dbuser1;Password=password;"     providerName="System.Data.SqlClient" />
    </connectionStrings>


 

Here's the code


public static object ExecuteScalar(string dbConnectionStringName, string query, Dictionary<string, DatabaseSQLParameters> parameters)
        {
            //Database db = DatabaseFactory.CreateDatabase(dbConnectionStringName);
            Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>(dbConnectionStringName);

            string providerName = ConfigurationManager.ConnectionStrings[dbConnectionStringName].ProviderName;

            DbCommand dbCmd;
            object returnVal;

            // Fill parameters
            if (parameters != null)
            {
                dbCmd = db.GetStoredProcCommand(query);
               
                foreach (KeyValuePair<string, DatabaseSQLParameters> keyValuePair in parameters)
                {
                    DatabaseSQLParameters parameter = keyValuePair.Value;

                    if (parameter.Direction == ParameterDirection.Input)
                    {
                        db.AddInParameter(dbCmd, parameter.ParameterName, parameter.ParameterType, parameter.ParameterValue);
                    }
                    else if (parameter.Direction == ParameterDirection.Output)
                    {
                        db.AddOutParameter(dbCmd, parameter.ParameterName, parameter.ParameterType, int.MaxValue);
                    }
                }
            }
            else
            {
                dbCmd = db.GetSqlStringCommand(query);
            }

            // Execute query
            returnVal = db.ExecuteScalar(dbCmd);

            // Get out parameters
            if (parameters != null)
            {
                foreach (KeyValuePair<string, DatabaseSQLParameters> keyValuePair in parameters)
                {
                    DatabaseSQLParameters parameter = keyValuePair.Value;

                    if (parameter.Direction == ParameterDirection.Output)
                    {
                        parameter.OutValue = dbCmd.Parameters[parameter.ParameterName].Value;
                    }
                }
            }

            // return
            return returnVal;
        }


Mar 9, 2012 at 6:51 AM
Edited Mar 9, 2012 at 6:57 AM

The error message indicates that the configuration is incorrect or has not been loaded.

Since the posted configuration looks OK, I'm wondering if it is deployed.  Is your configuration deployed to the BTSNTSvc.exe.config file located in C:\Program Files\Microsoft BizTalk Server 2010\?  Are you running 64 bit version of BizTalk (with 64 bit host)?  If so, then the config file would be BTSNTSvc64.exe.config.

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com 

Mar 9, 2012 at 8:13 AM

Hi Randy.

We're running in 32-bit and updated the correct config file.  Just reverted to EntLib 4 and all is working fine again.

Very odd -_-