DatabaseFactory.CreateDatabase(), Exception: Activation error occured while trying to get instance of type Database, key "" Exception is: InvalidOperationException - The type Database cannot be constructed. ..

Topics: Data Access Application Block
Sep 23, 2013 at 6:27 PM
Hi,

I've seen similar posts and I've tried much of what has been suggested without luck.

When I attempt to execute the following line:
       Database db = DatabaseFactory.CreateDatabase();
I get the following exception (partial):

Microsoft.Practices.ServiceLocation.ActivationException was unhandled by user code
HResult=-2146233088
Message=Activation error occured while trying to get instance of type Database, key ""
Source=Microsoft.Practices.ServiceLocation
StackTrace:
   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()
   at Microsoft.Practices.ESB.AlertService.DataAccess.AlertDataAccess.TestDatabaseConnectivity() in c:\Projects\Microsoft.Practices.ESB\Source\Samples\Management Portal\ESB.AlertService\DataAccess\AlertDataAccess.cs:line 252
   at Microsoft.Practices.ESB.AlertService.Process.OnStart(String[] args) in c:\Projects\Microsoft.Practices.ESB\Source\Samples\Management Portal\ESB.AlertService\Process.cs:line 78
   at System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback(Object state)
InnerException: Microsoft.Practices.Unity.ResolutionFailedException
   HResult=-2146233088
   Message=Resolution of the dependency failed, type = "Microsoft.Practices.EnterpriseLibrary.Data.Database", name = "(none)".
Exception occurred while: while resolving.
Exception is: InvalidOperationException - The type Database cannot be constructed. You must configure the container to supply this value.

The code is a Windows Service running on Windows 2012 is attempting to access a database on a separate server (SQL 2012 running on Windows 2012).




The complete app.config is as follows:

<configuration>
<configSections>
    <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
    <section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />   
    <section name="cachingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Caching.Configuration.CacheManagerSettings, Microsoft.Practices.EnterpriseLibrary.Caching, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
        <section name="Microsoft.Practices.ESB.Portal.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
        <section name="ESB.Portal.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
    </sectionGroup>
    <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
        <section name="Microsoft.Practices.ESB.AlertService.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false"/>
    </sectionGroup>
</configSections>
<dataConfiguration defaultDatabase="EsbExceptionDb"/>
<connectionStrings>
<add name="EsbExceptionDb" connectionString="Data Source=bizdbdev;Initial Catalog=EsbExceptionDb;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>

<enterpriseLibrary.ConfigurationSource selectedSource="AlertService">
    <sources>
        <add name="AlertService" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common, Version=5.0.414.0,Culture=neutral, PublicKeyToken=31bf3856ad364e35" filePath="BizTalk ESB Exception Notification.exe.config"/>
    </sources>
</enterpriseLibrary.ConfigurationSource>

<loggingConfiguration name="Logging Application Block" tracingEnabled="false" defaultCategory="Default Category" logWarningsWhenNoCategoriesMatch="false">
    <listeners>
        <add source="BizTalk ESB Exception Notification" formatter="Default Formatter" log="" machineName="." listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" traceOutputOptions="None" filter="All" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="Event Log Destination"/>
        <add fileName="trace.log" header="" footer="" formatter="" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" traceOutputOptions="None" filter="All" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="Flat File Destination"/>
    </listeners>
    <formatters>
        <add template="Timestamp: {timestamp}
Message: {message}
Category: {category}
Priority: {priority}
EventId: {eventid}
Severity: {severity}
Title:{title}
Machine: {machine}
Application Domain: {appDomain}
Process Id: {processId}
Process Name: {processName}
Win32 Thread Id: {win32ThreadId}
Thread Name: {threadName}
Extended Properties: {dictionary({key} - {value}
)}" type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="Default Formatter"/>
    </formatters>
    <logFilters>
        <add categoryFilterMode="AllowAllExceptDenied" type="Microsoft.Practices.EnterpriseLibrary.Logging.Filters.CategoryFilter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="Category"/>
        <add minimumPriority="0" maximumPriority="2147483647" type="Microsoft.Practices.EnterpriseLibrary.Logging.Filters.PriorityFilter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="Priority"/>
    </logFilters>
    <categorySources>
        <add switchValue="All" name="Default Category">
            <listeners>
                <add name="Event Log Destination"/>
            </listeners>
        </add>
        <add switchValue="All" name="Tracing">
            <listeners>
                <add name="Flat File Destination"/>
            </listeners>
        </add>
    </categorySources>
    <specialSources>
        <allEvents switchValue="All" name="All Events"/>
        <notProcessed switchValue="All" name="Unprocessed Category"/>
        <errors switchValue="All" name="Logging Errors &amp; Warnings">
            <listeners>
                <add name="Event Log Destination"/>
            </listeners>
        </errors>
    </specialSources>
</loggingConfiguration>

<exceptionHandling>
    <exceptionPolicies>
        <add name="Exception Policy">
            <exceptionTypes>
                <add type="System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" postHandlingAction="None" name="Exception">
                    <exceptionHandlers>
                        <add logCategory="Default Category" eventId="100" severity="Error" title="Exception" formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling" priority="0" useDefaultLogger="false" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="Logging Handler"/>
                    </exceptionHandlers>
                </add>
            </exceptionTypes>
        </add>
    </exceptionPolicies>
</exceptionHandling>

<cachingConfiguration defaultCacheManager="Cache Manager">
    <cacheManagers>
        <add expirationPollFrequencyInSeconds="60" maximumElementsInCacheBeforeScavenging="1000" numberToRemoveWhenScavenging="10" backingStoreName="Null Storage" type="Microsoft.Practices.EnterpriseLibrary.Caching.CacheManager, Microsoft.Practices.EnterpriseLibrary.Caching, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="Cache Manager"/>
    </cacheManagers>
    <backingStores>
        <add encryptionProviderName="" type="Microsoft.Practices.EnterpriseLibrary.Caching.BackingStoreImplementations.NullBackingStore, Microsoft.Practices.EnterpriseLibrary.Caching, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="Null Storage"/>
    </backingStores>
</cachingConfiguration>

<userSettings>
    <Microsoft.Practices.ESB.AlertService.Properties.Sett
Sep 25, 2013 at 6:27 AM
Edited Sep 25, 2013 at 6:27 AM
Is this running inside of BizTalk? Have you installed Enterprise Library in the GAC? If so then you should add the following to the config file to help resolve the Microsoft.Practices.EnterpriseLibrary.Data.dll.
<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>
 
~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to