DAAB: Unity Activation error

Topics: Data Access Application Block
Jun 17, 2010 at 12:06 PM

Hello,

after trying this within VS2010 Windows Forms Project:

			var _unity = new UnityContainer ();
var configurator = new UnityContainerConfigurator ( _unity );
EnterpriseLibraryContainer.ConfigureContainer ( configurator, ConfigurationSourceFactory.Create () );

var db= EnterpriseLibraryContainer.Current.GetInstance<Database> ( "mydb" ); // <== CRASH


using ( DbCommand com = db.GetSqlStringCommand ( "select count(*) from schema.table1 for read only with ur" ) )
{
object o = db.ExecuteScalar ( com );
MessageBox.Show ( o.ToString () );
}

An exception occured while trying to resolve the database-object from Unity (row marked  with // <== CRASH):

{"Activation error occured while trying to get instance of type Database, key \"mydb\""}

{"Resolution of the dependency failed, type = \"Microsoft.Practices.EnterpriseLibrary.Data.Database\", name = \"mydb\".\r\nException occurred while: while resolving.\r\nException is: InvalidOperationException - The type Database cannot be constructed. You must configure the container to supply this value.\r\n-----------------------------------------------\r\nAt the time of the exception, the container was:\r\n\r\n  Resolving Microsoft.Practices.EnterpriseLibrary.Data.Database,mydb\r\n"}

Here is app.config:

<configuration>
<configSections>
<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false"/>
<section name="typeRegistrationProvidersConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.TypeRegistrationProvidersConfigurationSection, Microsoft.Practices.EnterpriseLibrary.Common, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</configSections>
<typeRegistrationProvidersConfiguration>
<remove name="Data Access" />
<add name="Data Access" providerType="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSyntheticConfigSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</typeRegistrationProvidersConfiguration>
<dataConfiguration defaultDatabase="mydb"/>
<connectionStrings>
<add name="NexumBB" connectionString="Database=testdb;User ID=myuser;Server=192.168.100.1;Persist Security Info=True;Password=mypwd" providerName="IBM.Data.DB2"/>
</connectionStrings>
</configuration>

The necessary entlib-dlls's are local, not from GAC - this problem is known:
http://entlib.codeplex.com/workitem/26903?ProjectName=entlib
So my question: what else prevents the successful execution? Coded as an VS2008 project it works. :-|
Thanks for any help, schue.
Jun 17, 2010 at 1:07 PM

The connectionString "mydb" seems to be missing in your configuration. Update it accordingly and let us know if this resolves the issue.

Gino Terrado
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

 

Jun 17, 2010 at 1:18 PM

Thank you for the reply. But this is'nt the reason. My fault was, I only forgot to switch within example code from 'NexumBB' to 'mydb'. So the example should be the following and so it does not work:

 

<configuration>
<configSections>
<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false"/>
<section name="typeRegistrationProvidersConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.TypeRegistrationProvidersConfigurationSection, Microsoft.Practices.EnterpriseLibrary.Common, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</configSections>
<typeRegistrationProvidersConfiguration>
<remove name="Data Access" />
<add name="Data Access" providerType="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSyntheticConfigSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</typeRegistrationProvidersConfiguration>
<dataConfiguration defaultDatabase="mydb"/>
<connectionStrings>
<add name="mydb" connectionString="Database=testdb;User ID=myuser;Server=192.168.100.1;Persist Security Info=True;Password=mypwd" providerName="IBM.Data.DB2"/>
</connectionStrings>
</configuration>
Jun 17, 2010 at 2:26 PM

Ah ok, so how have you implemented your custom provider ("IBM.Data.DB2")? This same provider works with VS2008 right?

Jun 17, 2010 at 2:38 PM

I did not implement the provider by myself, IBM did the job: http://www-01.ibm.com/software/data/db2/ad/dotnet.html

And yes, this provider works within VS2008 and the same EntLib 5 setup. And this provider also works within other VS2010 solutions (for example an app with NHibernate to access DB2).

This VS2008 and VS2010 solutions are nothing more then simple testprojects, because my real app was'nt able to resolve a database-object from Unity.

I already hope I missed something and its me. Thats much better than an error, it results in not to use DAAB within my project. :)

 

Jun 21, 2010 at 7:43 AM

You should add a registration for your db2 dbproviderfactory in your config.  Refer to this link for the sample.

 

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Jun 24, 2010 at 11:51 AM

Actually it works. I added an appropriate section into my web.config and it works like a charm. So many thanks for your help. :)

The question is open how it can be, that an VS2008 project without registering a DB2Factory works and an VS2010 project does not. But I guess the answer is out of your scope/ responsibility.

Jun 25, 2010 at 1:27 AM

I'm using VS2008 and without adding a registration for the dbfactory, it doesn't work.  I'm curious, your VS2008 solution uses EntLib, right?  If you don't mind, I would like to have sample project which repros it.

 

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Jul 12, 2010 at 12:12 PM

Im sure I know the reason: within VS2008 there are IBM Database Addins installed. I guess they make the appropriate settings for the providerfactory. In that actually is no need to provide a solution. The manually added entries works. Thank you.