Activation error occured while trying to get instance of type OracleDatabase, key "" - Activation Exception

Topics: Data Access Application Block, General discussion
Sep 18, 2013 at 10:29 PM
Hello All,

We are using Enterprise Library building block 5.0 to connect to Oracle Database. Recently we migrated from ASP.NET 4.0 to ASP.NET MVC 4.0. After migrating to MVC, we started getting Activation Exception in our Data Access Layer.

Let me share you my code.

Web.Config
<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>  
  <dataConfiguration>
    <providerMappings>
      <add databaseType="EntLibContrib.Data.OdpNet.OracleDatabase, EntLibContrib.Data.OdpNet, Version=5.0.505.0, Culture=neutral, PublicKeyToken=null" name="Oracle.DataAccess.Client"/>
    </providerMappings>
  </dataConfiguration>
Due to security and audit issue, we can't put the connection string in web.config. We are reading it from secure location. We are creating OracleDatabase instance in Application_Start event of Global.asax file.
string connectionString =  "CONNECTION STRING";
 OracleDatabase database = new OracleDatabase(connectionString);

IUnityContainer container = new UnityContainer();
container.AddNewExtension<EnterpriseLibraryCoreExtension>();

var configurator = new UnityContainerConfigurator(container);
EnterpriseLibraryContainer.Current = new UnityServiceLocator(container);

container.RegisterInstance<OracleDatabase>(database);            

AreaRegistration.RegisterAllAreas();

WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);       
In Data Access Layer We are trying to get instance of OracleDatabase which throws Activation Exception.
Collection<Entity> entityCollection = new Collection<Entity>();            
OracleDatabase database = EnterpriseLibraryContainer.Current.GetInstance<OracleDatabase>();
using (DbCommand dbCommand = database.GetStoredProcCommand("USP_NAME"))
Code Removed for clarity
Inner exception is
Resolution of the dependency failed, type = "EntLibContrib.Data.OdpNet.OracleDatabase", name = "(none)".
Exception occurred while: while resolving.
Exception is: InvalidOperationException - The type String cannot be constructed. You must configure the container to supply this value.
-----------------------------------------------
At the time of the exception, the container was:

  Resolving EntLibContrib.Data.OdpNet.OracleDatabase,(none)
  Resolving parameter "connectionString" of constructor EntLibContrib.Data.OdpNet.OracleDatabase(System.String connectionString, System.Collections.Generic.IEnumerable`1[[EntLibContrib.Data.OdpNet.IOraclePackage, EntLibContrib.Data.OdpNet, Version=5.0.505.0, Culture=neutral, PublicKeyToken=null]] packages, Microsoft.Practices.EnterpriseLibrary.Data.Instrumentation.IDataInstrumentationProvider instrumentationProvider)
    Resolving System.String,(none)
What could be the root cause of this? Any help is highly appreciated.

Thank you.
Sep 20, 2013 at 7:33 AM
It looks like, for some reason, the OracleDatabase that you are registering with the container is not registered later on in the DAL. Because the instance is not registered, Unity tries to create an instance but it does not know how to instantiate the type string.

Is it possible that the container instance gets refreshed/created anywhere else (e.g. App_Start)? Is the DAL running in the same AppDomain as the WebApp? Does it make any difference if you move the Unity initialization to after the MVC initialization (don't think it will, though)?

What if you try to register a simple object instance as a test:
    public class MyClass
    {
        public MyClass (string myString)
        {
        }
    }

// Global.asax
IUnityContainer container = new UnityContainer();
container.AddNewExtension<EnterpriseLibraryCoreExtension>();

var configurator = new UnityContainerConfigurator(container);
EnterpriseLibraryContainer.Current = new UnityServiceLocator(container);

container.RegisterInstance<MyClass>(new MyClass("Test"));

// DAL
MyClass myClass = EnterpriseLibraryContainer.Current.GetInstance<MyClass>();            
Now if the above works (MyClass can be resolved) then it would point to something around the Database classes or configuration (maybe DbProviderFactories?). If it doesn't work then it looks like the UnityContainer is being replaced.

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to
Sep 23, 2013 at 4:58 PM
Hi Randy,

Thanks for your prompt reply.

I could fix the issue. I was using different namespaces in Global.asax and in Data Access Layer.

In Global.asax I was registering OracleDatabase instance using Microsoft.Practices.EnterpriseLibrary.Data.Oracle namespace whereas in Data Access layer, I was trying to resolve it using EntLibContrib.Data.OdpNet. This was causing the issue. I changed the namespace in Global.asax with EntLibContrib.Data.OdpNet and it worked.

Thank you.
Marked as answer by randylevy on 10/7/2013 at 10:56 PM
Mar 24, 2014 at 9:34 PM
Is it possible use Ent Lib 5.0 and ODP.NET not managed (Oracle.DataAccess 4.112.2.0 - 64 bit) and ASP.NET 4.5 , 64 bits ?