Activeation error occured while trying to get instance..

Topics: Data Access Application Block
Aug 21, 2012 at 5:31 PM

In the code line below, I'm getting the errors below.  I copied this from a web.config to the web.config I'm using:

  <configSections>
    <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
  </configSections>
  <dataConfiguration defaultDatabase="ISDBConnectionString"/>
  <connectionStrings>
    <add name="ISDBConnectionString" connectionString="servername;Initial Catalog=databasename;Integrated Security=true;" providerName="System.Data.SqlClient"/>
  </connectionStrings>

Could the issue be with the public key token? I copied the public key token for the dll from another web.conifg.  I'm not sure if each dll has an unique public key token.

code:

Database

SqlDb = DatabaseFactory.CreateDatabase(ConfigManager.ConnectionStrings["ISDBConnectionString"].ToString());

Errors:

Microsoft.Practices.ServiceLocation.ActivationException was unhandled by user code
  Message=Activation error occured while trying to get instance of type Database, key "Data Source=servername;Initial Catalog=databasename;Integrated Security=true;"
  Source=Microsoft.Practices.ServiceLocation
  StackTrace:
       at Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance(Type serviceType, String key) in c:\Home\Chris\Projects\CommonServiceLocator\main\Microsoft.Practices.ServiceLocation\ServiceLocatorImplBase.cs:line 53
       at Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance[TService](String key) in c:\Home\Chris\Projects\CommonServiceLocator\main\Microsoft.Practices.ServiceLocation\ServiceLocatorImplBase.cs:line 103
       at Microsoft.Practices.EnterpriseLibrary.Data.DatabaseFactory.InnerCreateDatabase(String name) in e:\Builds\EntLib\Latest\Source\Blocks\Data\Src\Data\DatabaseFactory.cs:line 82
       at Microsoft.Practices.EnterpriseLibrary.Data.DatabaseFactory.CreateDatabase(String name) in e:\Builds\EntLib\Latest\Source\Blocks\Data\Src\Data\DatabaseFactory.cs:line 68
       at BulkTradeAmendment._Default.GetBulkTradeAmendmentData() in C:\Documents and Settings\shahr3\my documents\visual studio 2010\Projects\BulkTradeAmendment\BulkTradeAmendment\Default.aspx.cs:line 40
       at BulkTradeAmendment._Default.Page_Load(Object sender, EventArgs e) in C:\Documents and Settings\shahr3\my documents\visual studio 2010\Projects\BulkTradeAmendment\BulkTradeAmendment\Default.aspx.cs:line 30
       at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
       at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
       at System.Web.UI.Control.OnLoad(EventArgs e)
       at System.Web.UI.Control.LoadRecursive()
       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
  InnerException: Microsoft.Practices.Unity.ResolutionFailedException
       Message=Resolution of the dependency failed, type = "Microsoft.Practices.EnterpriseLibrary.Data.Database", name = "Data Source=servername;Initial Catalog=daabasename;Integrated Security=true;".
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,Data Source=servername;Initial Catalog=databasename;Integrated Security=true;

       Source=Microsoft.Practices.Unity
       NameRequested=Data Source=servername;Initial Catalog=databasename;Integrated Security=true;
       TypeRequested=Database
       StackTrace:
            at Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing, String name, IEnumerable`1 resolverOverrides) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity\Src\UnityContainer.cs:line 515
            at Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, String name, IEnumerable`1 resolverOverrides) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity\Src\UnityContainer.cs:line 485
            at Microsoft.Practices.Unity.UnityContainer.Resolve(Type t, String name, ResolverOverride[] resolverOverrides) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity\Src\UnityContainer.cs:line 173
            at Microsoft.Practices.Unity.UnityServiceLocator.DoGetInstance(Type serviceType, String key) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity\Src\UnityServiceLocator.cs:line 64
            at Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance(Type serviceType, String key) in c:\Home\Chris\Projects\CommonServiceLocator\main\Microsoft.Practices.ServiceLocation\ServiceLocatorImplBase.cs:line 49
       InnerException: System.InvalidOperationException
            Message=The type Database cannot be constructed. You must configure the container to supply this value.
            Source=Microsoft.Practices.Unity
            StackTrace:
                 at Microsoft.Practices.ObjectBuilder2.DynamicMethodConstructorStrategy.GuardTypeIsNonPrimitive(IBuilderContext context, SelectedConstructor selectedConstructor) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity\Src\ObjectBuilder\Strategies\BuildPlan\DynamicMethod\Creation\DynamicMethodConstructorStrategy.cs:line 289
                 at Microsoft.Practices.ObjectBuilder2.DynamicMethodConstructorStrategy.PreBuildUp(IBuilderContext context) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity\Src\ObjectBuilder\Strategies\BuildPlan\DynamicMethod\Creation\DynamicMethodConstructorStrategy.cs:line 71
                 at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity\Src\ObjectBuilder\Strategies\StrategyChain.cs:line 110
                 at Microsoft.Practices.ObjectBuilder2.DynamicMethodBuildPlanCreatorPolicy.CreatePlan(IBuilderContext context, NamedTypeBuildKey buildKey) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity\Src\ObjectBuilder\Strategies\BuildPlan\DynamicMethod\DynamicMethodBuildPlanCreatorPolicy.cs:line 48
                 at Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity\Src\ObjectBuilder\Strategies\BuildPlan\BuildPlanStrategy.cs:line 37
                 at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity\Src\ObjectBuilder\Strategies\StrategyChain.cs:line 110
                 at Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing, String name, IEnumerable`1 resolverOverrides) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity\Src\UnityContainer.cs:line 511
            InnerException:


Locating source for 'e:\Builds\EntLib\Latest\Source\Blocks\Data\Src\Data\DatabaseFactory.cs'. Checksum: MD5 {47 a5 68 a5 71 72 0 7a 43 69 f3 d2 bb 1e 8f 7f}
The file 'e:\Builds\EntLib\Latest\Source\Blocks\Data\Src\Data\DatabaseFactory.cs' does not exist.
Looking in script documents for 'e:\Builds\EntLib\Latest\Source\Blocks\Data\Src\Data\DatabaseFactory.cs'...
Looking in the projects for 'e:\Builds\EntLib\Latest\Source\Blocks\Data\Src\Data\DatabaseFactory.cs'.
The file was not found in a project.
Looking in directory 'c:\Program Files\Microsoft Visual Studio 10.0\VC\crt\src\'...
Looking in directory 'c:\Program Files\Microsoft Visual Studio 10.0\VC\atlmfc\src\mfc\'...
Looking in directory 'c:\Program Files\Microsoft Visual Studio 10.0\VC\atlmfc\src\atl\'...
Looking in directory 'c:\Program Files\Microsoft Visual Studio 10.0\VC\atlmfc\include\'...
The debug source files settings for the active solution indicate that the debugger will not ask the user to find the file: e:\Builds\EntLib\Latest\Source\Blocks\Data\Src\Data\DatabaseFactory.cs.
The debugger could not locate the source file 'e:\Builds\EntLib\Latest\Source\Blocks\Data\Src\Data\DatabaseFactory.cs'.

Aug 21, 2012 at 5:54 PM

Update: The error isn't because of the public key token.  The public token for the dll that is referenced is the same that's mentioned in the web.config.

Aug 22, 2012 at 1:38 AM

You don't need to lookup the connection string -- the application block will do that for you based on the name.  So change the code from:

SqlDb = DatabaseFactory.CreateDatabase(ConfigurationManager.ConnectionStrings["ISDBConnectionString"].ToString());

to:

SqlDb = DatabaseFactory.CreateDatabase("ISDBConnectionString");

Or since you have set the default database to ISDBConnectionString you can also use:

SqlDb = DatabaseFactory.CreateDatabase();

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

Aug 22, 2012 at 2:09 AM
Edited Aug 22, 2012 at 2:19 AM

Hi Randy,

I made the changes you made above: SqlDb = DataBaseFactory.CreateDatabase();

I get the error below.   I'll add the Data.2010 project tomorrow and see which class and method it's failing so I can post a better error.  Thanks for your help.

Locating source for 'e:\Builds\EntLib\Latest\Source\Blocks\Data\Src\Data\DatabaseFactory.cs'. Checksum: MD5 {47 a5 68 a5 71 72 0 7a 43 69 f3 d2 bb 1e 8f 7f}
The file 'e:\Builds\EntLib\Latest\Source\Blocks\Data\Src\Data\DatabaseFactory.cs' does not exist.
Looking in script documents for 'e:\Builds\EntLib\Latest\Source\Blocks\Data\Src\Data\DatabaseFactory.cs'...
Looking in the projects for 'e:\Builds\EntLib\Latest\Source\Blocks\Data\Src\Data\DatabaseFactory.cs'.
The file was not found in a project.
Looking in directory 'c:\Program Files\Microsoft Visual Studio 10.0\VC\crt\src\'...
Looking in directory 'c:\Program Files\Microsoft Visual Studio 10.0\VC\atlmfc\src\mfc\'...
Looking in directory 'c:\Program Files\Microsoft Visual Studio 10.0\VC\atlmfc\src\atl\'...
Looking in directory 'c:\Program Files\Microsoft Visual Studio 10.0\VC\atlmfc\include\'...
The debug source files settings for the active solution indicate that the debugger will not ask the user to find the file: e:\Builds\EntLib\Latest\Source\Blocks\Data\Src\Data\DatabaseFactory.cs.
The debugger could not locate the source file 'e:\Builds\EntLib\Latest\Source\Blocks\Data\Src\Data\DatabaseFactory.cs'.

Do I have to gac the dll (Microsoft.Practices.EnterpriseLibrary.Data.dll)?

Aug 22, 2012 at 2:39 AM

No, you don't need to GAC the DLL (actually, could be considered harmful!).  It should work with the posted configuration.  What is the exact exception (and inner exception you are getting)?

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

Aug 22, 2012 at 3:22 AM

Right now I have the dll referenced.  Tomorrow morning I'll add the project so I can step into the Data project and provide you the exact exception.    After compiling and running the web app where should the Microsoft.Practices.EnterpriseLibrary.Data dll be located? The bin directory of the web app?

Do I have this correct? I've put the server name and database name in the actual 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" />
</configSections>
<dataConfiguration defaultDatabase="ISDBConnectionString"/>
<connectionStrings>
<add name="ISDBConnectionString" connectionString="servername;Initial Catalog=databasename;Integrated Security=true;" providerName="System.Data.SqlClient"/>
</connectionStrings>

Aug 22, 2012 at 3:53 AM

Yes, the DLLs should be in the bin directory.  You will need the following DLLs:

Microsoft.Practices.EnterpriseLibrary.Common.dll
Microsoft.Practices.EnterpriseLibrary.Data.dll
Microsoft.Practices.ServiceLocation.dll
Microsoft.Practices.Unity.dll
Microsoft.Practices.Unity.Interception.dll

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

Aug 22, 2012 at 1:05 PM
Edited Aug 22, 2012 at 1:51 PM

The error is in the DatabaseSyntheticConfigSettings.cs class in the DefaultDatabase property:

var databaseSettings = configurationSource.GetSection(DatabaseSettings.SectionName) as DatabaseSettings;

An error occurred creating the configuration section handler for dataConfiguration: Could not load file or assembly 'Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040) (C:\Documents and Settings\username\my documents\visual studio 2010\Projects\BulkTradeAmendment\BulkTradeAmendment\web.config line 10)

InnerException: Make sure that the file is a valid .NET framework assembly.

I'm referencing the dlls from C:\EntLib50Src\bin.

Aug 22, 2012 at 2:32 PM

What version of Enterprise Library are you using?  The config is specifying version 5.0.414.0.  Is it possible the assemblies are version 5.0.505.0?

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

Aug 22, 2012 at 2:58 PM

I checked the properties of Microsoft.Practices.EnterpriseLibrary.Data.dll and the version is 5.0.414.0.  I was reading this article: http://blogs.msdn.com/b/tomholl/archive/2007/04/19/avoiding-configuration-pitfalls-with-incompatible-copies-of-enterprise-library.aspx and I noticed that the public key token was null for the dlls referenced from C:\EntLib50Src\bin.  So I created a new web app and referenced the dlls from C:\Program Files\Microsoft Enterprise Library 5.0\Bin.  The public key token of the dll matches the public key token in the web.config.  But I'm getting the error below:

Locating source for 'e:\Builds\EntLib\Latest\Source\Blocks\Data\Src\Data\DatabaseFactory.cs'. Checksum: MD5 {47 a5 68 a5 71 72 0 7a 43 69 f3 d2 bb 1e 8f 7f}
The file 'e:\Builds\EntLib\Latest\Source\Blocks\Data\Src\Data\DatabaseFactory.cs' does not exist.
Looking in script documents for 'e:\Builds\EntLib\Latest\Source\Blocks\Data\Src\Data\DatabaseFactory.cs'...
Looking in the projects for 'e:\Builds\EntLib\Latest\Source\Blocks\Data\Src\Data\DatabaseFactory.cs'.
The file was not found in a project.
Looking in directory 'c:\Program Files\Microsoft Visual Studio 10.0\VC\crt\src\'...
Looking in directory 'c:\Program Files\Microsoft Visual Studio 10.0\VC\atlmfc\src\mfc\'...
Looking in directory 'c:\Program Files\Microsoft Visual Studio 10.0\VC\atlmfc\src\atl\'...
Looking in directory 'c:\Program Files\Microsoft Visual Studio 10.0\VC\atlmfc\include\'...
The debug source files settings for the active solution indicate that the debugger will not ask the user to find the file: e:\Builds\EntLib\Latest\Source\Blocks\Data\Src\Data\DatabaseFactory.cs.
The debugger could not locate the source file 'e:\Builds\EntLib\Latest\Source\Blocks\Data\Src\Data\DatabaseFactory.cs'.

I don't have more information on this error as I can't add the Data.2010 project to the web app.  The Data.2010 has dlls that conflict with dlls from Files\Microsoft Enterprise Library 5.0\Bin.

Aug 22, 2012 at 3:17 PM

I would recommend running Fuslogvw.exe (Assembly Binding Log Viewer) to see what is failing and why.  It should show which assembly could not be loaded.

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

Aug 22, 2012 at 3:54 PM
Edited Aug 22, 2012 at 6:44 PM

Thanks Randy. 

I have it working now.  These are the steps I did:

1) removed the version, culture, and public key token from the web.config

2) Commented the providerMappings tag from the web.config.

3) Removed the existing dlls from the bin, references.

4) Referenced the dlls from C:\C:\EntLib50Src\bin

I thought I already had tried this.  What is the providerMappings tag used for?  If the prod server has dlls that are GACed would I have to add the version, culture, and public key token?  

I'll revert back the web.config and download the fuslogvw.exe to see if I can find the issue.

Thanks for your help again.

Aug 23, 2012 at 4:09 AM

providerMappings maps the DBProviderFactory's to Enterprise Library database providers.  What provider are you mapping?

Yes, the configuration types should match the types in the production GAC.  Also bear in mind that you may have to fully qualify the Microsoft.Practices.EnterpriseLibrary.Data assembly as per http://entlib.codeplex.com/workitem/26903.

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

Aug 23, 2012 at 1:01 PM

I'm not sure. I saw the providerMappings tag in another web.config so I copied that into the web.config of the web app I'm building.  Is there a msdn article that educates about what needs to be added to the web.config for the EnterpriseLibrary?

Ok, will review the link you provided and revert back with any questions.

Thanks for your help.

Aug 23, 2012 at 6:55 PM

The providerMappings maps database provider factories (e.g. System.Data.SqlClient factory) to Enterprise Library Database classes.  For the out of the box supported databases (such as what you are using) this is not needed.  I didn't find a good reference except for the MSDN class reference.

To help sort out the XML configuration I would recommend using the configuration tool.  It will cover almost all scenarios (except for workarounds like I published earlier). 

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

Sep 6, 2012 at 3:00 PM

Hi Randy,

Currently my web.config has only one connection string.  What do I do when I include connection strings for uat and prod?  Then will I have to specify the connection string in the CreateDatabase? Or can I change the connectionString in the global.asax? 

So in the application_start I check the machine name of the server and then accordingly set the ISDBConnectionString value.

 <configSections>
    <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
  </configSections>
  <dataConfiguration defaultDatabase="ISDBConnectionString"/>
  <connectionStrings>
    <add name="ISDBConnectionString" connectionString="servername;Initial Catalog=databasename;Integrated Security=true;" providerName="System.Data.SqlClient"/>

<add name="ISDBConnectionString" connectionString="UATservername;Initial Catalog=databasename;Integrated Security=true;" providerName="System.Data.SqlClient"/>

  </connectionStrings>

Thanks!

Sep 6, 2012 at 4:05 PM

The Enterprise Library approach can be found in Appendix D - Enterprise Library Configuration Scenarios, Scenario 7 which involves using a delta file to merge the configuration information.

With VS2005 and VS2008 you could use Configuration Management within VS as outlined in Tip/Trick: Automating Dev, QA, Staging, and Production Web.Config Settings with VS 2005.

With VS2010, you can can use web.config transformations.  See:

for a walkthrough.

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