Error update Enterprise library 4.1 to Enterprise library 6

Topics: Enterprise Library Core
Oct 17, 2013 at 11:53 AM
Hello,
I have updated a Windows aplication from .NET Framework 3.5 to 4.5 and in this update I have update Enterprise library 4.1 to Enterprise library 6. I have two config files called 'app.config' and 'EntLib.config'. The 'EntLib.config' file contains the connectionStrings. The content of the 'app.config' file where reference to 'EntLib.config' is:
<section name="enterpriseLibrary.ConfigurationSource" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ConfigurationSourceSection, Microsoft.Practices.EnterpriseLibrary.Common, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>

<enterpriseLibrary.ConfigurationSource selectedSource="File Configuration Source">
  <sources>
     <add name="File Configuration Source" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    filePath="EntLib.config" />
     <add name="System Configuration Source" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.SystemConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
  </sources>
</enterpriseLibrary.ConfigurationSource>

This configuration is correct in Enterprise library 4.1 but with the Enterprise library 6 when I run then aplication It don't find the 'EntLib.config' therefore don't find the connectionStrings for connect to Database.

Can somebody help me?.
Thanks.
Oct 17, 2013 at 3:06 PM
If you haven't done so, I recommend reading the Enterprise Library Migration Guide.

What exact error/exception are you seeing?
Are you only using the Database Application Block?

You will need to bootstrap the blocks:
var configSource = ConfigurationSourceFactory.Create();
DatabaseFactory.SetDatabaseProviderFactory(new DatabaseProviderFactory(configSource), false);

If that doesn't help, can you post the entlib.config as well as the code you use to bootstrap and call the block (along with the specific error).

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to
Oct 17, 2013 at 3:56 PM
Hi Randy, thanks for your reply.

The error occurs when I call to Database the first time. Here is the line where the error occurs (the second one):

Dim BD As Microsoft.Practices.EnterpriseLibrary.Data.Database
BD = New Microsoft.Practices.EnterpriseLibrary.Data.DatabaseProviderFactory().Create("DB1")

And the error is:
The requested database DB1 is not defined in configuration.

Before the second line I have introduced then lines you send me:
Dim config As IConfigurationSource = ConfigurationSourceFactory.Create()
DatabaseFactory.SetDatabaseProviderFactory(New DatabaseProviderFactory(config), False)

But the error continue.

In the event Startup of the aplication I had introduced this lines only for the migration:
Dim config As IConfigurationSource = ConfigurationSourceFactory.Create()
Dim logWriterFactory As LogWriterFactory = New LogWriterFactory(config)
Logger.SetLogWriter(logWriterFactory.Create())
Dim factory As ExceptionPolicyFactory = New ExceptionPolicyFactory(config)
Dim exManager As ExceptionManager = factory.CreateManager()
ExceptionPolicy.SetExceptionManager(exManager)

The EntLib.config have the next sections:
<section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
<section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />

And the conectionstrings:
<dataConfiguration defaultDatabase="DB1" />
<connectionStrings>
    <add name="DB1" connectionString="Database=BD;Server=DESARROLLO;Integrated Security=SSPI"
        providerName="System.Data.SqlClient" />
</connectionStrings>

In app.config I had introduced redirectSections into enterpriseLibrary.ConfigurationSource but the error continue and I don't know if it's necessary.
<section name="enterpriseLibrary.ConfigurationSource" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ConfigurationSourceSection, Microsoft.Practices.EnterpriseLibrary.Common, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>

<enterpriseLibrary.ConfigurationSource selectedSource="File Configuration Source">
  <sources>
     <add name="File Configuration Source" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    filePath="EntLib.config" />
     <add name="System Configuration Source" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.SystemConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
  </sources>
  <redirectSections>
     <add sourceName="File Configuration Source" name="loggingConfiguration "/>
     <add sourceName="File Configuration Source" name="exceptionHandling" />
     <add sourceName="File Configuration Source" name="dataConfiguration" />
  </redirectSections>
</enterpriseLibrary.ConfigurationSource>
<applicationSettings>

Thanks.
Oct 17, 2013 at 4:58 PM
Edited Oct 17, 2013 at 4:59 PM
Have you deployed the EntLib.config file to the output folder?

The following code will throw an exception if the configuration is using a file configuration source:
BD = New Microsoft.Practices.EnterpriseLibrary.Data.DatabaseProviderFactory().Create("DB1") 
This is because it will use the default .NET configuration file and not the Enterprise Library file configuration source. You can use the static DatabaseFactory method instead:
Dim BD As Database = DatabaseFactory.CreateDatabase()
So here is my app.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="enterpriseLibrary.ConfigurationSource" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ConfigurationSourceSection, Microsoft.Practices.EnterpriseLibrary.Common, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
    </configSections>
    <enterpriseLibrary.ConfigurationSource selectedSource="File-based Configuration Source">
        <sources>
            <add name="File-based Configuration Source" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                filePath="entlib.config" />
        </sources>
    </enterpriseLibrary.ConfigurationSource>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
</configuration>
Here is my EntLib.config (deployed to the bin\debug\ folder):
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
        <section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
        <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
    </configSections>
  <loggingConfiguration name="" tracingEnabled="true" defaultCategory="General">
    <listeners>
      <add name="Event Log Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
          listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
          source="Enterprise Library Logging" formatter="Text Formatter"
          log="" machineName="." traceOutputOptions="None" />
    </listeners>
    <formatters>
      <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
          template="Timestamp: {timestamp}{newline}&#xA;Message: {message}{newline}&#xA;Category: {category}{newline}&#xA;Priority: {priority}{newline}&#xA;EventId: {eventid}{newline}&#xA;Severity: {severity}{newline}&#xA;Title:{title}{newline}&#xA;Machine: {localMachine}{newline}&#xA;App Domain: {localAppDomain}{newline}&#xA;ProcessId: {localProcessId}{newline}&#xA;Process Name: {localProcessName}{newline}&#xA;Thread Name: {threadName}{newline}&#xA;Win32 ThreadId:{win32ThreadId}{newline}&#xA;Extended Properties: {dictionary({key} - {value}{newline})}"
          name="Text Formatter" />
    </formatters>
    <categorySources>
      <add switchValue="All" name="General">
        <listeners>
          <add name="Event Log Listener" />
        </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 Listener" />
        </listeners>
      </errors>
    </specialSources>
  </loggingConfiguration>
  <exceptionHandling />
  <dataConfiguration defaultDatabase="DB1" />
  <connectionStrings>
      <add name="DB1" connectionString="Database=BD;Server=DESARROLLO;Integrated Security=SSPI"
          providerName="System.Data.SqlClient" />  
  </connectionStrings>
</configuration>
And here is my code:
' Startup
Dim configSource = ConfigurationSourceFactory.Create()
DatabaseFactory.SetDatabaseProviderFactory(New DatabaseProviderFactory(configSource), False)
Logger.SetLogWriter(New LogWriterFactory(configSource).Create())
ExceptionPolicy.SetExceptionManager(New ExceptionPolicyFactory(configSource).CreateManager())

' ...later
Dim db1 As Database = DatabaseFactory.CreateDatabase()
~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to
Marked as answer by randylevy on 10/18/2013 at 7:05 AM
Oct 18, 2013 at 10:08 AM
Hi Randy,
With the changes that you send me the aplication run perfectly.
Thank you very much :)