Activation error occured while trying to get instance of type Database, key ""

Topics: Data Access Application Block
Aug 12, 2012 at 7:34 AM

I have created  a DAL (class library, dll) to connect to databases. This project built in VS 2010 targetting .NET 4.0, references "Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.414.0,
                   Culture=neutral, PublicKeyToken=31bf3856ad364e35"

I am getting this error when instantiating a Database object through Factory whether i supply a kay or not.

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

Database db = DatabaseFactory.CreateDatabase();

// or  DatabaseFactory.CreateDatabase("someKey");

app.config
<?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="ConnString" />
    <connectionStrings>
        <add name="ConnString" connectionString="Database=Dummy;Server=.\MSSQLSERVER;Integrated Security=SSPI"
            providerName="System.Data.SqlClient" />
    </connectionStrings>
</configuration>

I have another project, a wrapper over Ent. Lib Unity
which loads the container and instantiates the objects using Unity Container. Another Project - BL, say Business Logic, which references the above project and gets any object of DAL.

I have another Console app (front end) which references BL.

All enterprise Lib Dlls are GACed.
When i invoke any method in BL from Console or test project,
which in turns tries to instantiate "Database object", i get the above error

Interestingly, the moment i place the "Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.414.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35" dll locally(Console Appliction's bin directory)
It succeeds in instantiation of a Database object.

Why is such a behaviour or am i missing anything?

Aug 13, 2012 at 7:10 AM

That's strange.  I would recommend running Fuslogvw.exe (Assembly Binding Log Viewer) to see what assemblies are being loaded and determining if there are any failures.  It would be interesting to compare the successful (local) test with an unsuccessful test to see if there are any differences.

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

Aug 13, 2012 at 11:06 AM
Edited Aug 13, 2012 at 11:07 AM

This is the reason for my troubles too.  (http://entlib.codeplex.com/discussions/389185)

Here are my fuslogvw.exe logs:

You can clearly see it stops if it does not find it locally:


Enterprise library sample (without data.dll in the output folder) :

=== Pre-bind state information ===
LOG: User = psyduck
LOG: DisplayName = Microsoft.Practices.EnterpriseLibrary.Data
 (Partial)
LOG: Appbase = file:///F:/Itineris/Documents/Downloads/EntLib Dev Guide RC - Code Samples/Logging/Logging/bin/Debug/
LOG: Initial PrivatePath = NULL
LOG: Dynamic Base = NULL
LOG: Cache Base = NULL
LOG: AppName = NULL
Calling assembly : Microsoft.Practices.EnterpriseLibrary.Common, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: F:\Itineris\Documents\Downloads\EntLib Dev Guide RC - Code Samples\Logging\Logging\bin\Debug\LoggingExample.exe.Config
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v2.0.50727\config\machine.config.
LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
LOG: Attempting download of new URL file:///F:/Itineris/Documents/Downloads/EntLib Dev Guide RC - Code Samples/Logging/Logging/bin/Debug/Microsoft.Practices.EnterpriseLibrary.Data.DLL.
LOG: Attempting download of new URL file:///F:/Itineris/Documents/Downloads/EntLib Dev Guide RC - Code Samples/Logging/Logging/bin/Debug/Microsoft.Practices.EnterpriseLibrary.Data/Microsoft.Practices.EnterpriseLibrary.Data.DLL.
LOG: Attempting download of new URL file:///F:/Itineris/Documents/Downloads/EntLib Dev Guide RC - Code Samples/Logging/Logging/bin/Debug/Microsoft.Practices.EnterpriseLibrary.Data.EXE.
LOG: Attempting download of new URL file:///F:/Itineris/Documents/Downloads/EntLib Dev Guide RC - Code Samples/Logging/Logging/bin/Debug/Microsoft.Practices.EnterpriseLibrary.Data/Microsoft.Practices.EnterpriseLibrary.Data.EXE.
LOG: All probing URLs attempted and failed.

Enterprise library sample (with data.dll in the output folder) :

=== Pre-bind state information ===
LOG: User = psyduck
LOG: DisplayName = Microsoft.Practices.EnterpriseLibrary.Data
 (Partial)
LOG: Appbase = file:///F:/Itineris/Documents/Downloads/EntLib Dev Guide RC - Code Samples/Logging/Logging/bin/Debug/
LOG: Initial PrivatePath = NULL
LOG: Dynamic Base = NULL
LOG: Cache Base = NULL
LOG: AppName = NULL
Calling assembly : Microsoft.Practices.EnterpriseLibrary.Common, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: F:\Itineris\Documents\Downloads\EntLib Dev Guide RC - Code Samples\Logging\Logging\bin\Debug\LoggingExample.exe.Config
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v2.0.50727\config\machine.config.
LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
LOG: Attempting download of new URL file:///F:/Itineris/Documents/Downloads/EntLib Dev Guide RC - Code Samples/Logging/Logging/bin/Debug/Microsoft.Practices.EnterpriseLibrary.Data.DLL.
LOG: Assembly download was successful. Attempting setup of file: F:\Itineris\Documents\Downloads\EntLib Dev Guide RC - Code Samples\Logging\Logging\bin\Debug\Microsoft.Practices.EnterpriseLibrary.Data.dll
LOG: Entering run-from-source setup phase.
LOG: Assembly Name is: Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
LOG: A partially-specified assembly bind succeeded from the application directory. Need to re-apply policy.
LOG: Using application configuration file: F:\Itineris\Documents\Downloads\EntLib Dev Guide RC - Code Samples\Logging\Logging\bin\Debug\LoggingExample.exe.Config
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v2.0.50727\config\machine.config.
LOG: Post-policy reference: Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
LOG: Binding succeeds. Returns assembly from C:\Windows\assembly\GAC_MSIL\Microsoft.Practices.EnterpriseLibrary.Data\5.0.505.0__31bf3856ad364e35\Microsoft.Practices.EnterpriseLibrary.Data.dll.
LOG: Assembly is loaded in default load context.
Aug 13, 2012 at 5:00 PM

@psyduck, thanks for posting your log.  This looks like an existing issue with loading assemblies in the GAC.  See http://entlib.codeplex.com/workitem/26903.  The posted solution is to fully qualify the assembly:

<runtime>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <qualifyAssembly partialName="Microsoft.Practices.EnterpriseLibrary.Data" fullName="Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
  </assemblyBinding>
</runtime>
 

Hopefully that helps.

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

Aug 17, 2012 at 6:33 AM

Thanks everyone.

Even i noticed the same thing in the fusion logs. I was wondering on Assembly load. Why loading with a partial name?

Interestingly, it is the Enterprise.Data which resulted into loading of Common assembly which in turns load using partial name.

 

Btw, it really solved my problem. Thanks every1 once again