I have multiple applications that use MS Enterprise Library and the Oracle ODP.Net drivers to connect to Oracle.
One application, a windows service, throws the following error when trying to create a connection to the Oracle DB:
The connection string for the database 'MyDBName' does not exist or does not have a valid provider.
Another application, a web service, does the exact same thing and it works fine. Both are running locally on my computer.
In the windows service, I checked the obvious: The connection string actually WAS there, the name was spelled right, and the Db Provider, Oracle.DataAccess.Client, was there and spelled right. I also confirmed ODP.net is correctly installed and configured in the machine.config (it would have to be because the web service works). Both web and win service are using .net 4.5.1. So I believe all the obvious has been ruled out.
Stepping into the ent lib code I found the source of the exception was because this method was returning false when called under the windows service:
private static bool IsValidProviderName(string providerName)
{
return DbProviderFactories.GetFactoryClasses().Rows.Find(providerName) != null;
}
It returns true when running under the web service. In both cases, the value of the supplied providerName is the same (Oracle.DataAccess.Client). I then looked at the Rows returned by GetFactoryClasses(), and realized that under the windows service, only a handful (about half) of the registered factory classes are returned from the machine.config, and the Oracle one was not among them. So that's the reason it's failing. To hack this into working, I've added the following to the app.config for my service:
<DbProviderFactories>
<remove invariant="Oracle.ManagedDataAccess.Client"/>
<remove invariant="Oracle.DataAccess.Client"/>
<add name="ODP.NET, Managed Driver" invariant="Oracle.ManagedDataAccess.Client" description="Oracle Data Provider for .NET, Managed Driver" type="Oracle.ManagedDataAccess.Client.OracleClientFactory, Oracle.ManagedDataAccess, Version=4.121.1.0, Culture=neutral, PublicKeyToken=89b483f429c47342" />
<add name="ODP.NET, Unmanaged Driver" invariant="Oracle.DataAccess.Client" description="Oracle Data Provider for .NET, Unmanaged Driver" type="Oracle.DataAccess.Client.OracleClientFactory, Oracle.DataAccess, Version=4.121.1.0, Culture=neutral, PublicKeyToken=89b483f429c47342" />
</DbProviderFactories>
These are copied straight out of the machine.config.
So my question is basically, why do two applications, running under the same version of .net, not produce consistent results when enumerating the registered DbProviderFactories?