Enterprise Library 5.0 Data Component usage in .NET COM Component

Topics: Data Access Application Block, Enterprise Library Core
Sep 29, 2014 at 9:07 PM
Hi, I have implemented a .NET library (dll) which is COM compatible. I am using the Enterprise library 5.0 Data component for data access. If I run the COM compatible library in Visual Studio debug mode, it works fine. If i register through regasm and run it, it always throws the error message. "Activation error occured while trying to get instance of type Database, key."

Question:
  1. Is it allowed to use Enterprise library Data component in .NET COM Compatible library?
  2. If not, Do we have any other options to access database in COM Compatible .NET library?
  3. Any suggestion, reason for this error msg.
I am using Oracle as the back end.

Thanks,
Satish
Oct 1, 2014 at 2:46 AM
The issue is that the XML configuration is not loaded when running as a COM component. You could either use programmatic configuration or explicitly load the configuration (e.g. use a FileConfigurationSource to load a config file). e.g.
var configSource = new FileConfigurationSource("entlib.config");
var dbFactory = new DatabaseProviderFactory(configSource);
Database myDb = dbFactory.CreateDefault();

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to
Oct 1, 2014 at 6:12 PM
Edited Oct 1, 2014 at 6:14 PM
I am still getting the Activation error. Here is the summary, what i did?

app.config contains the following entry.

<configSections>
<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</configSections>

Connection String information in ConnectionString.config file.

<configuration>
<configSections>
<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</configSections>

<connectionStrings>
<add name="Oracle_DB_Connection_1" connectionString="Data Source=DB1;user id=xxxx; password=xxxx; Max Pool Size=30; Connection Lifetime=1800" providerName="Oracle.DataAccess.Client"/>
<add name="Oracel_DB_Connection_2" connectionString="Data Source=DB2;user id=xxxx; password=xxxx; Max Pool Size=30; Connection Lifetime=1800" providerName="Oracle.DataAccess.Client"/>
</connectionStrings>
</configuration>
Code:

try
        {
            FileConfigurationSource dataSource = new FileConfigurationSource("connectionStrings.config");
            DatabaseProviderFactory dbFactory = new DatabaseProviderFactory(dataSource);


            Database _db1 = dbFactory.Create(DatabaseConnections.ORACLE_DB_CONNECTION1);
            Database _db2 = dbFactory.Create(DatabaseConnections.ORACLE_DB_CONNECTION2);


        }
Whenever it executes dbFactory.Create(DatabaseConnections.ORACLE_DB_CONNECTION1);, it throws activation error.

DatabaseConnections is a resource where mapping was defined.

DatabaseConnections.ORACLE_DB_CONNECTION1 - Oracle_DB_Connection_1
DatabaseConnections.ORACLE_DB_CONNECTION2 - Oracle_DB_Connection_2

Please advice.
Oct 3, 2014 at 7:23 AM
If the configuration is loaded, then I wonder if the DbProviderFactory settings are configured for Oracle.DataAccess.Client. If you change the providerName to "System.Data.SqlClient" is the Database object returned?

Can you provide a sample solution to replicate the issue? It would help. Thanks.

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to